题目链接:http://www.spoj.com/problems/ORDERSET/

题意:给你四个操作,将x插入节点,将值为x的节点删除,找第K大的节点,找值为X的节点是第几大

解题思路:treap树性质,copy了 clj 在 nocow 上的模板,利用 指针引用,孩子数组来简化代码

解题代码:

  1 // File Name: treap.cpp
  2 // Author: darkdream
  3 // Created Time: 2014年07月22日 星期二 09时06分23秒
  4 
  5 #include<vector>
  6 #include<list>
  7 #include<map>
  8 #include<set>
  9 #include<deque>
 10 #include<stack>
 11 #include<bitset>
 12 #include<algorithm>
 13 #include<functional>
 14 #include<numeric>
 15 #include<utility>
 16 #include<sstream>
 17 #include<iostream>
 18 #include<iomanip>
 19 #include<cstdio>
 20 #include<cmath>
 21 #include<cstdlib>
 22 #include<cstring>
 23 #include<ctime>
 24 using namespace std;
 25 
 26 const int inf = ~0U>>1;
 27 class treap
 28 {
 29    struct node{
 30      int value , key ,size;
 31      node(int v, node *n):value(v)
 32      {c[0] = c[1]= n; size = 1; key = rand()-1;}
 33      void rz(){size =c[0]->size + c[1]->size+1;}
 34      node*c[2];
 35    }*root,*null;
 36    void rot(node *&t,bool d)
 37    {
 38        node *c = t->c[d];
 39        t->c[d] = c->c[!d];
 40        c->c[!d] = t; 
 41        t->rz();c->rz();
 42        t = c;
 43    }
 44    void insert(node *&t,int x)
 45    {
 46        if(t == null)
 47        {
 48          t = new node(x,null);
 49          return ; 
 50        }
 51        if(x == t->value) return;
 52        bool d = x > t->value;
 53        insert(t->c[d],x);
 54        if(t->c[d]->key < t->key) //把小的随机值移动到上面
 55           rot(t,d);
 56        else t->rz();
 57    }
 58    void Delete(node *&t,int x)
 59    {
 60       if(t == null) return;
 61       if(t->value == x)  //將这个值一直旋转到叶子节点然后删除
 62       {
 63           bool d = t->c[1]->key < t->c[0]->key;
 64           if(t->c[d] == null)
 65           {
 66               delete t;
 67               t = null;
 68               return ;
 69           }
 70           rot(t,d);
 71           Delete(t->c[!d],x);
 72       }
 73       else{
 74         bool d = x>t->value;
 75         Delete(t->c[d],x);
 76       }
 77       t->rz();
 78    }
 79    int select(node *t ,int k )
 80    {
 81       int r = t->c[0]->size;
 82       if(k ==  r)
 83          return t->value;
 84       if(k < r) return select(t->c[0],k);
 85       return select(t->c[1], k - r - 1);
 86    }
 87    int rank(node *t , int x)
 88    {
 89        if(t == null) return 0 ; 
 90        int r = t->c[0]->size;
 91        if(x == t->value)
 92            return r;
 93        if(x < t->value)  return rank(t->c[0],x);
 94        return r+1+rank(t->c[1],x);
 95    }
 96    public:
 97       treap()
 98       {
 99         null = new node(0,0);
100         null ->size = 0 ;
101         null ->key = inf;
102         root = null;
103       }
104       void ins(int x)
105       {
106         insert(root,x);
107       }
108       int sel(int k)
109       {
110         if(k > root->size) return -inf;
111         return select(root,k-1);
112       }
113       int ran(int x)
114       {
115         return rank(root,x);
116       }
117       void del(int x)
118       {
119          Delete(root,x);
120       }
121 }T;
122 int main(){
123     //printf("%d\n",inf);   
124       //freopen("in","r",stdin);
125     int m;scanf("%d\n",&m);    
126     char t;int x,tmp;
127     while(m--)
128     {
129         scanf("%c %d\n",&t,&x);
130         switch(t)
131         {
132             case 'I':T.ins(x);break;
133             case 'D':T.del(x);break;
134             case 'K':tmp=T.sel(x);if(tmp==-inf)printf("invalid\n");else printf("%d\n",tmp);break;
135             case 'C':printf("%d\n",T.ran(x));break;
136         }        
137     }
138 return 0;
139 }
View Code
posted on 2014-07-22 10:29  dark_dream  阅读(244)  评论(0编辑  收藏  举报