如题,TLE+RE
#include<bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;
const int N=1e5+7;
int n,opt,xx,root,cnt;
struct sss {
int son[2];
int fa,cnt,val,siz;
} tre[N];
inline void push_up(int x) {
tre[x].siz=tre[tre[x].son[0]].siz+tre[tre[x].son[1]].siz+tre[x].cnt;
}
inline void clear(int x) {
tre[x]={0,0,0,0,0,0};
}
inline int get(int x) {
return tre[tre[x].fa].son[1] == x ;
}
inline void rotate(int x) {
int f=tre[x].fa,sonx=get(x),gf=tre[tre[x].fa].fa,sonf=get(tre[x].fa);
tre[tre[x].son[sonx^1]].fa=f;
tre[f].son[sonx]=tre[x].son[sonx^1];
tre[x].son[sonx^1]=f;
tre[f].fa=x;
tre[x].fa=gf;
if(gf) tre[gf].son[sonf]=x;
push_up(f);
push_up(x);
}
inline void Splay(int x) {
for(int f;f=tre[x].fa;rotate(x))
if(tre[tre[x].fa].fa) rotate(tre[x].fa);
root=x;
}
int find(int x) {
int pos=root ;
while(tre[pos].val != x) {
if(tre[pos].val < x) {
pos=tre[pos].son[1];
} else {
pos=tre[pos].son[0];
}
}
Splay(pos);
return pos;
}
inline int find_left(int x) {
while(tre[x].son[0]) {
x=tre[x].son[0] ;
}
Splay(x);
return x;
}
void f1(int x) {
if(!root) {
root=++cnt;
tre[cnt].cnt=tre[cnt].siz=1;
tre[cnt].val=x;
tre[cnt].fa=0;
return ;
}
int now=root,f=0;
while (1) {
if(tre[now].val==x) {
tre[now].cnt ++;
push_up(now) ;
push_up(f) ;
Splay(now) ;
return ;
}
f=now;
now=tre[now].son[tre[now].val<x];
if(!now) {
tre[++cnt].val=x;
tre[cnt].cnt++;
tre[cnt].siz++;
tre[cnt].fa=f;
tre[f].son[tre[f].val<x]=cnt;
push_up(f);
Splay(cnt) ;
break;
}
}
}
void f2(int x) {
int now=find(x);
root=0;
if(tre[now].cnt > 1) {
tre[now].cnt--;
push_up(now) ;
return ;
}
if(!tre[now].son[0]&&!tre[now].son[1]) {
tre[tre[now].fa].son[get(now)]=0;
clear(now);
return ;
}
if(!tre[now].son[1]) {
root=tre[now].son[0];
tre[tre[now].son[0]].fa=0;
clear(now);
return ;
}
if(!tre[now].son[0]) {
root=tre[now].son[1];
tre[tre[now].son[1]].fa=0;
clear(now);
return ;
}
int lf=find_left(tre[now].son[1]);
tre[tre[now].son[0]].fa=lf;
tre[lf].son[0]=tre[now].son[0];
clear(now);
Splay(lf);
push_up(lf);
}
int f3(int x) {
int o=root;
int ret=0;
while(1) {
if(tre[o].val>x) {
if(!tre[o].son[0]) {
break;
}
o=tre[o].son[0];
} else {
if(tre[o].son[0])
ret+=tre[tre[o].son[0]].siz;
if(!tre[o].son[1]||tre[o].val==x) {
break;
}
ret+=tre[o].cnt;
o=tre[o].son[1];
}
}
Splay(o);
return ret;
}
int f4(int x) {
x--;
int o=root;
int temp=tre[tre[o].son[0]].siz;
while(temp!=x) {
if(temp>x) {
o=tre[o].son[0];
temp-=tre[tre[o].son[1]].siz+1;
} else {
o=tre[o].son[1];
temp+=tre[tre[o].son[0]].siz+1;
}
}
Splay(o);
return tre[o].val;
}
int f5(int x) {
int o=root;
int ret;
while(1) {
if(tre[o].val>=x) {
if(!tre[o].son[0]) {
break;
}
o=tre[o].son[0];
} else {
ret=o;
if(!tre[o].son[1]) {
break;
}
o=tre[o].son[1];
}
}
Splay(ret);
return tre[ret].val;
}
int f6(int x) {
int o=root;
int ret;
while(1) {
if(tre[o].val>x) {
ret=o;
if(!tre[o].son[0]) {
break;
}
o=tre[o].son[0];
} else {
if(!tre[o].son[1]) {
break;
}
o=tre[o].son[1];
}
}
Splay(ret);
return tre[ret].val;
}
int main() {
for(int i=0;i<=N;i++) clear(i);
cin>>n;
while(n--) {
cin>>opt>>xx;
if(opt==1) {
f1(xx);
}
if(opt==2) {
f2(xx);
}
if(opt==3) {
cout<<f3(xx)+1<<"\n";
}
if(opt==4) {
cout<<f4(xx)<<"\n";
}
if(opt==5) {
cout<<f5(xx)<<"\n";
}
if(opt==6) {
cout<<f6(xx)<<"\n";
}
}
return 0;
}