Splay,TLE,only AC on #3、#14
#include<bits/stdc++.h>
using namespace std;
int n,op,x,rt,siz,fa[100010],son[100010][10],num[100010],cnt[100010],sz[100010];
void all_zero(int x){
fa[x]=0;
son[x][0]=0;
son[x][1]=0;
num[x]=0;
cnt[x]=0;
sz[x]=0;
}int get(int x){
if(x==son[fa[x]][0]){
return 0;
}return 1;
}void pushup(int x){
if(x>=1){
sz[x]=cnt[x];
if(son[x][0]>=1){
sz[x]+=sz[son[x][0]];
}if(son[x][1]>=1){
sz[x]+=sz[son[x][1]];
}
}
}void rotate(int x){
int f=fa[x],flag=get(x);
int gra=fa[f];
son[f][flag]=son[x][flag^1];
fa[son[f][flag]]=f;
son[x][flag^1]=f;
fa[f]=x,fa[x]=gra;
if(gra>=1){
if(son[gra][1]==f){
son[gra][1]=x;
}else{
son[gra][0]=x;
}
}pushup(f),pushup(x);
}void Splay(int x){
for(int f;f=fa[x];rotate(x)){
if(fa[f]>=1){
if(get(x)==get(f)){
rotate(f);
}else{
rotate(x);
}
}
}rt=x;
}void insert(int x){
if(rt==0){
siz++,rt=siz;
son[rt][0]=son[rt][1]=fa[rt]=0;
num[rt]=x,cnt[rt]=1,sz[rt]=1;
return;
}int now=rt,f=0;
while(1){
if(num[now]==x){
cnt[now]++;
pushup(now),pushup(f);
Splay(now);
return;
}f=now;
if(x>num[now]){
now=son[now][1];
}else{
now=son[now][0];
}if(now==0){
siz++;
son[siz][0]=son[siz][1]=0;
num[siz]=x;
cnt[siz]=1,sz[siz]=1;
fa[siz]=f;
if(x>num[now]){
son[f][1]=siz;
}else{
son[f][0]=siz;
}pushup(f);
Splay(siz);
return;
}
}
}int find_key(int x){
int ret=0,now=rt;
while(1){
if(x<num[now]){
now=son[now][0];
}else{
if(son[now][0]>=1){
ret+=sz[son[now][0]];
}if(x==num[now]){
Splay(now);
return ret+1;
}ret+=cnt[now];
now=son[now][1];
}
}
}int find_th(int x){
int now=rt;
while(1){
if(son[now][0]>=1&&x<=sz[son[now][0]]){
now=son[now][0];
}else{
int tmp=cnt[now];
if(son[now][0]>=1){
tmp+=sz[son[now][0]];
}if(now<=tmp){
return num[now];
}x-=tmp;
now=son[now][1];
}
}
}int pre(){
int now=son[rt][0];
while(son[now][1]>=1){
now=son[now][1];
}return now;
}int nxt(){
int now=son[rt][1];
while(son[now][0]>=1){
now=son[now][0];
}return now;
}void del(int x){
find_key(x);
if(cnt[rt]>1){
cnt[rt]--;
pushup(rt);
return;
}else if(son[rt][0]==son[rt][1]==0){
all_zero(rt);
rt=0,siz=0;
return;
}else if(son[rt][0]==0){
int tmp=rt;
rt=son[rt][1];
fa[rt]=0;
all_zero(tmp);
siz--;
return;
}else if(son[rt][1]==0){
int tmp=rt;
rt=son[rt][0];
fa[rt]=0;
all_zero(tmp);
siz--;
return;
}int rt_pre=pre(),tmp=rt;
Splay(rt_pre);
fa[son[tmp][1]]=rt,son[rt][1]=son[tmp][1];
all_zero(tmp);
pushup(rt);
return;
}int main(){
scanf("%d",&n);
while(n--){
scanf("%d%d",&op,&x);
if(op==1){
insert(x);
}else if(op==2){
del(x);
}else if(op==3){
printf("%d\n",find_key(x));
}else if(op==4){
printf("%d\n",find_th(x));
}else if(op==5){
insert(x);
printf("%d\n",num[pre()]);
del(x);
}else if(op==6){
insert(x);
printf("%d\n",num[nxt()]);
del(x);
}
}return 0;
}//Splay and play S \cf