3282: Tree
Time Limit: 30 Sec Memory Limit: 512 MB
Submit: 1265 Solved: 552
Description
给定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。
Input
第1行两个整数,分别为N和M,代表点数和操作数。
第2行到第N+1行,每行一个整数,整数在[1,10^9]内,代表每个点的权值。
第N+2行到第N+M+1行,每行三个整数,分别代表操作类型和操作所需的量。
Output
对于每一个0号操作,你须输出X到Y的路径上点权的Xor和。
Sample Input
3 3
1
2
3
1 1 2
0 1 2
0 1 1
Sample Output
3
1
HINT
1<=N,M<=300000
Source
动态树
LCT裸题。。
附上本蒟蒻的代码:
#include<cstdio>
#include<iostream>
using namespace std;
#define MAXN 300001
int n,m,father[MAXN],c[MAXN][2],st[MAXN],sum[MAXN],val[MAXN];
bool rev[MAXN];
int read()
{
int w=0,f=1; char ch=getchar();
while (ch<'0' || ch>'9')
{
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0' && ch<='9')
w=w*10+ch-'0',ch=getchar();
return w*f;
}
bool isroot(int x)
{
return c[father[x]][0]!=x && c[father[x]][1]!=x;
}
void update(int x)
{
int l=c[x][0],r=c[x][1];
sum[x]=sum[l]^sum[r]^val[x];
}
void pushdown(int x)
{
int l=c[x][0],r=c[x][1];
if (rev[x]) rev[x]^=1,rev[l]^=1,rev[r]^=1,swap(c[x][0],c[x][1]);
}
void rotate(int x)
{
int y=father[x],z=father[y],l,r;
if (c[y][0]==x) l=0;
else l=1;
r=l^1;
if (!isroot(y))
if (c[z][0]==y) c[z][0]=x;
else c[z][1]=x;
father[x]=z,father[y]=x,father[c[x][r]]=y,c[y][l]=c[x][r],c[x][r]=y,update(y),update(x);
}
void splay(int x)
{
int i,top=0,y,z;
st[++top]=x;
for (i=x;!isroot(i);i=father[i]) st[++top]=father[i];
for (i=top;i;i--) pushdown(st[i]);
while (!isroot(x))
{
y=father[x],z=father[y];
if (!isroot(y))
if (c[y][0]==x^c[z][0]==y) rotate(x);
else rotate(y);
rotate(x);
}
}
void access(int x)
{
int t;
for (t=0;x;t=x,x=father[x]) splay(x),c[x][1]=t,update(x);
}
void rever(int x)
{
access(x),splay(x),rev[x]^=1;
}
void link(int x,int y)
{
rever(x),father[x]=y;
}
void cut(int x,int y)
{
rever(x),access(y),splay(y),c[y][0]=father[x]=0;
}
int find(int x)
{
int y=x;
access(x),splay(x);
while (c[y][0]) y=c[y][0];
return y;
}
int main()
{
int i,x,y,p;
n=read(),m=read();
for (i=1;i<=n;i++) sum[i]=val[i]=read();
for (i=1;i<=m;i++)
{
p=read(),x=read(),y=read();
if (p==0) rever(x),access(y),splay(y),printf("%d\n",sum[y]);
if (p==1)
if (find(x)==find(y)) continue;
else link(x,y);
if (p==2)
if (find(x)!=find(y)) continue;
else cut(x,y);
if (p==3) access(x),splay(x),val[x]=y,update(x);
}
return 0;
}