名次树treap代码

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<algorithm>
  4 #include<cstring>
  5 #include<vector>
  6 using namespace std;
  7 typedef long long LL;
  8 const int MAXN=200000+10; 
  9 const int INF=0x7fffffff;
 10 struct NODE{
 11     NODE *ch[2];
 12     int r,v;
 13     int s;
 14     NODE(int v=0):v(v){r=rand();s=1;ch[1]=ch[0]=NULL;}
 15     bool operator < (const NODE &rhs) const {
 16         return r<rhs.r;
 17     }
 18     int cmp(int x){
 19         if(x == v) return -1;
 20         return x < v ? 0 : 1;
 21     }
 22     void mt(){
 23         s=1;
 24         if(ch[0] != NULL) s+=ch[0]->s;
 25         if(ch[1] != NULL) s+=ch[1]->s;
 26     }
 27 };
 28 void rotate(NODE *&o,int d){
 29     NODE *k=o->ch[d^1];o->ch[d^1]=k->ch[d];k->ch[d]=o;
 30     o->mt();k->mt();o=k;
 31 }
 32 void insert(NODE *&o,int x){
 33     if(o == NULL) o=new NODE(x);
 34     else {
 35         int d=o->v > x ? 0 : 1;
 36         insert(o->ch[d],x);
 37         if(o->ch[d]->r > o->r) rotate(o,d^1);
 38     }
 39     o->mt();
 40 }
 41 
 42 void remove(NODE *&o,int x){
 43     int d=o->cmp(x);
 44     int ret=0;
 45     if(d == -1){
 46         NODE *u=o;
 47         if(o->ch[1] != NULL && o->ch[0] != NULL){
 48             int d2=(o->ch[0]->r > o->ch[1]->r ? 1 : 0);
 49             rotate(o,d2);remove(o->ch[d2],x);
 50         }
 51         else {
 52             if(o->ch[0] == NULL) o=o->ch[1];
 53             else o=o->ch[0];
 54             delete u;
 55         }
 56     }
 57     else {
 58         remove(o->ch[d],x);
 59     }
 60     if(o != NULL) o->mt();
 61 }
 62 
 63 NODE *find(NODE *o,int x){
 64     if(o == NULL) return NULL;
 65     if(x == o->v) return o;
 66     return x < o->v ? find(o->ch[0],x) : find(o->ch[1],x); 
 67 }
 68 
 69 int kth(NODE *o,int k){
 70     if(k <= 0 || o == NULL || k > o->s) return 0;
 71     int s=(o->ch[0] == NULL ? 0 : o->ch[0]->s);
 72     if(k == s+1) return o->v;
 73     else if(k <= s) return kth(o->ch[0],k);
 74     else return kth(o->ch[1],k-s-1);
 75 }
 76 
 77 int rank(NODE *o,int x){
 78     if(o == NULL) return 1;
 79     if(x <= o->v) return rank(o->ch[0],x);
 80     return rank(o->ch[1],x)+(o->ch[0] == NULL ? 0:o->ch[0]->s)+1;
 81 }
 82 
 83 int prec(NODE *o,int t){//小于t的最大数 
 84     int ans=-INF;
 85     if(t > o->v){
 86         if(o->ch[1] != NULL) ans=prec(o->ch[1],t);
 87         return max(ans,o->v);
 88     }
 89     else if(o->ch[0] != NULL) ans=prec(o->ch[0],t);
 90     return ans;
 91 }
 92 
 93 int succ(NODE *o,int t){//大于t的最小数 
 94     int ans=INF;
 95      if(t < o->v){
 96          if(o->ch[0] != NULL) ans=succ(o->ch[0],t);
 97          return min(ans,o->v);
 98      }
 99      else if(o->ch[1] != NULL) ans=succ(o->ch[1],t);
100      return ans;
101 }
102 
103 #define submit
104 int main(){
105 #ifdef submit
106     freopen("phs.in","r",stdin);
107     freopen("phs.out","w",stdout);
108 #endif
109 #ifdef submi
110     freopen("in.txt","r",stdin);
111 #endif
112     int m,c,v;
113     NODE *root=new NODE(INF);
114     root->r=0x7fffffff;
115     insert(root,INF);
116     scanf("%d",&m);
117     while(m--){
118         scanf("%d %d",&c,&v);
119         if(c == 1) insert(root,v);
120         else if(c == 2){
121             NODE *o=find(root,v);
122             if(o != NULL) remove(root,v);
123         }
124         else if(c == 4) printf("%d\n",kth(root,v));
125         else if(c == 3) printf("%d\n",rank(root,v));
126         else if(c == 5) printf("%d\n",prec(root,v));
127         else if(c == 6) printf("%d\n",succ(root,v));
128     }
129     return(0);
130 }

 

posted on 2017-02-27 16:59  KCkowk  阅读(115)  评论(0编辑  收藏  举报