码迷,mamicode.com
首页 > 其他好文 > 详细

SBT模版

时间:2014-11-23 20:08:17      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   sp   for   on   div   log   

  1 /*Author:WNJXYK*/
  2 #include<cstdio>
  3 using namespace std;
  4 
  5 const int Maxn=10000;
  6 struct SBT{
  7     int left;
  8     int right;
  9     int size;
 10     int key;
 11 }; 
 12 SBT tree[Maxn+10];
 13 int root,cnt;
 14 
 15 inline void rotate_l(int &x){
 16     int y=tree[x].right;
 17     tree[x].right=tree[y].left;
 18     tree[y].left=x;
 19     tree[y].size=tree[x].size;
 20     tree[x].size=1+tree[tree[x].left].size+tree[tree[x].right].size;
 21     x=y;
 22 }
 23 
 24 inline void rotate_r(int &x){
 25     int y=tree[x].left;
 26     tree[x].left=tree[y].right;
 27     tree[y].right=x;
 28     tree[y].size=tree[x].size;
 29     tree[x].size=tree[tree[x].left].size+1+tree[tree[x].right].size;
 30     x=y; 
 31 }
 32 
 33 void maintain(int &x,bool flag){
 34     //printf("MainTain %d\n",x);
 35     if (flag==false){
 36         if (tree[tree[tree[x].left].left].size>tree[tree[x].right].size){
 37             rotate_r(x);
 38         }
 39         else if (tree[tree[tree[x].left].right].size>tree[tree[x].right].size){
 40             rotate_l(tree[x].left);
 41             rotate_r(x);
 42         }else return ;
 43     }else{
 44         if (tree[tree[tree[x].right].right].size>tree[tree[x].left].size){
 45             rotate_l(x);
 46         }else if (tree[tree[tree[x].right].left].size>tree[tree[x].left].size){
 47             rotate_r(tree[x].right);
 48             rotate_l(x);
 49         }else return ;
 50     }
 51     maintain(tree[x].left,false);
 52     maintain(tree[x].right,true);
 53     maintain(x,true);
 54     maintain(x,false);
 55 }
 56 void insert(int &x,int sp){
 57     if (!x){
 58         x=++cnt;
 59         tree[x].left=tree[x].right=0;
 60         tree[x].size=1;
 61         tree[x].key=sp;
 62     }else{
 63         tree[x].size++;
 64         if (sp<tree[x].key){
 65             insert(tree[x].left,sp);
 66         }else{
 67             insert(tree[x].right,sp);
 68         }
 69         maintain(x,sp>=tree[x].key);
 70     }
 71 }
 72 
 73 int del(int &x,int sp){
 74     tree[x].size--;
 75     if (sp==tree[x].key || ( sp<tree[x].key && tree[x].left==0) || (sp>tree[x].key && tree[x].right==0)){
 76         int y=tree[x].key;
 77         if (tree[x].left==0 ||tree[x].right==0){
 78             x=tree[x].left+tree[x].right;
 79             }else{
 80             tree[x].key=del(tree[x].left,tree[x].key+1);
 81         }
 82         return y;
 83     }else{
 84         if (sp<tree[x].key){
 85             return del(tree[x].left,sp);
 86         }else{
 87             return del(tree[x].right,sp);
 88         }
 89     }
 90 }
 91 
 92 inline int getMax(){
 93     int i;
 94     for (i=root;tree[i].right;i=tree[i].right);
 95     return tree[i].key;
 96 }
 97 
 98 inline int getMin(){
 99     int i;
100     for (i=root;tree[i].left;i=tree[i].left);
101     return tree[i].key;
102 }
103 
104 int rank(int &x,int sp){
105     if (sp<tree[x].key){
106         return rank(tree[x].left,sp);
107     }else if (sp>tree[x].key){
108         return rank(tree[x].right,sp)+tree[tree[x].left].size+1;
109     }
110     return tree[tree[x].left].size+1;
111 }
112 
113 int select(int &x,int rak){
114     int rk=tree[tree[x].left].size+1;
115     if (rak<rk){
116         return select(tree[x].left,rak);
117     }else if (rak>rk){
118         return select(tree[x].right,rak-rk);
119     }
120     return tree[x].key;
121 }
122 
123 int pred(int &x,int y,int sp){
124     if (x==0) return y;
125     if (tree[x].key<sp){
126         return pred(tree[x].right,x,sp);
127     }
128     return pred(tree[x].left,y,sp);
129 }
130 
131 int succ(int &x,int y,int sp){
132     if (x==0) return y;
133     if (tree[x].key>sp){
134         return succ(tree[x].left,x,sp);
135     }
136     return succ(tree[x].right,y,sp);
137 }
138 
139 inline void init(){
140     root=cnt=0;
141 }
142 
143 int main(){
144     init();
145     return 0;
146 }

 

SBT模版

标签:style   blog   io   color   sp   for   on   div   log   

原文地址:http://www.cnblogs.com/WNJXYK/p/4117283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!