洛谷P3369 【模板】普通平衡树 01trie/骚操作

Code:

#include <cstdio>
#include <algorithm>
#include <cstring>
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 100010 * 33 
using namespace std;
int root=1,tot=1,sumv[maxn],n,opt,x,ch[maxn][2]; 
void ins(int val,int c){
    val += (int)1e7;
    for(int i=31,p=root,t;i>=0;--i){
        t=(val>>i)&1;
        if(!ch[p][t]) ch[p][t]=++tot;
        p=ch[p][t]; sumv[p]+=c; 
    }
}
int rank(int val){
    val += (int)1e7; 
    int res=0,p=root; 
    for(int i=31;i>=0;--i){
        int t=(val>>i)&1;
        if(t) res += sumv[ch[p][0]];
        p=ch[p][t]; 
    }
    return res; 
}
int kth(int val){
    int k=root,res=0;
    for(int i=31;i>=0;--i){
        if(val>sumv[ch[k][0]]) res|=(1<<i),val-=sumv[ch[k][0]],k=ch[k][1];
        else k=ch[k][0]; 
    } 
    res-=(1e7); return res; 
}
int main(){
    //setIO("input");
    scanf("%d",&n);
    while(n--){
        scanf("%d%d",&opt,&x);
        if(opt==1) ins(x,1);
        else if(opt==2) ins(x,-1);
        else if(opt==3) printf("%d\n",rank(x)+1);
        else if(opt==4) printf("%d\n",kth(x));
        else if(opt==5) printf("%d\n",kth(rank(x)));
        else if(opt==6) printf("%d\n",kth(rank(x+1)+1)); 
    }
    return 0; 
}

  

posted @ 2019-02-13 15:23  EM-LGH  阅读(282)  评论(0编辑  收藏  举报