bzoj3224: Tyvj 1728 普通平衡树(splay)

3224: Tyvj 1728 普通平衡树

题目:传送门 

 


 

 

题解:

   啦啦啦啦又来敲个模版水经验啦~

    


 

 

代码:

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #include<cmath>
  5 #include<algorithm>
  6 using namespace std;
  7 struct node
  8 {
  9     int d,c,n,f,son[2];
 10 }tr[110000];int len,root;
 11 void add(int d,int f)
 12 {
 13     len++;tr[len].d=d;tr[len].n=tr[len].c=1;
 14     tr[len].son[0]=tr[len].son[1]=0;tr[len].f=f;
 15     if(d<tr[f].d)tr[f].son[0]=len;
 16     else         tr[f].son[1]=len;
 17 }
 18 void update(int x)
 19 {
 20     int lc=tr[x].son[0],rc=tr[x].son[1];
 21     tr[x].c=tr[lc].c+tr[rc].c+tr[x].n;
 22 }
 23 int findip(int d)
 24 {
 25     int x=root;
 26     while(tr[x].d!=d)
 27     {
 28         if(d<tr[x].d)
 29         {
 30             if(tr[x].son[0]==0)break;
 31             else x=tr[x].son[0];
 32         }
 33         else
 34         {
 35             if(tr[x].son[1]==0)break;
 36             else x=tr[x].son[1];
 37         }
 38     }
 39     return x;
 40 }
 41 void rotate(int x,int w)
 42 {
 43     int f=tr[x].f,ff=tr[f].f;
 44     int r,R;
 45     r=tr[x].son[w],R=f;
 46     tr[R].son[1-w]=r;
 47     if(r!=0)tr[r].f=R;
 48      
 49     r=x,R=ff;
 50     if(tr[R].son[0]==f)tr[R].son[0]=r;
 51     else               tr[R].son[1]=r;
 52     tr[r].f=R;
 53      
 54     r=f,R=x;
 55     tr[R].son[w]=r;
 56     tr[r].f=R;
 57      
 58     update(f);update(x);
 59 }
 60 void splay(int x,int rt)
 61 {
 62     while(tr[x].f!=rt)
 63     {
 64         int f=tr[x].f,ff=tr[f].f;
 65         if(ff==rt)
 66         {
 67             if(tr[f].son[0]==x)rotate(x,1);
 68             else               rotate(x,0);
 69         }
 70         else
 71         {
 72                  if(tr[ff].son[0]==f && tr[f].son[0]==x)rotate(f,1),rotate(x,1);
 73             else if(tr[ff].son[1]==f && tr[f].son[1]==x)rotate(f,0),rotate(x,0);
 74             else if(tr[ff].son[1]==f && tr[f].son[0]==x)rotate(x,1),rotate(x,0);
 75             else if(tr[ff].son[0]==f && tr[f].son[1]==x)rotate(x,0),rotate(x,1);
 76         }
 77     }
 78     if(rt==0)root=x;
 79 }
 80 void ins(int d)
 81 {
 82     if(root==0)
 83     {
 84         add(d,0);root=len;
 85         return ;
 86     }
 87     int x=findip(d);
 88     if(tr[x].d==d)
 89     {
 90         tr[x].n++;
 91         update(x);
 92         splay(x,0);
 93     }
 94     else
 95     {
 96         add(d,x);
 97         update(x);
 98         splay(len,0);
 99     }
100 }
101 void del(int d)
102 {
103     int x=findip(d);if(tr[x].d!=d)return ;
104     splay(x,0);
105     if(tr[x].n>1){tr[x].n--;update(x);return ;}
106     if(tr[x].son[0]==0 && tr[x].son[1]==0){root=0;len=0;}
107     else if(tr[x].son[0]!=0 && tr[x].son[1]==0){root=tr[x].son[0];tr[root].f=0;}
108     else if(tr[x].son[0]==0 && tr[x].son[1]!=0){root=tr[x].son[1];tr[root].f=0;}
109     else
110     {
111         int p=tr[x].son[0];
112         while(tr[p].son[1]!=0)p=tr[p].son[1];
113         splay(p,x);
114          
115         int r=tr[x].son[1],R=p;
116         tr[R].son[1]=r;
117         tr[r].f=R;
118          
119         root=p;tr[root].f=0;
120         update(root);
121     }
122 }
123 void findpaiming(int d)
124 {
125     int x=findip(d);splay(x,0);int lc=tr[x].son[0];
126     printf("%d\n",tr[lc].c+1);
127 }
128 void findshuzi(int k)
129 {
130     if(tr[root].c<k){printf("-1\n");return ;}
131     int x=root;
132     while(1)
133     {
134         int lc=tr[x].son[0],rc=tr[x].son[1];
135         if(k<=tr[lc].c)x=lc;
136         else if(k>tr[lc].c+tr[x].n)k-=tr[lc].c+tr[x].n,x=rc;
137         else break;
138     }
139     printf("%d\n",tr[x].d);
140 }
141 void findqianqu(int d)
142 {
143     int x=findip(d);splay(x,0);
144     if(d<=tr[x].d && tr[x].son[0]!=0)
145     {
146         x=tr[x].son[0];
147         while(tr[x].son[1]!=0)x=tr[x].son[1];
148     }
149     if(d<=tr[x].d)x=0;
150     printf("%d\n",tr[x].d);
151 }
152 void findhouji(int d)
153 {
154     int x=findip(d);splay(x,0);
155     if(d>=tr[x].d && tr[x].son[1]!=0)
156     {
157         x=tr[x].son[1];
158         while(tr[x].son[0]!=0)x=tr[x].son[0];
159     }
160     if(d>=tr[x].d)x=0;
161     printf("%d\n",tr[x].d);
162 }
163 /*
164 void dell(int l,int r)
165 {
166     int lc=findqianqu(tr[l].d),rc=findhouji(tr[r].d);
167     spaly(lc,0);spaly(rc,lc);
168     tr[rc].son[0]=0;len-=tr[tr[rc].lc].c;
169     update(rc);update(lc);
170 }
171 */
172 int main()
173 {
174     int n;scanf("%d",&n);root=0;len=0;
175     for(int i=1;i<=n;i++)
176     {
177         int opt,d;scanf("%d%d",&opt,&d);
178         if(opt==1)ins(d);
179         else if(opt==2)del(d);
180         else if(opt==3)findpaiming(d);
181         else if(opt==4)findshuzi(d);
182         else if(opt==5)findqianqu(d);
183         else if(opt==6)findhouji(d);
184     }
185     return 0;
186 }

 

posted @ 2018-04-12 21:05  CHerish_OI  阅读(320)  评论(1编辑  收藏  举报