hdu 4286 Data Handler

分析

  模拟一个伸展树的操作,最后中序遍历一下就可以。。

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<iostream>
  4 #include<algorithm>
  5 #define N 1000010
  6 #pragma comment(linker, "/STACK:102400000,102400000")
  7 using namespace std;
  8 int ch[N][2],pre[N],s[N],sz[N],num[N];
  9 bool rev[N];
 10 int ans[N];
 11 int root,id,n,L,R,c;
 12 inline void pushup(int rt){
 13     sz[rt]=sz[ch[rt][0]]+sz[ch[rt][1]]+1;
 14 }
 15 inline void pushdown(int rt){
 16     if(rev[rt]){
 17         rev[ch[rt][0]]^=1;
 18         rev[ch[rt][1]]^=1;
 19         swap(ch[rt][0],ch[rt][1]);
 20         rev[rt]=0;
 21     }
 22 }
 23 void rotate(int x,int f){//f==0 right f==1 left
 24     int y=pre[x];
 25     pushdown(y);
 26     pushdown(x);
 27     ch[y][!f]=ch[x][f];
 28     pre[ch[x][f]]=y;
 29     pre[x]=pre[y];
 30     if(pre[x])ch[pre[y]][ch[pre[y]][1]==y]=x;
 31     ch[x][f]=y;
 32     pre[y]=x;
 33     pushup(y);
 34 }
 35 void splay(int x,int goal){
 36     pushdown(x);
 37     while(pre[x]!=goal){
 38         if(pre[pre[x]]==goal) rotate(x,ch[pre[x]][0]==x);
 39         else{
 40             int y=pre[x],f=(ch[pre[y]][0]==y);
 41             if(ch[y][f]==x) rotate(x,!f);
 42             else rotate(y,f);
 43             rotate(x,f);
 44         }
 45     }
 46     pushup(x);
 47     if(goal==0) root=x;
 48 }
 49 
 50 void select(int k,int goal){
 51     int x=root;
 52     pushdown(x);
 53     while(sz[ch[x][0]]!=k){
 54         if(sz[ch[x][0]]>k) x=ch[x][0];
 55         else k-=(sz[ch[x][0]]+1),x=ch[x][1];
 56         pushdown(x);
 57     }
 58     splay(x,goal);
 59 }
 60 
 61 void newnode(int &t,int a){
 62     t=++id;
 63     ch[t][0]=ch[t][1]=pre[t]=0;
 64     s[t]=a;
 65     rev[t]=0;
 66     sz[t]=1;
 67 }
 68 void build(int l,int r,int &t,int f){
 69     if(l>r)return ;
 70     int m=(l+r)>>1;
 71     newnode(t,num[m]);
 72     build(l,m-1,ch[t][0],t);
 73     build(m+1,r,ch[t][1],t);
 74     pre[t]=f;
 75     pushup(t);
 76 }
 77 void init(){
 78     ch[0][0]=ch[0][1]=pre[0]=0;
 79     sz[0]=0;rev[0]=0;
 80     id=root=0;
 81     newnode(root,-1);
 82     newnode(ch[root][1],-1);
 83     pre[id]=root;
 84     sz[root]=2;
 85     build(1,n,ch[ch[root][1]][0],ch[root][1]);
 86     pushup(ch[root][1]);
 87     pushup(root);
 88 }
 89 void show(int x){
 90     if(x==0)return;
 91     pushdown(x);
 92     show(ch[x][0]);
 93     ans[++c]=s[x];
 94     show(ch[x][1]);
 95 }
 96 int main(){
 97  //   freopen("in.txt","r",stdin);
 98     int t;
 99     scanf("%d",&t);
100     while(t--){
101         scanf("%d",&n);
102         for(int i=1;i<=n;i++)scanf("%d",&num[i]);
103         init();
104         scanf("%d%d",&L,&R);
105         int m;
106         scanf("%d",&m);
107          char op[20],u[5];
108         while(m--){
109             int a;
110             scanf("%s",op);
111             if(op[0]=='M'&&op[4]=='L'){
112                 scanf("%s",u);
113                 if(u[0]=='L') L--;
114                 else R--;
115             }else if(op[0]=='M'&&op[4]=='R'){
116                 scanf("%s",u);
117                 if(u[0]=='L') L++;
118                 else R++;
119             }else if(op[0]=='I'){
120                 scanf("%s%d",u,&a);
121                 if(u[0]=='L'){
122                     select(L-1,0);
123                     select(L,root);
124                     newnode(ch[ch[root][1]][0],a);
125                     pre[ch[ch[root][1]][0]]=ch[root][1];
126                     ++R;
127                 }else{
128                     select(R,0);
129                     select(R+1,root);
130                     newnode(ch[ch[root][1]][0],a);
131                     pre[ch[ch[root][1]][0]]=ch[root][1];
132                     ++R;
133                 }
134             }else if(op[0]=='D'){
135                 scanf("%s",u);
136                 if(u[0]=='L'){
137                     select(L-1,0);
138                     select(L+1,root);
139                     ch[ch[root][1]][0]=0;
140                     pushup(ch[root][1]);
141                     pushup(root);
142                     R--;
143                 }else{
144                     select(R-1,0);
145                     select(R+1,root);
146                     ch[ch[root][1]][0]=0;
147                     pushup(ch[root][1]);
148                     pushup(root);
149                     R--;
150                 }
151             }else{
152                 select(L-1,0);
153                 select(R+1,root);
154                 rev[ch[ch[root][1]][0]]^=1;
155             }
156         }
157         c=0;
158         show(root);
159         for(int i=2;i<c-1;i++)
160             printf("%d ",ans[i]);
161         printf("%d\n",ans[c-1]);
162     }
163     return 0;
164 }
posted @ 2012-10-09 11:00  silver__bullet  阅读(149)  评论(0编辑  收藏  举报