uva 11922 - Permutation Transformer

splay的题;

学习白书上和网上的代码敲的;

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <cstdlib>
  4 #include <algorithm>
  5 using namespace std;
  6 int n,m;
  7 struct node
  8 {
  9     node *ch[2];
 10     int s,v;
 11     int flip;
 12     node(int v):v(v)
 13     {
 14         ch[1]=ch[0]=NULL;
 15         s=1;
 16         flip=0;
 17     }
 18     void maintain()
 19     {
 20         s=1;
 21         if(ch[0]!=NULL)s+=ch[0]->s;
 22         if(ch[1]!=NULL)s+=ch[1]->s;
 23     }
 24     void pushdown()
 25     {
 26         if(flip)
 27         {
 28             flip=0;
 29             swap(ch[0],ch[1]);
 30             if(ch[0]!=NULL)ch[0]->flip=!ch[0]->flip;
 31             if(ch[1]!=NULL)ch[1]->flip=!ch[1]->flip;
 32         }
 33     }
 34     int cmp(int x)const
 35     {
 36         int t=(ch[0]==NULL)?0:ch[0]->s;
 37         if(t>=x)return 0;
 38         if(t+1==x)return -1;
 39         return 1;
 40     }
 41 };
 42 void rotate(node* &root,int d)
 43 {
 44     node *k=root->ch[d^1];
 45     root->ch[d^1]=k->ch[d];
 46     k->ch[d]=root;
 47     root=k;
 48     root->ch[d]->maintain();
 49     root->maintain();
 50 }
 51 
 52 void build(node* &root,int l,int r)
 53 {
 54     int mid=(l+r)>>1;
 55     root=new node(mid);
 56     if(l<mid)build(root->ch[0],l,mid-1);
 57     if(r>mid)build(root->ch[1],mid+1,r);
 58     root->maintain();
 59 }
 60 
 61 void splay(node* &root,int k)
 62 {
 63     root->pushdown();
 64     int d=root->cmp(k);
 65     if(d==1)
 66     {
 67         if(root->ch[0]!=NULL)
 68             k-=root->ch[0]->s;
 69         k--;
 70     }
 71     if(d!=-1)
 72     {
 73         node *p=root->ch[d];
 74         p->pushdown();
 75         int d2=p->cmp(k);
 76         int k2=k;
 77         if(d2==1)
 78         {
 79             if(p->ch[0]!=NULL)
 80                 k2-=p->ch[0]->s;
 81             k2--;
 82         }
 83         if(d2!=-1)
 84         {
 85             splay(p->ch[d2],k2);
 86             if(d==d2) rotate(root,d^1);
 87             else rotate(root->ch[d],d);
 88         }
 89         rotate(root,d^1);
 90     }
 91     return;
 92 }
 93 
 94 void split(node *root,int k,node* &left,node* &right)
 95 {
 96     splay(root,k);
 97     left=root;
 98     right=root->ch[1];
 99     root->ch[1]=NULL;
100     left->maintain();//左边的要维护,右边不需要;
101 }
102 
103 node *merge(node *left,node *right)
104 {
105     splay(left,left->s);//
106     left->ch[1]=right;
107     left->maintain();
108     return left;
109 }
110 
111 void dfs(node *root)
112 {
113     root->pushdown();
114     if(root->ch[0])dfs(root->ch[0]);
115     if(root->v&&root->v!=n+1)printf("%d\n",root->v);
116     if(root->ch[1])dfs(root->ch[1]);
117 }
118 
119 void del(node *root)
120 {
121     if(root->ch[0])del(root->ch[0]);
122     if(root->ch[1])del(root->ch[1]);
123     delete root;
124 }
125 
126 
127 node *root;
128 int main()
129 {
130     int a,b;
131     while(scanf("%d%d",&n,&m)!=EOF)
132     {
133         root=NULL;
134         build(root,0,n+1);
135         while(m--)
136         {
137             scanf("%d%d",&a,&b);
138             node *right,*mid,*left,*tmp,*o;
139             split(root,a,left,o);
140             split(o,b-a+1,mid,right);
141             if(right->s>1)
142             {
143                 split(right,right->s-1,tmp,o);
144                 mid->flip^=1;
145                 root=merge(left,merge(merge(tmp,mid),o));
146             }
147             else
148             {
149                 mid->flip^=1;
150                 root=merge(left,merge(mid,right));
151             }
152         }
153         dfs(root);
154         del(root);
155     }
156     return 0;
157 }
View Code

 

posted @ 2013-11-01 18:58  Yours1103  阅读(188)  评论(0编辑  收藏  举报