93分,WA on test #8
Wrong Answer.wrong answer On line 50001 column 2, read 3, expected 0.
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
struct node
{
int ch[2],fa,val,xtot,tag;
}t[N];
int n,m,rt,tot;
int getpos(int cur)
{
return cur==t[t[cur].fa].ch[1];
}
int isroot(int cur)
{
return t[t[cur].fa].ch[0]!=cur&&t[t[cur].fa].ch[1]!=cur;
}
void pushup(int cur)
{
t[cur].xtot=t[cur].val;
if(t[cur].ch[0])t[cur].xtot^=t[t[cur].ch[0]].xtot;
if(t[cur].ch[1])t[cur].xtot^=t[t[cur].ch[1]].xtot;
return;
}
void addtag(int cur)
{
t[cur].tag^=1;
swap(t[cur].ch[0],t[cur].ch[1]);
return;
}
void pushdown(int cur)
{
if(!t[cur].tag)return;
if(t[cur].ch[0])addtag(t[cur].ch[0]);
if(t[cur].ch[1])addtag(t[cur].ch[1]);
t[cur].tag=0;
return;
}
void update(int cur)
{
if(!isroot(cur))update(t[cur].fa);
pushdown(cur);
return;
}
int create(int val)
{
tot++;
t[tot].val=t[tot].xtot=val;
t[tot].ch[0]=t[tot].ch[1]=0;
t[tot].fa=t[tot].tag=0;
return tot;
}
void rotate(int cur)
{
int fa=t[cur].fa,gp=t[fa].fa,ps=getpos(cur);
if(!isroot(fa))t[gp].ch[getpos(fa)]=cur;
t[fa].ch[ps]=t[cur].ch[ps^1];
if(t[cur].ch[ps^1])t[t[cur].ch[ps^1]].fa=fa;
t[cur].ch[ps^1]=fa;
t[fa].fa=cur; t[cur].fa=gp;
pushup(fa);pushup(cur);
return;
}
void splay(int cur)
{
update(cur);
for(int fa=t[cur].fa;!isroot(cur);fa=t[cur].fa)
{
if(!isroot(fa))
rotate(getpos(cur)==getpos(fa)?fa:cur);
rotate(cur);
}
return;
}
int access(int cur)
{
int son=0;
while(cur)
{
splay(cur);
t[cur].ch[1]=son;
pushup(cur);
son=cur,cur=t[cur].fa;
}
return son;
}
void makeroot(int cur)
{
cur=access(cur);
addtag(cur);
}
void split(int x,int y)
{
makeroot(x);
access(y);
splay(y);
return;
}
int find(int cur)
{
access(cur);
splay(cur);
while(t[cur].ch[0])
cur=t[cur].ch[0];
splay(cur);
return cur;
}
void link(int x,int y)
{
makeroot(x);
splay(x);
if(find(y)==x)return;
t[x].fa=y;
return;
}
void cut(int x,int y)
{
makeroot(x);
access(y);
splay(y);
if(t[y].fa!=x||t[y].ch[0])return;
t[y].fa=t[x].ch[0]=0;
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
int opt,x,y;
for(int i=1;i<=n;i++)
{
cin>>x;
create(x);
}
for(int i=1;i<=m;i++)
{
cin>>opt>>x>>y;
if(opt==0)
{
split(x,y);
if(i==50001)cout<<t[y].xtot<<"\n";
}
if(opt==1)link(x,y);
if(opt==2)cut(x,y);
if(opt==3)
{
splay(x);
t[x].val=y;
pushup(x);
}
}
return 0;
}