平衡树模板(splay,sbt,treap)

平衡树...反正我只会sbt和treap,只会写treap

noip完了之后再训练了,现在先把板搬过来,以及splay版由zjh提供

 

sbt:

  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<cstdlib>
  4 #define N 100005
  5 using namespace std;
  6 int n,m;
  7 int count=0; 
  8 int root=0;
  9 
 10 struct node
 11 {
 12        int weight;
 13        int size,l,r;
 14 }sbt[N];
 15 
 16 void lrotate(int &x)   //左旋 
 17 { 
 18        int y=sbt[x].r;
 19        sbt[x].r=sbt[y].l;
 20        sbt[y].l=x;
 21        sbt[y].size=sbt[x].size;
 22        sbt[x].size=sbt[sbt[x].l].size+sbt[sbt[x].r].size+1;
 23        x=y;
 24 }
 25 
 26 void rrotate(int &x)   //右旋
 27 {
 28        int y=sbt[x].l;
 29        sbt[x].l=sbt[y].r;
 30        sbt[y].r=x;
 31        sbt[y].size=sbt[x].size;
 32        sbt[x].size=sbt[sbt[x].l].size+sbt[sbt[x].r].size+1;
 33        x=y;
 34 }
 35 
 36 void maintain(int &root,bool flag)   //维护 
 37 {
 38        if(flag==false)
 39        {
 40            if(sbt[sbt[sbt[root].l].l].size>sbt[sbt[root].r].size)
 41          rrotate(root);
 42            else
 43         {
 44             if(sbt[sbt[sbt[root].l].r].size>sbt[sbt[root].r].size)
 45                {
 46                    lrotate(sbt[root].l);
 47                    rrotate(root);
 48                }
 49                else return;
 50            }
 51        }
 52        else
 53        {
 54            if(sbt[sbt[sbt[root].r].r].size>sbt[sbt[root].l].size)
 55          lrotate(root);
 56            else
 57         {
 58                if(sbt[sbt[sbt[root].r].l].size>sbt[sbt[root].l].size)
 59                {
 60                    rrotate(sbt[root].r);
 61                    lrotate(root);
 62                }
 63                else return;
 64            }
 65        }
 66        maintain(sbt[root].l,false);
 67        maintain(sbt[root].r,true);
 68        maintain(root,true);
 69        maintain(root,false);
 70 }
 71 
 72 void insert(int &root,int x)   //插入 
 73 {
 74        if(root==0)
 75        {
 76        sbt[root=++count].weight=x;
 77        sbt[root].size=1;
 78        }
 79        else
 80        {
 81            sbt[root].size++;
 82            if(x<sbt[root].weight)
 83             insert(sbt[root].l,x);
 84          else insert(sbt[root].r,x);
 85            maintain(root,x>=sbt[root].weight);
 86        }
 87 }
 88 
 89 int del(int &x,int v)   //删除 
 90 {
 91     sbt[x].size--;
 92     if((v==sbt[x].weight)||(v<sbt[x].weight&&sbt[x].l==0)||(v>sbt[x].weight&&sbt[x].r==0))
 93     {
 94         int r=sbt[x].weight;
 95         if(sbt[x].l==0||sbt[x].r==0)
 96          x=sbt[x].l+sbt[x].r;
 97         else
 98          sbt[x].weight=del(sbt[x].l,sbt[x].weight+1);
 99         return r;
100     }
101     else
102     {
103         if(v<sbt[x].weight)
104          return del(sbt[x].l,v);
105         else
106          return del(sbt[x].r,v);
107     }
108 }
109 
110 int Successor(int x,int r)   //后驱 
111 {
112     if(x==0)return r;
113     if(sbt[x].weight<=r)
114      return Successor(sbt[x].r,r);
115     else
116     {
117         int k=Successor(sbt[x].l,r);
118         if(r==k)return sbt[x].weight;
119         else return k;
120     }
121 }
122 
123 int Predecessor(int x,int r)   //前驱 
124 {
125     if(x==0)return r;
126     if(sbt[x].weight>=r)
127      return Predecessor(sbt[x].l,r);
128     else
129     {
130         int k=Predecessor(sbt[x].r,r);
131         if(r==k)return sbt[x].weight;
132         else return k;
133     }
134 }
135 
136 int select(int now,int x)   //查找排名为x的数 
137 {
138        int r=sbt[sbt[now].l].size+1;
139        if(x<r)
140      return select(sbt[now].l,x);
141        if(x>r)
142       return select(sbt[now].r,x-r);
143        if(x==r)return sbt[now].weight;
144 }
145 
146 int rank(int x,int k)   //查找数x的排名
147 {
148     if(x==0)
149      return 1;
150     if(sbt[x].weight>=k)
151      return rank(sbt[x].l,k);
152     else
153      return sbt[sbt[x].l].size+rank(sbt[x].r,k)+1;
154 }
155 
156 void DFS(int now)
157 {
158     if(sbt[now].l!=0)
159      DFS(sbt[now].l);
160     cout<<sbt[now].weight<<" ";
161     if(sbt[now].r!=0)
162      DFS(sbt[now].r);
163 }
164 
165 int main()
166 {
167     char a;
168     int b;
169     scanf("%d%d",&n,&m);
170     for(int i=0;i<n;i++)
171     {
172         scanf("%d%d",&a,&b);
173         if(a==1)insert(root,b);
174         else if(a==2)del(root,b);
175         else cout<<select(root,b)<<endl;
176     }
177     DFS(root);
178     return 0;
179 }

 

