在看了大佬的博客后写下(抄下)的……
大佬的博客:https://www.cnblogs.com/olinr/p/9455411.html
#include<bits/stdc++.h>
#define maxx 400005
using namespace std;
int c[maxx][2],val[maxx],fa[maxx],dis[maxx];
bool rev[maxx];
struct node{
int st[maxx];
int tp;
bool empty(){return tp==0;}
void pop(){st[tp--]=0;}
void push(int x){st[++tp]=x;}
int top(){return st[tp];}
}s;
bool pdroot(int x){return (c[fa[x]][0]==x||c[fa[x]][1]==x);}
void put_up(int x){dis[x]=dis[c[x][0]]^dis[c[x][1]]^val[x];}
void zychange(int x)
{
rev[x]^=1;
swap(c[x][0],c[x][1]);
}
void put_down(int x)
{
if(rev[x])
{
if(c[x][0])zychange(c[x][0]);
if(c[x][1])zychange(c[x][1]);
rev[x]=0;
}
}
void rot(int x)
{
int y=fa[x],z=fa[y];
int l=(c[y][1]==x),r=l^1;
if(pdroot(y))c[z][c[z][1]==y]=x;
fa[x]=z;
fa[y]=x;
fa[c[x][r]]=y;
c[y][l]=c[x][r];c[x][r]=y;//注意 这个语句顺序不能换!!(卡了我半小时……)
put_up(y);
put_up(x);
}
void splay(int x)
{
int y=x;
s.push(y);
while(pdroot(y))
{
y=fa[y];
s.push(y);
}
while(!s.empty())
{
put_down(s.top());
s.pop();
}
while(pdroot(x))
{
int y=fa[x],z=fa[y];
int l=(c[y][1]==x),l1=(c[z][1]==y);
if(pdroot(y))
{
if(l^l1)rot(x);
else rot(y);
}
rot(x);
}
put_up(x);
}
void access(int x)
{
for(int y=0;x;x=fa[y=x])
{
splay(x);
c[x][1]=y;
put_up(x);
}
}
void makeroot(int x)
{
access(x);
splay(x);
zychange(x);
}
void split(int x,int y)
{
makeroot(x);
access(y);
splay(y);
}
int findroot(int x)
{
access(x);
splay(x);
while(c[x][0])
{
put_down(x);
x=c[x][0];
}
return x;
}
void link(int x,int y)
{
makeroot(x);
if(findroot(y)!=x)fa[x]=y;
}
void cut(int x,int y)
{
makeroot(x);
if(findroot(y)==x&&fa[x]==y&&!c[x][1])
{
fa[x]=c[y][0]=0;
put_up(y);
}
}
int main()
{
int n,m;
int x,y,ch;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&val[i]);
while(m--)
{
scanf("%d%d%d",&ch,&x,&y);
if(ch==0)
{
split(y,x);
printf("%d",dis[x]);
putchar('\n');
continue;
}
if(ch==1)
{
link(x,y);
continue;
}
if(ch==2)
{
cut(y,x);
continue;
}
if(ch==3)
{
splay(x);
val[x]=y;
continue;
}
}
}