平衡树(Splay)模板

  支持区间操作。

  单点操作和区间操作分开使用,需要一起使用需要部分修改。

  对应题目FJUTOJ2490

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 
  5 using namespace std;
  6 
  7 const int N=100005,INF=0x3f3f3f3f;
  8 
  9 struct Splay_Tree
 10 {
 11     struct Node
 12     {
 13         int val,Max,add,Size,son[2];
 14         bool rev;
 15         void init(int _val)
 16         {
 17             val=Max=_val,Size=1;
 18             add=rev=son[0]=son[1]=0;
 19         }
 20     } T[N];
 21     int fa[N],root,tot;
 22 
 23     void pushDown(int x)
 24     {
 25         if(x==0)return ;
 26         if(T[x].add)
 27         {
 28             if(T[x].son[0])
 29             {
 30                 T[T[x].son[0]].val+=T[x].add;
 31                 T[T[x].son[0]].Max+=T[x].add;
 32                 T[T[x].son[0]].add+=T[x].add;
 33             }
 34             if(T[x].son[1])
 35             {
 36                 T[T[x].son[1]].val+=T[x].add;
 37                 T[T[x].son[1]].Max+=T[x].add;
 38                 T[T[x].son[1]].add+=T[x].add;
 39             }
 40             T[x].add=0;
 41         }
 42         if(T[x].rev)
 43         {
 44             if(T[x].son[0])T[T[x].son[0]].rev^=1;
 45             if(T[x].son[1])T[T[x].son[1]].rev^=1;
 46             swap(T[x].son[0],T[x].son[1]);
 47             T[x].rev=0;
 48         }
 49     }
 50 
 51     void pushUp(int x)
 52     {
 53         T[x].Max=T[x].val,T[x].Size=1;
 54         if(T[x].son[0])
 55         {
 56             T[x].Max=max(T[x].Max,T[T[x].son[0]].Max);
 57             T[x].Size+=T[T[x].son[0]].Size;
 58         }
 59         if(T[x].son[1])
 60         {
 61             T[x].Max=max(T[x].Max,T[T[x].son[1]].Max);
 62             T[x].Size+=T[T[x].son[1]].Size;
 63         }
 64     }
 65 
 66     void Rotate(int x,int kind)
 67     {
 68         int y=fa[x],z=fa[y];
 69         T[y].son[!kind]=T[x].son[kind],fa[T[x].son[kind]]=y;
 70         T[x].son[kind]=y,fa[y]=x;
 71         T[z].son[T[z].son[1]==y]=x,fa[x]=z;
 72         pushUp(y);
 73     }
 74 
 75     void Splay(int x,int goal)
 76     {
 77         if(x==goal)return ;
 78         while(fa[x]!=goal)
 79         {
 80             int y=fa[x],z=fa[y];
 81             pushDown(z),pushDown(y),pushDown(x);
 82             int rx=T[y].son[0]==x,ry=T[z].son[0]==y;
 83             if(z==goal)Rotate(x,rx);
 84             else
 85             {
 86                 if(rx==ry)Rotate(y,ry);
 87                 else Rotate(x,rx);
 88                 Rotate(x,ry);
 89             }
 90         }
 91         pushUp(x);
 92         if(goal==0)root=x;
 93     }
 94 
 95     int Select(int pos)
 96     {
 97         int u=root;
 98         pushDown(u);
 99         while(T[T[u].son[0]].Size!=pos)
100         {
101             if(pos<T[T[u].son[0]].Size)u=T[u].son[0];
102             else
103             {
104                 pos-=T[T[u].son[0]].Size+1;
105                 u=T[u].son[1];
106             }
107             pushDown(u);
108         }
109         return u;
110     }
111 
112     void update(int L,int R,int val)
113     {
114         int u=Select(L-1),v=Select(R+1);
115         Splay(u,0);
116         Splay(v,u);
117         T[T[v].son[0]].Max+=val;
118         T[T[v].son[0]].val+=val;
119         T[T[v].son[0]].add+=val;
120     }
121 
122     void Reverse(int L,int R)
123     {
124         int u=Select(L-1),v=Select(R+1);
125         Splay(u,0);
126         Splay(v,u);
127         T[T[v].son[0]].rev^=1;
128     }
129 
130     int query(int L,int R)
131     {
132         int u=Select(L-1),v=Select(R+1);
133         Splay(u,0);
134         Splay(v,u);
135         return T[T[v].son[0]].Max;
136     }
137 
138     int build(int L,int R)
139     {
140         if(L>R)return 0;
141         if(L==R)return L;
142         int mid=(L+R)>>1,sL,sR;
143         T[mid].son[0]=sL=build(L,mid-1);
144         T[mid].son[1]=sR=build(mid+1,R);
145         fa[sL]=fa[sR]=mid;
146         pushUp(mid);
147         return mid;
148     }
149 
150     void init(int n)
151     {
152         T[0].init(-INF),T[1].init(-INF),T[n+2].init(-INF);
153         for(int i=2;i<=n+1;i++)T[i].init(0);
154         root=build(1,n+2),fa[root]=0;
155         fa[0]=0,T[0].son[1]=root,T[0].Size=0;
156     }
157 
158     inline void up(int x)
159     {
160         T[x].Size=1;
161         if(T[x].son[0])
162             T[x].Size+=T[T[x].son[0]].Size;
163         if(T[x].son[1])
164             T[x].Size+=T[T[x].son[1]].Size;
165     }
166 
167     void Insert(int &t,int val,int par=0)
168     {
169         if(t==0)
170         {
171             t=++tot;
172             T[t].init(val);
173             fa[t]=par;
174             Splay(tot,0);
175         }
176         else
177         {
178             int cur=t;
179             if(val<T[t].val)Insert(T[t].son[0],val,cur);
180             //else if(val==T[t].val)return ;
181             else Insert(T[t].son[1],val,cur);
182             up(cur);
183         }
184     }
185 
186     int find(int t,int v)
187     {
188         if(t==0)return 0;
189         else if(T[t].val==v)
190         {
191             Splay(t,0);
192             return t;
193         }
194         else if(v<T[t].val)return find(T[t].son[0],v);
195         else return find(T[t].son[1],v);
196     }
197 
198     ///Delete Root
199     void Delete()
200     {
201         if(!T[root].son[0])
202         {
203             fa[T[root].son[1]]=0;
204             root=T[root].son[1];
205         }
206         else
207         {
208             int cur=T[root].son[0];
209             while(T[cur].son[1])cur=T[cur].son[1];
210             Splay(cur,root);
211             T[cur].son[1]=T[root].son[1];
212             root=cur,fa[cur]=0,fa[T[root].son[1]]=root;
213             up(root);
214         }
215     }
216 
217     int size()
218     {
219         return T[root].Size;
220     }
221 
222     ///找前驱
223     int pred(int t,int v)
224     {
225         if(t==0)return v;
226         else
227         {
228             if(v<=T[t].val)return pred(T[t].son[0],v);
229             else
230             {
231                 int ans=pred(T[t].son[1],v);
232                 if(ans==v)
233                     ans=T[t].val,Splay(t,0);
234                 return ans;
235             }
236         }
237     }
238 
239     ///找后继
240     int succ(int t,int v)
241     {
242         if(t==0)return v;
243         else
244         {
245             if(v>=T[t].val)return succ(T[t].son[1],v);
246             else
247             {
248                 int ans=succ(T[t].son[0],v);
249                 if(ans==v)
250                     ans=T[t].val,Splay(t,0);
251                 return ans;
252             }
253         }
254     }
255 
256     void Preorder( int t ){
257         if( !t ) return;
258         Preorder( T[t].son[0] );
259         printf("%d " , T[t].val );
260         Preorder( T[t].son[1] );
261     }
262 
263     void init()
264     {
265         T[0].init(-INF);
266         tot=root=0;
267     }
268 
269 };
270 
271 Splay_Tree tree;
272 
273 int main()
274 {
275     int n,ans=0;
276     scanf("%d",&n);
277     tree.init();
278     tree.Insert(tree.root,-INF);
279     tree.Insert(tree.root,INF);
280     scanf("%d",&ans);
281     tree.Insert(tree.root,ans);
282     for(int i=1,x;i<n;i++)
283     {
284         scanf("%d",&x);
285         if(!tree.find(tree.root,x))
286         {
287             tree.Insert(tree.root,x);
288             ans+=min(x-tree.pred(tree.root,x),tree.succ(tree.root,x)-x);
289         }
290     }
291     printf("%d
",ans);
292     return 0;
293 }

再贴一个比较麻烦的题的代码FJUT2491

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 
  5 using namespace std;
  6 
  7 const int N=120005,INF=0x3f3f3f3f;
  8 
  9 struct Splay_Tree
 10 {
 11     struct Node
 12     {
 13         int val,Max,add,Size,son[2];
 14         bool rev;
 15         void init(int _val)
 16         {
 17             val=Max=_val,Size=1;
 18             add=rev=son[0]=son[1]=0;
 19         }
 20     } T[N];
 21     int fa[N],root,tot,loc[N];
 22 
 23     /*void pushDown(int x)
 24     {
 25         if(x==0)return ;
 26         if(T[x].add)
 27         {
 28             if(T[x].son[0])
 29             {
 30                 T[T[x].son[0]].val+=T[x].add;
 31                 T[T[x].son[0]].Max+=T[x].add;
 32                 T[T[x].son[0]].add+=T[x].add;
 33             }
 34             if(T[x].son[1])
 35             {
 36                 T[T[x].son[1]].val+=T[x].add;
 37                 T[T[x].son[1]].Max+=T[x].add;
 38                 T[T[x].son[1]].add+=T[x].add;
 39             }
 40             T[x].add=0;
 41         }
 42         if(T[x].rev)
 43         {
 44             if(T[x].son[0])T[T[x].son[0]].rev^=1;
 45             if(T[x].son[1])T[T[x].son[1]].rev^=1;
 46             swap(T[x].son[0],T[x].son[1]);
 47             T[x].rev=0;
 48         }
 49     }*/
 50 
 51     /*void pushUp(int x)
 52     {
 53         T[x].Max=T[x].val,T[x].Size=1;
 54         if(T[x].son[0])
 55         {
 56             T[x].Max=max(T[x].Max,T[T[x].son[0]].Max);
 57             T[x].Size+=T[T[x].son[0]].Size;
 58         }
 59         if(T[x].son[1])
 60         {
 61             T[x].Max=max(T[x].Max,T[T[x].son[1]].Max);
 62             T[x].Size+=T[T[x].son[1]].Size;
 63         }
 64     }*/
 65 
 66     inline void up(int x)
 67     {
 68         T[x].Size=1;
 69         if(T[x].son[0])
 70             T[x].Size+=T[T[x].son[0]].Size;
 71         if(T[x].son[1])
 72             T[x].Size+=T[T[x].son[1]].Size;
 73     }
 74 
 75     void Rotate(int x,int kind)
 76     {
 77         int y=fa[x],z=fa[y];
 78         T[y].son[!kind]=T[x].son[kind],fa[T[x].son[kind]]=y;
 79         T[x].son[kind]=y,fa[y]=x;
 80         T[z].son[T[z].son[1]==y]=x,fa[x]=z;
 81         up(y);
 82     }
 83 
 84     void Splay(int x,int goal)
 85     {
 86         if(x==goal)return ;
 87         while(fa[x]!=goal)
 88         {
 89             int y=fa[x],z=fa[y];
 90             int rx=T[y].son[0]==x,ry=T[z].son[0]==y;
 91             if(z==goal)Rotate(x,rx);
 92             else
 93             {
 94                 if(rx==ry)Rotate(y,ry);
 95                 else Rotate(x,rx);
 96                 Rotate(x,ry);
 97             }
 98         }
 99         up(x);
100         if(goal==0)root=x;
101     }
102 
103     int Select(int pos)
104     {
105         pos--;
106         int u=root;
107         while(T[T[u].son[0]].Size!=pos)
108         {
109             if(pos<T[T[u].son[0]].Size)u=T[u].son[0];
110             else
111             {
112                 pos-=T[T[u].son[0]].Size+1;
113                 u=T[u].son[1];
114             }
115         }
116         return u;
117     }
118 
119     void update(int L,int R,int val)
120     {
121         int u=Select(L-1),v=Select(R+1);
122         Splay(u,0);
123         Splay(v,u);
124         T[T[v].son[0]].Max+=val;
125         T[T[v].son[0]].val+=val;
126         T[T[v].son[0]].add+=val;
127     }
128 
129     void Reverse(int L,int R)
130     {
131         int u=Select(L-1),v=Select(R+1);
132         Splay(u,0);
133         Splay(v,u);
134         T[T[v].son[0]].rev^=1;
135     }
136 
137     int query(int L,int R)
138     {
139         int u=Select(L-1),v=Select(R+1);
140         Splay(u,0);
141         Splay(v,u);
142         return T[T[v].son[0]].Max;
143     }
144 
145     int build(int L,int R)
146     {
147         if(L>R)return 0;
148         if(L==R)return L;
149         int mid=(L+R)>>1,sL,sR;
150         T[mid].son[0]=sL=build(L,mid-1);
151         T[mid].son[1]=sR=build(mid+1,R);
152         fa[sL]=fa[sR]=mid;
153         up(mid);
154         return mid;
155     }
156 
157     void init(int n)
158     {
159         T[0].init(-INF);
160         for(int i=1,x;i<=n;i++)
161         {
162             scanf("%d",&x);
163             T[i].init(x),loc[x]=i;
164         }
165         root=build(1,n),fa[root]=0;
166         fa[0]=0,T[0].son[1]=root,T[0].Size=0;
167     }
168 
169     void Insert(int &t,int val,int par=0)
170     {
171         if(t==0)
172         {
173             t=++tot;
174             T[t].init(val);
175             fa[t]=par;
176             Splay(tot,0);
177         }
178         else
179         {
180             int cur=t;
181             if(val<T[t].val)Insert(T[t].son[0],val,cur);
182             //else if(val==T[t].val)return ;
183             else Insert(T[t].son[1],val,cur);
184             up(cur);
185         }
186     }
187 
188     int find(int t,int v)
189     {
190         if(t==0)return 0;
191         else if(T[t].val==v)
192         {
193             Splay(t,0);
194             return t;
195         }
196         else if(v<T[t].val)return find(T[t].son[0],v);
197         else return find(T[t].son[1],v);
198     }
199 
200     ///Delete Root
201     void Delete()
202     {
203         if(!T[root].son[0])
204         {
205             fa[T[root].son[1]]=0;
206             root=T[root].son[1];
207         }
208         else
209         {
210             int cur=T[root].son[0];
211             while(T[cur].son[1])cur=T[cur].son[1];
212             Splay(cur,root);
213             T[cur].son[1]=T[root].son[1];
214             root=cur,fa[cur]=0,fa[T[root].son[1]]=root;
215             up(root);
216         }
217     }
218 
219     int size()
220     {
221         return T[root].Size;
222     }
223 
224     ///找前驱
225     int pred(int t,int v)
226     {
227         if(t==0)return v;
228         else
229         {
230             if(v<=T[t].val)return pred(T[t].son[0],v);
231             else
232             {
233                 int ans=pred(T[t].son[1],v);
234                 if(ans==v)
235                     ans=T[t].val,Splay(t,0);
236                 return ans;
237             }
238         }
239     }
240 
241     int pred(int t)
242     {
243         Splay(t,0);
244         int u=T[t].son[0];
245         if(u==0)return fa[t];
246         while(T[u].son[1])u=T[u].son[1];
247         return u;
248     }
249 
250     ///找后继
251     int succ(int t,int v)
252     {
253         if(t==0)return v;
254         else
255         {
256             if(v>=T[t].val)return succ(T[t].son[1],v);
257             else
258             {
259                 int ans=succ(T[t].son[0],v);
260                 if(ans==v)
261                     ans=T[t].val,Splay(t,0);
262                 return ans;
263             }
264         }
265     }
266 
267     int succ(int t)
268     {
269         Splay(t,0);
270         int u=T[t].son[1];
271         if(u==0)return fa[t];
272         while(T[u].son[0])u=T[u].son[0];
273         return u;
274     }
275 
276     void Preorder( int t ){
277         if( !t ) return;
278         Preorder( T[t].son[0] );
279         if(T[t].son[0]&&fa[T[t].son[0]]!=t)
280             printf("!");
281         if(loc[T[t].val]!=t)
282             printf("?");
283         printf("%d " , T[t].val );
284         Preorder( T[t].son[1] );
285     }
286 
287     void init()
288     {
289         T[0].init(-INF);
290         tot=root=0;
291     }
292 
293     void update(int x)
294     {
295         while(x!=0)
296         up(x),x=fa[x];
297     }
298 
299 };
300 
301 Splay_Tree tree;
302 
303 char s[20];
304 
305 int main()
306 {
307     int n,m;
308     scanf("%d%d",&n,&m);
309     tree.init(n);
310     int x,y,loc,locm,loch;
311     while(m--)
312     {
313         scanf("%s",s);
314         if(s[0]=='T')
315         {
316             scanf("%d",&x);
317             loc=tree.loc[x];
318             locm=tree.Select(1);
319             if(locm==loc)
320                 continue;
321             tree.Splay(loc,0);
322             tree.Delete();
323             tree.T[locm].son[0]=loc;
324             tree.fa[loc]=locm;
325             tree.T[loc].son[0]=tree.T[loc].son[1]=0;
326             tree.update(loc);
327         }
328         else if(s[0]=='B')
329         {
330             scanf("%d",&x);
331             loc=tree.loc[x];
332             locm=tree.Select(n);
333             if(locm==loc)
334                 continue;
335             tree.Splay(loc,0);
336             tree.Delete();
337             tree.T[locm].son[1]=loc;
338             tree.fa[loc]=locm;
339             tree.T[loc].son[0]=tree.T[loc].son[1]=0;
340             tree.update(loc);
341         }
342         else if(s[0]=='I')
343         {
344             scanf("%d%d",&x,&y);
345             if(y==1)
346             {
347                 loc=tree.loc[x];
348                 loch=tree.succ(loc);
349                 swap(tree.T[loc].val,tree.T[loch].val);
350                 swap(tree.loc[tree.T[loc].val],tree.loc[tree.T[loch].val]);
351 
352             }
353             else if(y==-1)
354             {
355                 loc=tree.loc[x];
356                 loch=tree.pred(loc);
357                 swap(tree.T[loc].val,tree.T[loch].val);
358                 swap(tree.loc[tree.T[loc].val],tree.loc[tree.T[loch].val]);
359             }
360         }
361         else if(s[0]=='A')
362         {
363             scanf("%d",&x);
364             loc=tree.loc[x];
365             tree.Splay(loc,0);
366             printf("%d
",tree.T[tree.T[loc].son[0]].Size);
367         }
368         else if(s[0]=='Q')
369         {
370             scanf("%d",&x);
371             printf("%d
",tree.T[tree.Select(x)].val);
372         }
373         //tree.Preorder(tree.root);
374         //printf("
");
375     }
376     return 0;
377 }
原文地址:https://www.cnblogs.com/xseventh/p/7305204.html