#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int n,m,op,x,y,last,tot; int fa[8000030],ls[8000030],rs[8000030],deep[8000030],root[200030]; void build(int l,int r,int &cur){ cur=++tot; if (l==r){fa[cur]=l; deep[cur]=1; return;} int mid=(l+r)>>1; build(l,mid,ls[cur]); build(mid+1,r,rs[cur]); } void modify(int l,int r,int &cur,int cur2,int x,int y){ cur=++tot;; if (l==r){fa[cur]=y; return;} int mid=(l+r)>>1; if (x<=mid){modify(l,mid,ls[cur],ls[cur2],x,y); rs[cur]=rs[cur2];} else{modify(mid+1,r,rs[cur],rs[cur2],x,y); ls[cur]=ls[cur2];} } int query(int l,int r,int cur,int x){ if (l==r) return cur; int mid=(l+r)>>1; return (x<=mid)?query(l,mid,ls[cur],x):query(mid+1,r,rs[cur],x); } int gfa(int x,int cur){ int p=query(1,n,cur,x); if (fa[p]==x) return p; return gfa(fa[p],cur); } void add(int l,int r,int cur,int y){ if (l==r){deep[cur]++; return;} int mid=(l+r)>>1; if (y<=mid) add(l,mid,ls[cur],y); else add(mid+1,r,rs[cur],y); } int main(){ scanf("%d%d",&n,&m); build(1,n,root[0]); for (int i=1;i<=m;i++){ scanf("%d",&op); if (op==1){ root[i]=root[i-1]; scanf("%d%d",&x,&y); x^=last; y^=last; x=gfa(x,root[i]); y=gfa(y,root[i]); if (fa[x]==fa[y]) continue; if (deep[x]>deep[y]) swap(x,y); modify(1,n,root[i],root[i-1],fa[x],fa[y]); if (deep[x]==deep[y]) add(1,n,root[i],fa[y]); } if (op==2){ scanf("%d",&x); x^=last; root[i]=root[x]; } if (op==3){ root[i]=root[i-1]; scanf("%d%d",&x,&y); x^=last; y^=last; x=gfa(x,root[i]); y=gfa(y,root[i]); if (fa[x]==fa[y]){last=1; puts("1");} else{last=0; puts("0");} } } return 0; }
|