bzoj3223: Tyvj 1729 文艺平衡树

地址:http://www.lydsy.com/JudgeOnline/problem.php?id=3223

题目:

3223: Tyvj 1729 文艺平衡树

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 5292  Solved: 3123
[Submit][Status][Discuss]

Description

 

您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 

Input

第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n)  m表示翻转操作次数
接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=n 

 

Output

 

输出一行n个数字,表示原始序列经过m次变换后的结果 

 

Sample Input

5 3

1 3

1 3

1 4

Sample Output

4 3 2 1 5

HINT

 



N,M<=100000

 

Source

 
思路:
  复习splay
  1 /**************************************************************
  2     Problem: 3223
  3     User: weeping
  4     Language: C++
  5     Result: Accepted
  6     Time:3032 ms
  7     Memory:4032 kb
  8 ****************************************************************/
  9  
 10 #include <bits/stdc++.h>
 11  
 12 using namespace std;
 13  
 14 #define lc ch[x][0]
 15 #define rc ch[x][1]
 16  
 17 struct SplayTree
 18 {
 19  
 20     const static int maxn = 1e5 + 15;
 21  
 22     int tot,root,ch[maxn][2], key[maxn], val[maxn], sz[maxn], rev[maxn], fa[maxn];
 23  
 24     inline void init( int x, int ky, int v = 0, int par = 0 )
 25     {
 26         lc=rc=0, fa[x]= par, key[x] = ky, val[x] = v, sz[x] = 1, rev[x] = 0;
 27     }
 28  
 29     inline void init()
 30     {
 31         init( 0, 0, 0 );
 32         sz[0] = 0;
 33         tot = root = 0 ;
 34     }
 35  
 36     inline void push_up(int x)
 37     {
 38         sz[x] = sz[lc] + sz[rc] + 1;
 39     }
 40  
 41     inline void reverse(int x)
 42     {
 43         rev[x] ^= 1, swap( lc, rc);
 44     }
 45  
 46     inline void push_down(int x)
 47     {
 48         if(rev[x])
 49         {
 50             if(lc)  reverse(lc);
 51             if(rc)  reverse(rc);
 52             rev[x] = 0;
 53         }
 54     }
 55  
 56     void rotate( int x)
 57     {
 58         int f = fa[x], gf = fa[f];
 59         int t1 = (ch[f][1] == x), t2 = (ch[gf][1] == f);
 60         if( gf ) ch[gf][t2] = x;
 61         fa[x] = gf, ch[f][t1] = ch[x][1^t1], fa[ch[f][t1]] = f;
 62         ch[x][t1^1] = f, fa[f] = x;
 63         push_up( f ), push_up( x );
 64     }
 65  
 66     void splay( int x, int tar )
 67     {
 68         for(int f = fa[x], gf = fa[f]; f != tar; rotate(x), f = fa[x], gf = fa[f])
 69         if(gf != tar)
 70             rotate( ((ch[f][1] == x) == (ch[gf][1] == f) )? f: x);
 71         if( !tar ) root = x;
 72     }
 73  
 74     void insert( int ky, int v)
 75     {
 76         int x = root, ls = root;
 77         while(x)
 78         {
 79             push_down(x);
 80             sz[x] ++, ls = x;
 81             x = ch[x][ky > key[x]];
 82         }
 83         init( ++tot, ky, v, ls);
 84         ch[ls][ky > key[ls]] = tot;
 85         splay( tot, 0);
 86     }
 87  
 88     int find( int ky)
 89     {
 90         int x = root;
 91         while(x)
 92         {
 93             push_down(x);
 94             if(key[x] == ky) break;
 95             x = ch[x][ky > key[x]];
 96         }
 97         if(x)   splay(x,0);
 98         else x = -1;
 99         return x;
100     }
101  
102     // Delete Root
103     void Delete()
104     {
105         if( !ch[root][0] )
106         {
107             fa[ ch[root][1] ] = 0 ;
108             root = ch[root][1];
109         }
110         else
111         {
112             int cur = ch[root][0];
113             while( ch[cur][1] ) cur = ch[cur][1];
114             splay( cur, root );
115             ch[cur][1] = ch[root][1];
116             root = cur, fa[cur] = 0, fa[ch[root][1]] = root;
117             push_up( root );
118         }
119     }
120  
121     int kth( int k)
122     {
123         int x = root;
124         if(sz[x] < k) return -1;
125         while(x)
126         {
127             push_down(x);
128             if(k == sz[lc] + 1) break;
129             if(k > sz[lc])
130                 k -= sz[lc] + 1, x = rc;
131             else
132                 x = lc;
133         }
134         if(x)   splay(x,0);
135         else x = -1;
136         return x;
137     }
138  
139     int pred( void)
140     {
141         int x = root;
142         if(!x || !lc)   return -1;
143         while(lc)    push_down(x), x = lc;
144         splay( x, 0);
145         return x;
146     }
147  
148     int succ( void)
149     {
150         int x = root;
151         if(!x || !rc) return -1;
152         while(rc)   push_down(x), x = rc;
153         splay( x, 0);
154         return x;
155     }
156  
157     void debug( int x,int n)
158     {
159         if( !x ) return;
160         push_down(x);
161         if(lc) debug( lc, n);
162         if(val[x]<=n && val[x]>0)
163             printf("%d ", val[x] );
164         if(rc) debug( rc, n);
165     }
166  
167     void go(int l,int r,int n)
168     {
169         int y = kth(r + 2), x = kth(l);
170         splay( y, x);
171         //debug(root,n);puts("");
172         reverse(ch[rc][0]);
173         //debug(root,n);puts("");
174     }
175 } sp;
176  
177 int main(void)
178 {
179     int n,m,l,r;
180     sp.init();
181     scanf("%d%d",&n,&m);
182     for(int i=0;i<=n+1;i++)
183         sp.insert(i,i);
184     while(m--)
185         scanf("%d%d",&l,&r),sp.go(l,r,n);
186     sp.debug( sp.root, n);
187     return 0;
188 }

 

posted @ 2017-10-13 16:36  weeping  阅读(175)  评论(0编辑  收藏  举报