【bzoj3282】Tree LCT

题目描述

给定N个点以及每个点的权值,要你处理接下来的M个操作。操作有4种。操作从0到3编号。点从1到N编号。

0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和。保证x到y是联通的。

1:后接两个整数(x,y),代表连接x到y,若x到Y已经联通则无需连接。

2:后接两个整数(x,y),代表删除边(x,y),不保证边(x,y)存在。

3:后接两个整数(x,y),代表将点X上的权值变成Y。

输入

第1行两个整数,分别为N和M,代表点数和操作数。

第2行到第N+1行,每行一个整数,整数在[1,10^9]内,代表每个点的权值。

第N+2行到第N+M+1行,每行三个整数,分别代表操作类型和操作所需的量。

输出

对于每一个0号操作,你须输出X到Y的路径上点权的Xor和。

样例输入

3 3
1
2
3
1 1 2
0 1 2
0 1 1

样例输出

3
1


题解

真正的LCT模板题,几乎包括了LCT所有基本操作

#include <cstdio>
#include <algorithm>
#define N 300010
#define lson c[0][x]
#define rson c[1][x]
using namespace std;
int fa[N] , c[2][N] , w[N] , sum[N] , rev[N];
char str[5];
void pushup(int x)
{
	sum[x] = sum[lson] ^ sum[rson] ^ w[x];
}
void pushdown(int x)
{
	if(rev[x])
	{
		swap(c[0][lson] , c[1][lson]);
		swap(c[0][rson] , c[1][rson]);
		rev[lson] ^= 1 , rev[rson] ^= 1;
		rev[x] = 0;
	}
}
bool isroot(int x)
{
	return c[0][fa[x]] != x && c[1][fa[x]] != x;
}
void update(int x)
{
	if(!isroot(x)) update(fa[x]);
	pushdown(x);
}
void rotate(int x)
{
	int y = fa[x] , z = fa[y] , l = (c[1][y] == x) , r = l ^ 1;
	if(!isroot(y)) c[c[1][z] == y][z] = x;
	fa[x] = z , fa[y] = x , fa[c[r][x]] = y , c[l][y] = c[r][x] , c[r][x] = y;
	pushup(y) , pushup(x);
}
void splay(int x)
{
	update(x);
	while(!isroot(x))
	{
		int y = fa[x] , z = fa[y];
		if(!isroot(y))
		{
			if((c[0][y] == x) ^ (c[0][z] == y)) rotate(x);
			else rotate(y);
		}
		rotate(x);
	}
}
void access(int x)
{
	int t = 0;
	while(x) splay(x) , rson = t , pushup(x) , t = x , x = fa[x];
}
void makeroot(int x)
{
	access(x) , splay(x);
	swap(lson , rson) , rev[x] ^= 1;
}
int find(int x)
{
	access(x) , splay(x);
	while(lson) pushdown(x) , x = lson;
	return x;
}
void link(int x , int y)
{
	makeroot(x) , fa[x] = y;
}
void cut(int x , int y)
{
	makeroot(x) , access(y) , splay(y);
	if(c[0][y] == x && c[1][x] == 0) fa[x] = c[0][y] = 0 , pushup(y);
}
void split(int x , int y)
{
	makeroot(y) , access(x) , splay(x);
}
int main()
{
	int n , m , i , p , x , y;
	scanf("%d%d" , &n , &m);
	for(i = 1 ; i <= n ; i ++ ) scanf("%d" , &w[i]) , sum[i] = w[i];
	while(m -- )
	{
		scanf("%d%d%d" , &p , &x , &y);
		switch(p)
		{
			case 0: split(x , y) , printf("%d\n" , sum[x]); break;
			case 1: if(find(x) != find(y)) link(x , y); break;
			case 2: cut(x , y); break;
			default: split(x , x) , w[x] = y , pushup(x);
		}
	}
	return 0;
}

 

posted @ 2017-05-02 10:59  GXZlegend  阅读(242)  评论(0编辑  收藏  举报