RT, 洛谷评测时RE有可能被判断成MLE (我就是这样)
#include<iostream>
using namespace std;
struct node
{
int v,l,r,sz,cnt;
int pr;
}t[1000010];
int tot=0,rt=0;
int kth(int o,int k)
{
if(!o) return 0;
if(k<=t[o].cnt+t[t[o].l].sz&&k>t[t[o].l].sz) return t[o].v;
if(k<=t[t[o].l].sz) return kth(t[o].l,k);
else return kth(t[o].r,k-t[t[o].l].sz-t[o].cnt);
}
int atot=0;
void update(int o)
{
t[o].sz=t[t[o].l].sz+t[o].cnt+t[t[o].r].sz;
}
void Rt(int &f)
{
if(f==0||t[f].l==0) return ;
int L=t[f].l;
t[f].l=t[L].r;
update(f);
t[L].r=f;
f=L;
}
void Lt(int &f)
{
if(f==0||t[f].r==0) return ;
int R=t[f].r;
t[f].r=t[R].l;
update(f);
t[R].l=f;
f=R;
return ;
}
void Ins(int &o,int x)
{
if(!o)
{
int k=rand();
o=++tot;
atot++;
t[o].v=x;
t[o].cnt=t[o].sz=1;
t[o].l=t[o].r=0;
t[o].pr=k;
return ;
}
if(t[o].v==x) {t[o].cnt++;t[o].sz++;atot++;return ;}
t[o].sz++;
if(t[o].v>x)
{
Ins(t[o].l,x);
if(t[t[o].l].pr<t[o].pr) Rt(o);
}
if(t[o].v<x)
{
Ins(t[o].r,x);
if(t[t[o].r].pr<t[o].pr) Lt(o);
}
update(o);
}
int qrnk(int o,int v)
{
if(o==0) return 0;
if(t[o].v==v) return t[t[o].l].sz+1;
if(t[o].v>v) return qrnk(t[o].l,v);
if(t[o].v<v) return qrnk(t[o].r,v)+t[t[o].l].sz+t[o].cnt;
return 0;
}
int delmn(int &o)
{
if(!t[o].l)
{
int u=o;
o=t[o].r;
return u;
}
else
{
int u=delmn(t[o].l);
t[o].sz-=t[u].cnt;
return u;
}
}
void del(int &o,int v)
{
t[o].sz--;
if(t[o].v==v)
{
if(t[o].cnt>1) {t[o].cnt--;return ;}
if(t[o].l&&t[o].r) o=delmn(t[o].r);
else o=t[o].l+t[o].r;
return;
}
if(t[o].v>v)
{
del(t[o].l,v);
if(t[t[o].l].pr<t[o].pr) Rt(o);
}
if(t[o].v<v)
{
del(t[o].r,v);
if(t[t[o].r].pr<t[o].pr) Lt(o);
}
}
int N(int o,int v,int ans)
{
if(t[o].v<=v)
{
if(!t[o].r) return ans;
return N(t[o].r,v,ans);
}
else
{
if(!t[o].l) return o;
return N(t[o].l,v,o);
}
}
int P(int o,int v,int ans)
{
if(t[o].v>=v)
{
if(!t[o].l) return ans;
return P(t[o].l,v,ans);
}
else
{
if(!t[o].r) return o;
return P(t[o].r,v,o);
}
}
int hk;
int print(int o)
{
if(!o) return 0;
print(t[o].l);
cout<<o<<' '<<t[o].sz<<' '<<t[o].cnt<<' '<<t[o].l<<' '<<t[o].r<<' '<<t[o].v<<endl;
print(t[o].r);
return 0;
}
int main()
{
srand(114514);
freopen("P3369_4.in","r",stdin);
freopen("Treap.out","w",stdout);
int n;
cin>>n;
while(n--)
{
int op;
int v;
cin>>op>>v;
if(op==1) {Ins(rt,v);}
if(op==3)
{
cout<<qrnk(rt,v)<<'\n';
}
if(op==4)
{
if(v>=1&&v<=atot) {cout<<kth(rt,v)<<'\n';}
else cout<<-1<<'\n';
}
if(op==6)
{
int x=N(rt,v,2146666666);
if(x==2146666666) cout<<"No"<<endl;
else cout<<t[x].v<<endl;
}
if(op==5)
{
int x=P(rt,v,-2146666666);
if(x==-2146666666) cout<<"No"<<endl;
else cout<<t[x].v<<endl;
}
if(op==2)
{
if(qrnk(rt,v)==-1) cout<<' '<<endl;
else del(rt,v);
}
// print(rt);
}
return 0;
}