treap:

  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<cstdlib>
  4 #include<string.h>
  5 #define N 500005 
  6 using namespace std;
  7 int count=1,root=0;
  8 int n;
  9 int tot;
 10 
 11 struct treap
 12 {
 13     int key,size,pri,son[2];   //左儿子右儿子 
 14     void set(int x,int y)
 15     {
 16         key=x;
 17         pri=y;
 18         size=1;
 19         son[0]=son[1]=0;
 20     }
 21 }tree[N];
 22 
 23 void rotate(int p,int &x)  //p=0左旋,p=1右旋 
 24 {
 25     int y=tree[x].son[!p]; 
 26     tree[x].size=tree[x].size-tree[y].size+tree[tree[y].son[p]].size;
 27     tree[x].son[!p]=tree[y].son[p];
 28     tree[y].size=tree[y].size-tree[tree[y].son[p]].size+tree[x].size;
 29     tree[y].son[p]=x;
 30     x=y;
 31 }
 32 
 33 void insert(int key,int &x)
 34 {
 35     if(x==0)
 36      tree[x=count++].set(key,rand());
 37     else
 38     {
 39         tree[x].size++;
 40         int p=key<tree[x].key;
 41         insert(key,tree[x].son[!p]);
 42         if(tree[x].pri<tree[tree[x].son[!p]].pri)
 43          rotate(p,x);
 44     }
 45 }
 46 
 47 void del(int key,int &x)
 48 {
 49     if(tree[x].key==key)
 50     {
 51         if(tree[x].son[0]&&tree[x].son[1])
 52         {
 53             int p=tree[tree[x].son[0]].pri>tree[tree[x].son[1]].pri;
 54             rotate(p, x);
 55             tree[x].size--;
 56             del(key,tree[x].son[p]);
 57         }
 58         else
 59         {
 60             tree[x].size--;
 61             if(!tree[x].son[0])
 62                 x=tree[x].son[1];
 63             else
 64                 x=tree[x].son[0];
 65         }
 66     }
 67     else
 68     {
 69         tree[x].size--;
 70         int p=tree[x].key>key;
 71         del(key,tree[x].son[!p]);
 72     }
 73 }
 74 
 75 int find(int p,int &x)
 76 {
 77     if(p==tree[tree[x].son[0]].size+1)
 78      return x;
 79     if(p>tree[tree[x].son[0]].size+1)
 80      find(p-tree[tree[x].son[0]].size-1,tree[x].son[1]);
 81     else
 82      find(p,tree[x].son[0]);
 83 }
 84 
 85 int rank(int key,int &x)
 86 {
 87     if(x==0)
 88      return 0;
 89     if(key>=tree[x].key)
 90      return tree[tree[x].son[0]].size+1+rank(key,tree[x].son[1]);
 91     else return rank(key,tree[x].son[0]);
 92 }
 93 
 94 int pre(int key,int &x)
 95 {
 96     if(x==0)
 97      return key;
 98     if(key<=tree[x].key)
 99      return pre(key,tree[x].son[0]);
100     else
101     {
102         int thi=pre(key,tree[x].son[1]);
103         if(thi==key)return tree[x].key;
104         return thi;
105     }
106 }
107 
108 int suc(int key,int &x)
109 {
110     if(x==0)
111      return key;
112     if(key>=tree[x].key)
113      return suc(key,tree[x].son[1]);
114     else
115     {
116         int thi=suc(key,tree[x].son[0]);
117         if(thi==key)return tree[x].key;
118         return thi;
119     }
120 }
121 
122 int main()
123 {
124     int a,b;
125     scanf("%d",&n);
126     for(int i=0;i<n;i++)
127     {
128         scanf("%d%d",&a,&b);
129         if(a==1)
130          insert(b,root),tot++;
131         if(a==2)
132          del(b,root),tot--;
133         if(a==3)
134          printf("%d\n",rank(b,root));
135         if(a==4)
136          printf("%d\n",tree[find(b,root)].key);
137         if(a==5)
138          printf("%d\n",pre(b,root));
139         if(a==6)
140          printf("%d\n",suc(b,root));
141     }
142     return 0;
143 }

 

