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;
}