【题意】
1. 插入x数
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)
【算法】
重要的细节以注释的形式标注在代码中。
#include#include #include using namespace std;const int maxn=100010;struct tree{ int l,r,sz,rnd,num;}t[maxn*2];int n,sz,root;void up(int k){t[k].sz=1+t[t[k].l].sz+t[t[k].r].sz;}void lturn(int &k){ int o=t[k].r; t[k].r=t[o].l; t[o].l=k; up(k);up(o); k=o;}void rturn(int &k){ int o=t[k].l; t[k].l=t[o].r; t[o].r=k; up(k);up(o); k=o;}void ins(int &k,int x){ if(!k){k=++sz;t[k].rnd=rand();t[k].num=x;t[k].sz=1;return;}//return t[k].sz++; if(x<=t[k].num){ ins(t[k].l,x); if(t[t[k].l].rnd x)return min(t[k].num,suc(t[k].l,x)); else return suc(t[k].r,x);}int main(){ srand(233);//srand!!!!!!!!!! scanf("%d",&n);sz=root=0; for(int i=1;i<=n;i++){ int opt,x; scanf("%d%d",&opt,&x); if(opt==1)ins(root,x); if(opt==2)del(root,x); if(opt==3)printf("%d\n",find(root,x)+1);//+1 if(opt==4)printf("%d\n",rank(root,x)); if(opt==5)printf("%d\n",pre(root,x)); if(opt==6)printf("%d\n",suc(root,x)); } return 0;}