Title: https://www.luogu.org/problemnew/show/P3369
Treap template.
code show as below:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int const MAXN=100005,inf=1e9; int n,rt,cnt; struct N{ int ch [3], val, siz, pri, num; }t[MAXN]; int newnode() { int r=++cnt; t[r].siz=1;t[r].num=1;t[r].pri=rand(); return r; } void pushup(int &o) { t [o] .siz = t [t [o] .ch [0]]. siz + t [t [o] .ch [1]]. siz + t [o] .num; } void rotate(int &o,int d) { int u=t[o].ch[d]; t[o].ch[d]=t[u].ch[d^1]; t[u].ch[d^1]=o; t [u] .you = t [o] .you; pushup(o); o=u; } void add(int &o,int x) { if(!o) { o=newnode(); t[o].val=x; return; } if(t[o].val==x) { t [o] .num ++; t [o] .you ++; return; } int d=t[o].val<x; add(t[o].ch[d],x); pushup(o); if(t[t[o].ch[d]].pri<t[o].pri)rotate(o,d); } void del(int &o,int x) { if(!o)return; if(t[o].val==x) { if(t[o].num>1)t[o].num--; else if(t[o].ch[1]*t[o].ch[0]==0)// o=t[o].ch[1]+t[o].ch[0]; else { int d=t[t[o].ch[0]].pri<t[t[o].ch[1]].pri; rotate(o,d^1); del (o, x); // o! } pushup(o); return; } int d=t[o].val<x; del (t [o] .ch [d], x); pushup(o); } int query(int &o,int x) { if(!o)return 0; if(t[o].val==x)return t[t[o].ch[0]].siz+1; int d=t[o].val<x; if(!d)return query(t[o].ch[d],x); return query(t[o].ch[d],x)+t[o].num+t[t[o].ch[0]].siz; } int qr(int &o,int x) { if(!o||!x)return 0; int d = xt [t [o] .ch [0]]. you; if(d<=0)return qr(t[o].ch[0],x); if(d<=t[o].num)return t[o].val; return qr(t[o].ch[1],x-t[t[o].ch[0]].siz-t[o].num); } int pre(int &o,int x) { if(!o)return -inf;// if(t[o].val<x)return max(t[o].val,pre(t[o].ch[1],x)); return pre(t[o].ch[0],x); } int lst(int &o,int x) { if(!o)return inf;// if(t[o].val>x)return min(t[o].val,lst(t[o].ch[0],x)); return lst(t[o].ch[1],x); } intmain() { scanf("%d",&n); for(int i=1,x,opt;i<=n;i++) { scanf("%d%d",&opt,&x); if(opt==1)add(rt,x);//Insert x number if(opt==2)del(rt,x);//Delete x number (if there are multiple identical numbers, only delete one) if(opt==3)printf("%d\n",query(rt,x)); //Query the ranking of the number of x (the ranking is defined as the number of numbers smaller than the current number + 1, if there are multiple identical numbers, output the smallest ranking) if(opt==4)printf("%d\n",qr(rt,x));//Query the number of rank x if(opt==5)printf("%d\n",pre(rt,x));//Seek the predecessor of x (the predecessor is defined as the largest number less than x) if(opt==6)printf("%d\n",lst(rt,x));//Seek the successor of x (the successor is defined as the smallest number greater than x) } return 0; }