测试链接:https://www.luogu.org/problemnew/show/P3690
#include<stdio.h>
#include<algorithm>
#define N 200005
using namespace std;
int rev[N],ss[N],ch[N][2],pre[N],st[N],t,n,val[N];
bool is_root(int x){return ch[pre[x]][0]!=x&&ch[pre[x]][1]!=x;}
void push_rev(int x)//更新翻转信息
{
if(!rev[x])return;
rev[x]^=1;swap(ch[x][0],ch[x][1]);
rev[ch[x][0]]^=1;rev[ch[x][1]]^=1;
}
void update(int x)
{
ss[x]=val[x];
if(ch[x][0])ss[x]^=ss[ch[x][0]];
if(ch[x][1])ss[x]^=ss[ch[x][1]];
}
void rotate(int x)
{
int y=pre[x],z=pre[y];
bool f=ch[y][1]==x;
if(!is_root(y))ch[z][ch[z][1]==y]=x;
ch[y][f]=ch[x][!f];ch[x][!f]=y;
pre[x]=z;pre[y]=x;pre[ch[y][f]]=y;
update(y);update(x);
}
void splay(int x)
{
st[t=1]=x;
for(int y=x;!is_root(y);st[++t]=y=pre[y]);
for(;t;t--)if(rev[st[t]])push_rev(st[t]);
for(;!is_root(x);rotate(x))if(!is_root(pre[x]))
(ch[pre[x]][0]==x)^(ch[pre[pre[x]]][0]==pre[x])?rotate(x):rotate(pre[x]);
update(x);
}
void access(int x)
{
for(int t=0;
x;ch[x][1]=t,t=x,x=pre[x])splay(x);
}
void make_root(int x)
{
access(x);
splay(x);
rev[x]^=1;
}
void link(int x,int y)//连接两个点
{
make_root(x);
pre[x]=y;
}
int findroot(int x)//找x点所在splay树的根
{
access(x);
splay(x);
push_rev(x);
while(ch[x][0])push_rev(x),x=ch[x][0];
splay(x);
return x;
}
void cut(int x,int y)//切断x到y的边
{
make_root(x);
if(findroot(y)==x&&pre[y]==x&&!ch[y][0])
{
pre[y]=ch[x][1]=0;
update(x);
}
}
void path(int x,int y)//提取x到y的路径
{
make_root(x);
access(y);
splay(y);
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&val[i]);
for(int i=1;i<=m;i++)
{
int op,x,y;
scanf("%d%d%d",&op,&x,&y);
if(op==0)
{
path(x,y);
printf("%d\n",ss[y]);
}
else if(op==1)
{
link(x,y);
}
else if(op==2)
{
cut(x,y);
}
else
{
splay(x);
val[x]=y;
update(x);
}
}
return 0;
}