首页 > 代码库 > 普通平衡树

普通平衡树

来自某二中大佬。

  1 #include<cstdio>
  2 #include<cstdlib>
  3 using namespace std;
  4  
  5 class Splay{
  6 public:
  7     Splay(){
  8         root=NULL;
  9         for(top;top<siez;top++)
 10             stk[top]=tree+top;
 11     }
 12     inline void insert(int val){
 13         if(find(val)!=NULL)
 14             ++root->cnt,update(root);
 15         else    if(root==NULL)
 16             root=newnode(val,NULL);
 17         else    splay(insert(root,val),NULL);
 18     }
 19     inline void erase(int val){
 20         if(find(val)!=NULL) erase(root,1);
 21     }
 22     inline int rnk(int val){
 23         if(find(val)!=NULL) return size(root->son[0])+1;
 24         else    return 0;
 25     }
 26     inline int qry(int kth){
 27         if(size(root)<kth)  return 0;
 28         for(node *t=root;t;){
 29             if(kth>size(t->son[0])){
 30                 kth-=size(t->son[0]);
 31                 if(kth<=t->cnt)
 32                     return t->v;
 33                 else    kth-=t->cnt,t=t->son[1];
 34             }
 35             else    t=t->son[0];
 36         }
 37     }
 38     inline int prv(int val){
 39         int ret=-0x7fffffff;
 40         for(node *t=root;t;){
 41             if(t->v<val){
 42                 if(ret<t->v)
 43                     ret=t->v;
 44             }
 45             t=t->son[val>t->v];
 46         }
 47         return ret;
 48     }
 49     inline int nxt(int val){
 50         int ret=0x7fffffff;
 51         for(node *t=root;t;){
 52             if(t->v>val){
 53                 if(ret>t->v)
 54                     ret=t->v;
 55             }
 56             t=t->son[val>=t->v];
 57         }
 58         return ret;
 59     }
 60 private:
 61     struct node{
 62         int v,cnt,siz;
 63         node *son[2],*f;
 64         node(){son[0]=son[1]=f=NULL;v=cnt=siz=0;}
 65     }*root;
 66     const static int siez=100100;
 67     node *stk[siez],tree[siez];int top;
 68     inline node * newnode(int v,node *f){
 69         node *t=stk[--top];
 70         t->siz=t->cnt=1;t->v=v;
 71         t->son[1]=t->son[0]=NULL;
 72         t->f=f;
 73         return t;
 74     }
 75     inline void freenode(node *t){
 76         stk[top++]=t;
 77     }
 78     inline int size(node* t){
 79         return t==NULL?0:t->siz;
 80     }
 81     inline void update(node *t){
 82         if(t!=NULL)
 83             t->siz=t->cnt+size(t->son[0])+size(t->son[1]);
 84     }
 85     
 86     inline bool son(node* f,node* s){
 87         if(f==NULL) return 0;
 88         return f->son[1]==s;
 89     }
 90     inline void connect(node *f,node *s,bool k){
 91         if(f!=NULL) f->son[k]=s;
 92         else    root=s;
 93         if(s!=NULL) s->f=f;
 94     }
 95     inline void rotate(node* t){
 96         node *f=t->f,*g=f->f;
 97         bool a=son(f,t),b=!a;
 98         connect(f,t->son[b],a);
 99         connect(g,t,son(g,f));
100         connect(t,f,b);
101         update(f);
102         update(t);
103     }
104     inline  void splay(node *t,node *p){
105         if(t){
106             while(t->f!=p){
107                 node *f =t->f,*g=f->f;
108                 if(g==p)    rotate(t);
109                 else{
110                     if(son(g,f)^son(f,t))
111                         rotate(t),rotate(t);
112                     else
113                         rotate(f),rotate(t);
114                 }
115             }
116         }
117     }
118     inline node *find(int val){
119         node *t=root;
120         while(t!=NULL &&t->v!=val){
121             t=t->son[val>=t->v];
122         }
123         return splay(t,NULL ),t;
124     }
125     node *insert(node *t,int val){
126         node *ret=t->son[val>=t->v];
127         if(ret==NULL)
128             ret=t->son[val>=t->v]=newnode(val,t);
129         else ret=insert(ret,val);
130         return update(t),ret;
131     }
132     inline void erase(node *t){
133         if(t->son[0]==NULL)
134             connect(NULL ,t->son[1],0),update(root);
135         else    if(t->son[1]==NULL){
136             connect(NULL,t->son[0],1),update(root);
137         }
138         else{
139             node *p=t->son[0];
140             while(p->son[1]!=NULL)  p=p->son[1];
141             splay(p,t);
142             connect(NULL,p,0);
143             connect(p,t->son[1],1);
144             update(root);
145         }
146         freenode(t);
147     }
148     inline void erase(node *t ,int k){
149         t->cnt-=k;
150         if(t->cnt<=0)   erase(t);
151         else    update(t);
152     }
153 }s;
154 int main(){
155     freopen("phs.in","r",stdin);
156     freopen("phs.out","w",stdout);
157     int n,op,x;
158     scanf("%d",&n);
159     while(n--){
160         scanf("%d%d",&op,&x);
161         switch(op){
162             case 1: s.insert(x);
163             break;
164             case 2 :s.erase(x);
165             break;
166             case 3: printf("%d\n",s.rnk(x));
167             break;
168             case 4:printf("%d\n",s.qry(x));
169             break;
170             case 5:printf("%d\n",s.prv(x));
171             break;
172             default:printf("%d\n",s.nxt(x));
173             break;
174         }
175     }
176 }

 

普通平衡树