1个WA,6个MLE。
#include<bits/stdc++.h>
using namespace std;
namespace io{
int read(){
int r=0; char c=0; bool f=1;
do { if(c=='-') f=0; c=getchar(); } while(!isdigit(c));
do r=(r<<3)+(r<<1)+(c^'0'), c=getchar(); while(isdigit(c));
return f ? r : (~r+1);
}
void write(int x){
if(x<0) { putchar('-'), write(~x+1); return; }
int s[45], idx=0;
do s[++idx]=x%10, x/=10; while(x);
do putchar(s[idx--]+'0'); while(idx);
}
}
using namespace io;
const int N=1e5+3, INF=0x7fffffff;
struct treap{
int w, l, r, dat, cnt, son;
} a[N];
int root, idx;
int New(int x){
a[++idx].w=x;
a[idx].l=a[idx].r=a[idx].son=0;
a[idx].cnt=1;
a[idx].dat=rand();
return idx;
}
void Build(){
New(-INF), New(INF);
a[1].r=2, root=1, a[1].son=-1;
}
void zig(int &p){
int q=a[p].l;
a[p].son-=a[q].cnt+a[q].son;
a[p].l=a[q].r, a[q].r=p, p=q;
}
void zag(int &p){
int q=a[p].r;
a[q].son+=a[p].cnt+a[p].son;
a[p].r=a[q].l, a[q].l=p, p=q;
}
void Insert(int &p, int x){
if(!p) p=New(x);
else if(a[p].w==x) ++a[p].cnt;
else if(x<a[p].w){
++a[p].son, Insert(a[p].l, x);
if(a[a[p].l].dat>a[p].dat) zig(p);
}
else{
Insert(a[p].r, x);
if(a[a[p].r].dat>a[p].dat) zag(p);
}
}
void Remove(int &p, int x){
if(x==a[p].w){
if(a[p].cnt>1) --a[p].cnt;
else{
if(a[p].l==0&&a[p].r==0) p=0;
else if(a[p].l==0) p=a[p].r;
else if(a[p].r==0) p=a[p].l;
else{
if(a[a[p].l].dat>a[a[p].r].dat) zig(p), Remove(a[p].r, x);
else zag(p), Remove(a[p].l, x);
}
}
}
else if(x<a[p].w) --a[p].son, Remove(a[p].l, x);
else Remove(a[p].r, x);
}
int GetRank(int p, int x, int now){
if(a[p].w==x) return now+a[p].son+1;
else if(x<a[p].w) return GetRank(a[p].l, x, now);
else { now+=a[p].son+a[p].cnt; return GetRank(a[p].r, x, now);}
}
int GetNum(int p, int x, int now){
if(a[p].w==-INF) return GetNum(a[p].r, x, now);
else if(a[p].w==INF) return GetNum(a[p].l, x, now);
else if(x<a[p].son+now+1) return GetNum(a[p].l, x, now);
else if(x<=a[p].son+now+a[p].cnt&&x>=a[p].son+now+1) return a[p].w;
else return GetNum(a[p].r, x, now+a[p].cnt+a[p].son);
}
int GetPre(int p, int x, int fa, int ans){
if(!p) return a[ans].w;
else if(x==a[p].w){
if(a[p].l==0) return fa;
else{
int ans=a[p].l;
while(a[ans].r) ans=a[ans].r;
return a[ans].w;
}
}
else if(x<a[p].w) return GetPre(a[p].l, x, p, ans);
else {ans=p; return GetPre(a[p].r, x, p, ans);}
}
int GetBack(int p, int x, int ans){
if(!p) return a[ans].w;
else if(x==a[p].w){
int ans=a[p].r;
while(a[ans].l) ans=a[ans].l;
return a[ans].w;
}
else if(x<a[p].w){ ans=p; return GetBack(a[p].l, x, ans); }
else return GetBack(a[p].r, x, ans);
}
int main(){
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
Build();
int n;
n=read();
while(n--){
int opt, x;
opt=read(), x=read();
switch(opt){
case 1: Insert(root, x);
break;
case 2: Remove(root, x);
break;
case 3: write(GetRank(root,x,0)), puts("");
break;
case 4: write(GetNum(root,x,0)), puts("");
break;
case 5: write(GetPre(root,x,root,root)), puts("");
break;
default: write(GetBack(root,x,root)), puts("");
break;
}
}
return 0;
}
求助