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

hdu 3487

时间:2014-09-09 15:58:08      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   ar   for   art   

 

因为知道了翻转的延迟标记的处理,这题写起来就没有什么卡代码的地方。

这题还有个操作,把区间切下来插入某个点。

 

bubuko.com,布布扣
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #define KT ch[ch[root][1]][0]
  6 #define LC ch[x][0]
  7 #define RC ch[x][1]
  8 #define N 310001
  9 using namespace std;
 10 
 11 struct SplayTree{
 12     int root;
 13     int fa[N];
 14     int ch[N][2];
 15     int sz[N];
 16     int top1;
 17     int top2;
 18     int ss[N];
 19     int que[N];
 20 
 21     void pushdown(int x){
 22         if(flip[x]){
 23             flip[LC]^=1;flip[RC]^=1;
 24             rever(LC);rever(RC);
 25             flip[x]=0;
 26         }
 27     }
 28     void pushup(int x){
 29         sz[x]=1+sz[LC]+sz[RC];
 30     }
 31     void rotate(int x,bool f){
 32         int y=fa[x];
 33         int z=fa[y];
 34         pushdown(y);
 35         pushdown(x);
 36         ch[y][!f]=ch[x][f];
 37         fa[ch[x][f]]=y;
 38         fa[x]=z;
 39         if (z) {
 40             ch[z][ch[z][1]==y] = x;
 41         }
 42         ch[x][f] = y;
 43         fa[y]=x;
 44         pushup(y);
 45     }
 46     void splay(int x, int g) {
 47         int y = fa[x];
 48         pushdown(x);
 49         while(y!=g){
 50             int z= fa[y];
 51             bool f = (ch[y][0]==x);
 52             if(z != g && f == (ch[z][0]==y)){
 53                 rotate(y,f);
 54             }
 55             rotate(x,f);
 56             y=fa[x];
 57         }
 58         pushup(x);
 59         if(g==0) root = x;
 60     }
 61     void rotateTo(int k,int g) {
 62         int x=root;
 63         pushdown(x);
 64         while(sz[LC] != k){
 65             if(k<sz[LC]){
 66                 x=LC;
 67             }else{
 68                 k -= sz[LC] + 1;
 69                 x = RC;
 70             }
 71             pushdown(x);
 72         }
 73         splay(x,g);
 74     }
 75     void build(int l,int r,int f){
 76         if(l>r){
 77             return ;
 78         }
 79         int x = (l + r) >> 1;
 80         LC = (x - 1 >= l)? (x - 1 + l)>> 1 :0;
 81         RC = (r >= x + 1)? (x + 1 + r)>> 1 :0;
 82         fa[x] = f;
 83         build(l,x - 1,x);
 84         build(x + 1,r,x);
 85         pushup(x);
 86     }
 87     void erase(int x){
 88         if(x==0)
 89             return;
 90         int father= fa[x];
 91         int head = 0, tail=0;
 92         for(que[tail++] =x ; head < tail; head++){
 93             ss[top2++] = que[head];
 94             if(ch[que[head]][0]){
 95                 que[tail++]=ch[que[head]][0];
 96             }
 97             if(ch[que[head]][1]){
 98                 que[tail++] = ch[que[head]][1];
 99             }
100         }
101         ch[father][ch[father][1]==x]=0;
102         pushup(father);
103     }
104     void treaval(int x){
105         if (x) {
106             pushdown(x);
107             treaval(LC);
108             //printf("@%d",val[x]);
109             ans[cnt++]=val[x];
110             treaval(RC);
111         }
112     }
113     void newNode(int &x,int c){
114         if(top2){
115             x = ss[--top2];
116         } else {
117             x = ++top1;
118         }
119         LC = RC = fa[x] =0;
120         sz[x] = 1;
121         val[x] = c;
122         flip[x]=0;
123     }
124     void makeTree(int &x, int l, int r, int f){
125         if(l > r){
126             return;
127         }
128         int m=(l+r)>>1;
129         newNode(x, m);
130         makeTree(LC,l,m-1,x);
131         makeTree(RC,m+1,r,x);
132         fa[x]=f;
133         pushup(x);
134     }
135     void debug(){
136         treaval(root);
137         cout<<endl;
138     }
139     void cutTo(int l,int r,int c){
140         rotateTo(l-1,0);
141         rotateTo(r+1,root);
142         //debug();
143         int tmp=KT;
144         KT=0;
145         pushup(ch[root][1]);
146         pushup(root);
147         rotateTo(c,0);
148         rotateTo(c+1,root);
149         fa[tmp]=ch[root][1];
150         KT=tmp;
151         pushup(ch[root][1]);
152         pushup(root);
153         //debug();
154     }
155     void rever(int x){
156         swap(ch[x][0],ch[x][1]);
157     }
158     void reverse(int l,int r){
159         rotateTo(l-1,0);
160         rotateTo(r+1,root);
161         flip[KT]^=1;
162         rever(KT);
163     }
164     void init(int n){
165 
166         top1=top2=root=0;
167         newNode(root,0);
168         newNode(ch[root][1],0);
169         fa[ch[root][1]]=root;
170         //for(int i=1;i<=n;i++)scanf("%d",&num[i]);
171         makeTree(KT,1,n,ch[root][1]);
172         pushup(ch[root][1]);
173         pushup(root);
174     }
175     void solve(int n,int m){
176         char op[10];
177         int l,r;
178         int c;
179 
180         for(int i=0;i<m;i++){
181 
182             scanf("%s%d%d",op,&l,&r);
183             if(op[0]==F){
184                 reverse(l,r);
185             }else{
186                 scanf("%d",&c);
187                 cutTo(l,r,c);
188             }
189         }
190         cnt=0;
191         treaval(root);
192         for(int i=1;i<n;i++){
193             printf("%d ",ans[i]);
194         }
195         printf("%d\n",ans[n]);
196 
197     }
198     int val[N];
199     int flip[N];
200     int num[N];
201     int ans[N];
202     int cnt;
203 }spt;
204 int main()
205 {
206     int n,m;
207 
208     while(scanf("%d%d",&n,&m),n>=0||m>=0){
209         spt.init(n);
210         spt.solve(n,m);
211     }
212     return 0;
213 }
View Code

 

hdu 3487

标签:style   blog   http   color   os   io   ar   for   art   

原文地址:http://www.cnblogs.com/youyouyou/p/3962618.html

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