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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| #include <bits/stdc++.h> using namespace std; inline int gin(){ int s=0,f=1; char c=getchar(); while(c<'0' || c>'9'){ if(c=='-') f=-1; c=getchar(); } while(c>='0'&&c<='9'){ s=(s<<3)+(s<<1)+(c^48); c=getchar(); } return s*f; }
const int N=2e6+5; int n,m,fa[N],lc[N],rc[N],val[N],sz[N],rt,tot,cnt[N];
inline void push(int x){ if(x){ sz[x]=cnt[x]; if(lc[x]) sz[x]+=sz[lc[x]]; if(rc[x]) sz[x]+=sz[rc[x]]; } return; }
void rotate(int x){ int p=fa[x],g=fa[p]; int b=lc[p]==x?rc[x]:lc[x]; fa[x]=g,fa[p]=x; if(b) fa[b]=p; if(g) (lc[g]==p?lc[g]:rc[g])=x; if(x==lc[p]) rc[x]=p,lc[p]=b; else lc[x]=p,rc[p]=b; push(p);push(x); }
inline bool wrt(int x){return x==rc[fa[x]];} void splay(int x,int tar=0){ while(fa[x]!=tar){ if(fa[fa[x]]!=tar) wrt(x)==wrt(fa[x])?rotate(fa[x]):rotate(x); rotate(x); } if(!tar) rt=x; }
inline int find(int v){ int x=rt; while(x){ if(v==val[x]) break; if(val[x]>v) x=lc[x]; else x=rc[x]; } if(x) splay(x); return x; }
inline void ins(int v){ int x=rt,p=0,dir=0; while(x){ ++sz[p=x]; if(v==val[x]){cnt[x]++;push(x);splay(x);return;} if(v< val[x]) x=lc[x],dir=0; else x=rc[x],dir=1; } x=++tot; fa[x]=p,val[x]=v,sz[x]=1,cnt[x]=1; if(p) (dir==0?lc[p]:rc[p])=x; push(p); splay(x); }
inline void join(int u,int v){ fa[u]=0,fa[v]=0; int w=u; while(rc[w]) w=rc[w]; splay(w); rc[w]=v,fa[v]=w; }
inline void del(int v){ int x=find(v); if(cnt[x]>=2){cnt[x]--;push(x);return;} if(!lc[x] || !rc[x]) fa[rt=lc[x]|rc[x]]=0; else join(lc[x],rc[x]); }
inline int rk(int v){ int x=find(v); return sz[lc[x]]+1; }
inline int kth(int k){ int x=rt,y=k; while(x){ if(y<=sz[lc[x]]) x=lc[x]; else { y-=sz[lc[x]]+cnt[x]; if(y<=0){ splay(x); return val[x]; } x=rc[x]; } } }
inline int pre(){ int x=lc[rt]; while(rc[x]) x=rc[x]; splay(x); return x; } inline int nxt(){ int x=rc[rt]; while(lc[x]) x=lc[x]; splay(x); return x; }
signed main(){ n=gin(),m=gin(); for(int i=1;i<=n;i++) ins(gin()); int last=0,ans=0; for(int i=1;i<=m;i++){ int op=gin(),x=gin(); x^=last; switch(op){ case 1:ins(x);break; case 2:del(x);break; case 3:ins(x);last=rk(x);del(x);break; case 4:last=kth(x);break; case 5:ins(x);last=val[pre()];del(x);break; case 6:ins(x);last=val[nxt()];del(x);break; } if(op>=3) ans^=last; } printf("%d\n",ans); return 0; }
|