LCT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<string>
#include<cstring>
#include<map>
using namespace std;
#define ll long long
#define R register
#define fo(a,b,c) for(R int a=b;a<=c;++a)
#define mmt(x,y) memset(x,y,sizeof(x))
#define dbg(x) cout<<#x<<" = "<<x<<'\n'
#define maxn 200100
int n,m,val[maxn];
int c[maxn][2],fa[maxn],xr[maxn],q[maxn],rev[maxn];
void up(int x){xr[x]=xr[c[x][0]]^xr[c[x][1]]^val[x];}
void down(int x)
{
if(rev[x])
{
rev[c[x][0]]^=1;rev[c[x][1]]^=1;rev[x]^=1;
swap(c[x][0],c[x][1]);
}
}
bool isrot(int x){return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;}
void rotate(int x)
{
R int y=fa[x],z=fa[y],l,r;
if(c[y][0]==x)l=0;else l=1;
r=l^1;
if(!isrot(y)){if(c[z][0]==y)c[z][0]=x;else c[z][1]=x;}
fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
c[y][l]=c[x][r];c[x][r]=y;
up(y);up(x);
}
void splay(int x)
{
int top=1;q[top]=x;
for(int i=x;!isrot(i);i=fa[i])q[++top]=fa[i];
for(int i=top;i;--i)down(q[i]);
while(!isrot(x))
{
R int y=fa[x],z=fa[y];
if(!isrot(y))
{
if((c[y][0]==x)^(c[z][0]==y))rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x)
{
for(int t=0;x;t=x,x=fa[x])
splay(x),c[x][1]=t,up(x);
}
void makerot(int x){access(x);splay(x);rev[x]^=1;}
int find(int x){access(x);splay(x);while(c[x][0])x=c[x][0];return x;}
void split(int x,int y){makerot(x);access(y);splay(y);}
void cut(int x,int y){split(x,y);if(c[y][0]==x)c[y][0]=0,fa[x]=0;}
void link(int x,int y){makerot(x);fa[x]=y;}
int main()
{
scanf("%d%d",&n,&m);
fo(i,1,n)scanf("%d",&val[i]),xr[i]=val[i];
while(m--)
{
R int odd,x,y;scanf("%d%d%d",&odd,&x,&y);
if(odd==0)
{
split(x,y);
printf("%d\n",xr[y]);
}
else if(odd==1)
{
if(find(x)!=find(y))link(x,y);
}
else if(odd==2)
{
if(find(x)==find(y))cut(x,y);
}
else access(x),splay(x),val[x]=y,up(x);
}
return 0;
}
显示 Gitment 评论