splay:

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <cstdlib>
  4 #include <algorithm>
  5 #define MAXN 5000000
  6 
  7 struct Node
  8 {
  9     int key , size , ct;
 10     Node *ch[2], *pa;
 11     void gain(){size = ch[0] -> size + ch[1] -> size + ct;}
 12 }nul, *Null = &nul;
 13 
 14 struct Splay
 15 {
 16     int cnt;
 17     Node *root;
 18     Node mp[MAXN+5];
 19 
 20     void init(){root = Null ; cnt = 0; nul = (Node){0 , 0 , 0 , Null , Null , Null};}
 21     
 22     void Rotate(Node *x , int fl)
 23     {
 24         Node *pre = x -> pa;
 25         pre -> ch[!fl] = x -> ch[fl];
 26         if(x -> ch[fl] != Null)
 27             x -> ch[fl] -> pa = pre;
 28         x -> pa = pre -> pa;
 29         if(pre -> pa != Null)    
 30             if(pre -> pa -> ch[0] == pre)
 31                 pre -> pa -> ch[0] = x;
 32             else
 33                 pre -> pa -> ch[1] = x;
 34         x -> ch[fl] = pre ; 
 35         pre -> pa = x;
 36         pre -> gain();
 37         x -> gain();
 38     }
 39     
 40     void Maintain(Node *x , Node *now)
 41     {
 42         Node *pre;
 43         while(x -> pa != now)
 44         {
 45             pre = x -> pa;
 46             if(x == pre -> ch[0])
 47             {
 48                 if(pre -> pa != now && pre -> pa -> ch[0] == pre)
 49                     Rotate(pre , true);
 50                 Rotate(x , true);
 51             }else{
 52                 if(pre -> pa != now && pre == pre -> pa -> ch[1])
 53                     Rotate(pre , false);
 54                 Rotate(x , false);
 55             }
 56         }
 57         if(now == Null)
 58             root = x;
 59     }
 60 
 61     void Insert(int key)
 62     {
 63         if(root == Null)
 64         {
 65             cnt = 0;
 66             root = &mp[++cnt];
 67             root -> ch[0] = root -> ch[1] = root -> pa = Null;
 68             root -> key = key;
 69             root -> size = root -> ct = 1;
 70             return ;
 71         }
 72         Node *x = root , *suc = Null;
 73         while(true)
 74         {
 75             x -> size++;
 76             if(key == x -> key)
 77             {
 78                 x -> ct++;
 79                 x -> gain();
 80                 suc = x;
 81                 break;
 82             }else if(key < x -> key){
 83                 if(x -> ch[0] != Null)
 84                     x = x -> ch[0];
 85                 else{
 86                     x -> ch[0] = &mp[++cnt];
 87                     suc = x -> ch[0];
 88                     suc -> key = key;
 89                     suc -> size = suc -> ct = 1;
 90                     suc -> ch[0] = suc -> ch[1] = Null;
 91                     suc -> pa = x;
 92                     break;
 93                 }
 94             }else{
 95                 if(x -> ch[1] != Null)
 96                     x = x -> ch[1];
 97                 else{
 98                     x -> ch[1] = &mp[++cnt];
 99                     suc = x -> ch[1];
100                     suc -> key = key;
101                     suc -> size = suc -> ct = 1;
102                     suc -> ch[0] = suc -> ch[1] = Null;
103                     suc -> pa = x;
104                     break;
105                 }
106             }
107         }
108         Maintain(suc , Null);
109     }
110 
111     Node *Search(int key)
112     {
113         if(root == Null)
114             return Null;
115         Node *x = root , *suc = Null;
116         while(true)
117         {
118             if(key == x -> key)
119             {
120                 suc = x;
121                 break;
122             }else if(key > x -> key){
123                 if(x -> ch[1] != Null)
124                     x = x -> ch[1];
125                 else
126                     break;
127             }else{
128                 if(x -> ch[0] != Null)
129                     x = x -> ch[0];
130                 else
131                     break;
132             }
133         }
134         Maintain(x , Null);
135         return suc;
136     }
137 
138     Node *Min(Node *x)
139     {
140         Node *pre = x -> pa;
141         while(x -> ch[0] != Null)
142             x = x -> ch[0];
143         Maintain(x , pre);
144         return x;
145     }
146 
147     void Delete(int key)
148     {
149         if(root == Null)
150             return ;
151         Node *x = Search(key);
152         Node *suc = Null;
153         if(x == Null)
154             return ;
155         if(x -> ct > 1)
156         {
157             x -> ct--;
158             x -> gain();
159             return ;
160         }else if(x -> ch[0] == Null && x -> ch[1] == Null){
161             init();
162             return ;
163         }else if(x -> ch[0] == Null){
164             root = x -> ch[1];
165             x -> ch[1] -> pa = Null;
166             return ;
167         }else if(x -> ch[1] == Null){
168             root = x -> ch[0];
169             x -> ch[0] -> pa = Null;
170             return ;
171         }
172         suc = Min(x -> ch[1]);
173         suc -> pa = Null;
174         suc -> ch[0] = x -> ch[0];
175         suc -> ch[0] -> pa = suc;
176         suc -> gain();
177         root = suc;
178     }
179 
180     int Rank(int key)
181     {
182         Node *x = Search(key);
183         if(x == Null)
184             return 0;
185         return x -> ch[0] -> size + 1/* or x -> cnt*/;
186     }
187 
188     Node *Select(int key)
189     {
190         if(root == Null || key > root -> size)
191             return Null;
192         Node *x = root;
193         while(true)
194         {
195             if(x -> ch[0] -> size + 1 <= key && key <= x -> ch[0] -> size + x -> ct)
196                 break;
197             else if(key <= x -> ch[0] -> size)
198                 x = x -> ch[0];
199             else{
200                 key -= x -> ch[0] -> size + x -> ct;
201                 x = x -> ch[1];
202             }
203         }
204         Maintain(x , Null);
205         return x;
206     }
207 
208     int Successor(Node *x , int key)
209     {
210         if(x == Null)
211             return key;
212         if(x -> key <= key)
213             return Successor(x -> ch[1] , key);
214         int r = Successor(x -> ch[0] , key);
215         if(r == key)
216             return x -> key;
217         return r;
218     }
219 
220     int Predecessor(Node *x , int key)
221     {
222         if(x == Null)
223             return key;
224         if(x -> key >= key)
225             return Predecessor(x -> ch[0] , key);
226         int l = Predecessor(x -> ch[1] , key);
227         if(l == key)
228             return x -> key;
229         return l;
230     }
231         
232 }sp;
233 
234 #include <iostream>
235 
236 int main()
237 {
238     int n;
239     scanf("%d",&n);
240     sp.init();
241     for(int i=1;i<=n;i++)
242     {
243         int cmd,x;
244         scanf("%d%d",&cmd,&x);
245         if(cmd == 1)
246             sp.Insert(x);
247         else if(cmd==2)
248             sp.Delete(x);
249         else if(cmd==32)
250             printf("%d\n",sp.Search(x) -> key);
251         else if(cmd==3)
252             printf("%d\n",sp.Rank(x));
253         else if(cmd==4)
254             printf("%d\n",sp.Select(x) -> key);
255         else if(cmd==5)
256             printf("%d\n",sp.Predecessor(sp.root,x));
257         else if(cmd==6)
258             printf("%d\n",sp.Successor(sp.root,x));
259         else if(cmd==0)
260             for(int i=0;i<=sp.cnt;i++)
261                 printf("%d %d\n",sp.mp[i].key,sp.mp[i].size);
262     }
263     return 0;
264 }

 

posted @ 2016-11-16 21:12  zyx_45889  阅读(237)  评论(0编辑  收藏  举报