rt,人麻了。应该是删除插入的时候出了锅。
调试没删方便查错(?)。
#include <bits/stdc++.h>
//#define int long long
#define Rg register
#define Ri Rg int
#define Il inline
#define vec vector
#define pb push_back
#define fi first
#define se second
#define IT iterator
using namespace std;
typedef double db;
typedef pair<int,int> pii;
const int N=2e6,Inf=1e9;
const db eps=1e-8;
Il int read(){
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=(x<<1)+(x<<3)+(ch^48);
return x*f;
}
int n,Q,lans=0,ans=0;
int g[N+5];
struct Splay{
int id=0,rt=0,cnt[N+5],va[N+5],sz[N+5],ch[N+5][2],fa[N+5];
#define type(p) (ch[fa[p]][1]==p)
Il void pup(int p){
sz[p]=sz[ch[p][0]]+sz[ch[p][1]]+cnt[p];
return;
}
Il void rot(int p){
int fp=fa[p],ff=fa[fp];
bool ty=type(p);
ch[fp][ty]=ch[p][!ty],fa[ch[p][!ty]]=fp;
ch[p][!ty]=fp,fa[fp]=p,fa[p]=ff;
if(ff)ch[ff][type(fp)]=p;
pup(fp),pup(p);
return;
}
Il void splay(int p,int to){
for(Ri f;(f=fa[p])^to;rot(p))if(fa[f]^to)rot(type(p)==type(f)?f:p);
if(!to)rt=p;
return;
}
Il void pick(int x){
if(!rt)return;
int p=rt;
while(va[p]!=x&&ch[p][x>va[p]])p=ch[p][x>va[p]];
splay(p,0);
return;
}
Il void ins(int x){
if(!rt){rt=++id;va[rt]=x;cnt[rt]=sz[rt]=1;return;}
int p=rt,fp=rt;
while(p&&va[p]!=x)fp=p,p=ch[p][x>va[p]];
if(p)cnt[p]++;
else p=++id,cnt[p]=1,va[p]=x,fa[p]=fp,ch[fp][x>va[fp]]=p;
pup(p);
splay(p,0);
return;
}
Il void del(int x){
pick(x);
if(va[rt]!=x)return;
if(cnt[rt]>1)cnt[rt]--;
else if(!ch[rt][0]&&!ch[rt][1])rt=0;
else if(!ch[rt][0])rt=ch[rt][1],fa[rt]=0;
else if(!ch[rt][1])rt=ch[rt][0],fa[rt]=0;
else{
int p=ch[rt][0];
while(ch[p][1])p=ch[p][1];
splay(p,rt);
ch[p][1]=ch[rt][1],rt=p,fa[rt]=0;
}
pup(rt);
return;
}
Il int rnk(int x){
pick(x);
return sz[ch[rt][0]]+(va[rt]<x?cnt[rt]+1:1);
}
Il int kth(int K){
int p=rt;
while(1){
if(K<=sz[ch[p][0]])p=ch[p][0];
else if(K<=sz[ch[p][0]]+cnt[p]){splay(p,0);return va[p];}
else K-=sz[ch[p][0]]+cnt[p],p=ch[p][1];
}
return 0;
}
Il int pre(int x){
return kth(rnk(x)-1);
}
Il int suf(int x){
return kth(rnk(x+1));
}
#undef type
}bst;
signed main(){
n=read(),Q=read();
bst.ins(Inf),bst.ins(-Inf);
for(Ri i=1;i<=n;i++)bst.ins(read());
while(Q--){
int op=read(),x=read()^lans;
if(op==1)bst.ins(x);
if(op==2)bst.del(x);
if(op==3)ans^=(lans=bst.rnk(x)-1),printf("ans:%d\n",lans);
if(op==4)ans^=(lans=bst.kth(x+1)),printf("ans:%d\n",lans);
if(op==5)ans^=(lans=bst.pre(x)),printf("ans:%d\n",lans);
if(op==6)ans^=(lans=bst.suf(x)),printf("ans:%d\n",lans);
printf("size:%d\n",bst.sz[bst.rt]);
}
printf("%d",ans);
return 0;
}
谢谢。