LCT裸题了。题目怎么说就怎么打。
【代码】
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>
#define N 300005
#define INF 1e9
using namespace std;
typedef unsigned int ll;
typedef pair<ll,ll> pa;
int read()
{
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
int n,m,top;
int son[N][2],st[N],fa[N],Xor[N],a[N];
bool rev[N];
void Input_Init()
{
n=read();m=read();
for(int i=1;i<=n;i++) Xor[i]=a[i]=read();
}
bool isroot(int x){
return son[fa[x]][0]!=x&&son[fa[x]][1]!=x;
}
void pushup(int k){
Xor[k]=Xor[son[k][0]]^Xor[son[k][1]]^a[k];
}
void pushdown(int k){
if(!rev[k]) return;
rev[k]=0;rev[son[k][0]]^=1;rev[son[k][1]]^=1;
swap(son[k][0],son[k][1]);
}
void Rotate(int x)
{
int y=fa[x],z=fa[y],l,r;
l=son[y][1]==x;r=l^1;
if(!isroot(y)) son[z][son[z][1]==y]=x;
fa[x]=z;fa[y]=x;fa[son[x][r]]=y;
son[y][l]=son[x][r];son[x][r]=y;
pushup(y);pushup(x);
}
void Splay(int x)
{
st[++top]=x;
for(int i=x;!isroot(i);i=fa[i]) st[++top]=fa[i];
while(top) pushdown(st[top--]);
while(!isroot(x))
{
int y=fa[x],z=fa[y];
if(!isroot(y)) {
if(son[z][0]==y^son[y][0]==x) Rotate(x);
else Rotate(y);
}
Rotate(x);
}
}
void Access(int x){
for(int t=0;x;t=x,x=fa[x])
Splay(x),son[x][1]=t,pushup(x);
}
void Make_Root(int x){
Access(x);Splay(x);rev[x]^=1;
}
void Split(int x,int y){
Make_Root(x);Access(y);Splay(y);
}
void Link(int x,int y){
Make_Root(x);fa[x]=y;
}
void Cut(int x,int y){
Split(x,y);
son[y][0]=fa[x]=0;pushup(y);
}
int Find(int x){
Access(x);Splay(x);
while(son[x][0]) x=son[x][0];
return x;
}
void Solve_Query(int x,int y){
Split(x,y);printf("%d\n",Xor[y]);
}
void Solve_Link(int x,int y){
if(Find(x)==Find(y)) return;
Link(x,y);
}
void Solve_Cut(int x,int y){
if(Find(x)!=Find(y)) return;
Cut(x,y);
}
void Solve_Change(int x,int y){
Splay(x);Xor[x]=a[x]=y;
}
void Solve()
{
static int f,x,y;
while(m--)
{
f=read(),x=read(),y=read();
if(!f) Solve_Query(x,y);
else if(f==1) Solve_Link(x,y);
else if(f==2) Solve_Cut(x,y);
else Solve_Change(x,y);
}
}
int main()
{
Input_Init();
Solve();
return 0;
}