UVA 11922 Splay interval flip + split + merge

Source: Internet
Author: User

UVA 11922 Splay interval flip + split + merge

-Permutation Transformer Time Limit:2000 MS Memory Limit:0 KB 64bit IO Format:% Lld & % lluSubmit Status Practice ultraviolet A 11922 Appoint description: System Crawler)

Description


Permutation Transformer

Write a program to transform the permutation 1, 2, 3 ,...,NAccordingMInstructions. Each instruction (A,B) Means to take out the subsequence fromA-Th toB-Th element, reverse it, then append it to the end.

Input

There is only one case for this problem. The first line contains two integersNAndM(1N,M100,000). Each of the nextMLines contains an instruction consisting of two integersAAndB(1ABN).

Output

PrintNLines, one for each integer, the final permutation.


Explanation of the sample below

Instruction (): Take out the subsequence {,}, reverse it to {5, 4, 3, 2}, append it to the remaining permutation {, 9, 10}

Instruction (): The subsequence from the 4-th to the 8-th element of {, 8, 9, 10, 5, 4, 3, 2} is {, 5, 4 }. take it out, reverse it, and you'll get the sample output.


Warning: Don't useCin,CoutFor this problem, use faster I/o methods e. gScanf,Printf.

Sample Input

10 22 54 8

Sample Output

16732451098




Perform m operations on the sequence, flip the [L, R] interval each time, and move it to the end.

After the range is extracted, flip, split, and merge twice.


Code:

/*************************************************************************    > File Name: Spaly.cpp    > Author: acvcla    > QQ:    > Mail: acvcla@gmail.com    > Created Time: 2014Äê11ÔÂ16ÈÕ ÐÇÆÚÈÕ 00ʱ14·Ö26Ãë ************************************************************************/#include
 
  #include#include
  
   #include
   
    #include
    
     #include
     #include
      
       #include
       
        #include
        
         #include
         
          #include
          
           #include
           
            #include
            
             using namespace std;typedef long long LL;const int maxn = 2e5 + 100;const int inf=1e9+7;#define rep(i,a,b) for(int i=(a);i<=(b);i++)#define pb push_backint ch[maxn][2],pre[maxn],rev[maxn],siz[maxn];int root,tot;int key[maxn];void newnode(int &x,int fa,int k){x=++tot;key[x]=k;siz[x]=1;pre[x]=fa;rev[x]=ch[x][1]=ch[x][0]=0;}void push_up(int x){if(!x)return;siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;}void upadte_rev(int x){if(!x)return ;swap(ch[x][0],ch[x][1]);rev[x]^=1;}void push_down(int x){if(!x)return;if(rev[x]){upadte_rev(ch[x][0]);upadte_rev(ch[x][1]);rev[x]^=1;}}void Rotate(int x,int kind){int y=pre[x];push_down(y);push_down(x);ch[y][!kind]=ch[x][kind];pre[ch[x][kind]]=y;ch[x][kind]=y;if(pre[y])ch[pre[y]][ch[pre[y]][1]==y]=x;pre[x]=pre[y];pre[y]=x;push_up(y);push_up(x);}void Splay(int x,int goal){while(pre[x]!=goal){if(pre[pre[x]]==goal)Rotate(x,ch[pre[x]][0]==x);else{int y=pre[x];int kind=(ch[pre[y]][0]==y);if(ch[y][kind]==x){Rotate(x,!kind);Rotate(x,kind);}else{Rotate(y,kind);Rotate(x,kind);}}}if(goal==0)root=x;}void built(int &x,int L,int R,int fa){if(L>R)return;int mid=(R+L)>>1;newnode(x,fa,mid);built(ch[x][0],L,mid-1,x);built(ch[x][1],mid+1,R,x);push_up(x);}void init(int n){siz[0]=pre[0]=0;root=tot=ch[0][0]=ch[0][1]=0;newnode(root,0,inf);newnode(ch[root][1],root,inf);built(ch[ch[root][1]][0],1,n,ch[root][1]);push_up(ch[root][1]);push_up(root);}int Get_kth(int x,int k){push_down(x);int sz=siz[ch[x][0]]+1;if(sz==k)return x;if(sz>k)return Get_kth(ch[x][0],k);return Get_kth(ch[x][1],k-sz);}void reverse(int L,int R){Splay(Get_kth(root,L-1),0);Splay(Get_kth(root,R+1),root);upadte_rev(ch[ch[root][1]][0]);}void append_to_end(){int t=ch[ch[root][1]][0];ch[ch[root][1]][0]=0;push_up(ch[root][1]);push_up(root);Splay(Get_kth(root,siz[root]-1),0);Splay(Get_kth(root,siz[root]),root);ch[ch[root][1]][0]=t;pre[t]=ch[root][1];push_up(ch[root][1]);push_up(root);}void print(int x){if(!x)return;push_down(x);print(ch[x][0]);if(key[x]!=inf)printf("%d\n",key[x]);print(ch[x][1]);}int main(int argc, char const *argv[]){int n,m,L,R;while(~scanf("%d%d",&n,&m)){init(n);while(m--){scanf("%d%d",&L,&R);reverse(L+1,R+1);append_to_end();}print(root);}return 0;}
            
           
          
         
        
       
      
    
   
  
 



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.