zoukankan      html  css  js  c++  java
  • HDU 3487 Splay tree

    Play with Chain

    Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 6779    Accepted Submission(s): 2678


    Problem Description
    YaoYao is fond of playing his chains. He has a chain containing n diamonds on it. Diamonds are numbered from 1 to n.
    At first, the diamonds on the chain is a sequence: 1, 2, 3, …, n.
    He will perform two types of operations:
    CUT a b c: He will first cut down the chain from the ath diamond to the bth diamond. And then insert it after the cth diamond on the remaining chain.
    For example, if n=8, the chain is: 1 2 3 4 5 6 7 8; We perform “CUT 3 5 4”, Then we first cut down 3 4 5, and the remaining chain would be: 1 2 6 7 8. Then we insert “3 4 5” into the chain before 5th diamond, the chain turns out to be: 1 2 6 7 3 4 5 8.

    FLIP a b: We first cut down the chain from the ath diamond to the bth diamond. Then reverse the chain and put them back to the original position.
    For example, if we perform “FLIP 2 6” on the chain: 1 2 6 7 3 4 5 8. The chain will turn out to be: 1 4 3 7 6 2 5 8

    He wants to know what the chain looks like after perform m operations. Could you help him? 
     
    Input
    There will be multiple test cases in a test data. 
    For each test case, the first line contains two numbers: n and m (1≤n, m≤3*100000), indicating the total number of diamonds on the chain and the number of operations respectively.
    Then m lines follow, each line contains one operation. The command is like this:
    CUT a b c // Means a CUT operation, 1 ≤ a ≤ b ≤ n, 0≤ c ≤ n-(b-a+1).
    FLIP a b    // Means a FLIP operation, 1 ≤ a < b ≤ n.
    The input ends up with two negative numbers, which should not be processed as a case.
     
    Output
    For each test case, you should print a line with n numbers. The ith number is the number of the ith diamond on the chain.
     
    Sample Input
    8 2
    CUT 3 5 4
    FLIP 2 6
    -1 -1
     
    Sample Output
    1 4 3 7 6 2 5 8
    题意:
    有n个数1~n,两种操作,a b c表示将第a~b的数拿出来放到第c个数后面,a b表示翻转a~b之间的数
    输出最终的数列
    代码:
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    #define Key_Value ch[ch[root][1]][0]
    const int maxn=300009;
    int n,q;
    int pre[maxn],ch[maxn][2],rev[maxn],size[maxn],key[maxn],root,tot1;
    void Update_Rev(int r){
        if(r==0) return;
        swap(ch[r][0],ch[r][1]);
        rev[r]^=1;
    }
    void Push_Down(int r){
        if(rev[r]){
            Update_Rev(ch[r][0]);
            Update_Rev(ch[r][1]);
            rev[r]=0;
        }
    }
    void Push_Up(int r){
        size[r]=1+size[ch[r][0]]+size[ch[r][1]];
    }
    void New_Node(int &r,int fa,int k){
        r=++tot1;
        pre[r]=fa;
        key[r]=k;
        rev[r]=ch[r][0]=ch[r][1]=0;
        size[r]=1;
    }
    void Build(int &x,int l,int r,int fa){
        if(l>r) return;
        int mid=(l+r)>>1;
        New_Node(x,fa,mid);
        Build(ch[x][0],l,mid-1,x);
        Build(ch[x][1],mid+1,r,x);
        Push_Up(x);
    }
    void Init(){
        root=tot1=0;
        key[root]=ch[root][0]=ch[root][1]=size[root]=rev[root]=pre[root]=0;
        New_Node(root,0,-1);
        New_Node(ch[root][1],root,-1);
        Build(Key_Value,1,n,ch[root][1]);
        Push_Up(ch[root][1]);
        Push_Up(root);
    }
    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;
        if(pre[y]) ch[pre[y]][ch[pre[y]][1]==y]=x;
        pre[x]=pre[y];
        ch[x][kind]=y;
        pre[y]=x;
        Push_Up(y);
    }
    void Splay(int r,int goal){
        while(pre[r]!=goal){
            if(pre[pre[r]]==goal)
                Rotate(r,ch[pre[r]][0]==r);
            else{
                int y=pre[r];
                int kind=ch[pre[y]][0]==y;
                if(ch[y][kind]==r){
                    Rotate(r,!kind);
                    Rotate(r,kind);
                }else{
                    Rotate(y,kind);
                    Rotate(r,kind);
                }
            }
        }
        Push_Up(r);
        if(goal==0) root=r;
    }
    int Get_Kth(int r,int k){
        Push_Down(r);
        int t=size[ch[r][0]];
        if(k==t+1) return r;
        else if(k<=t) return Get_Kth(ch[r][0],k);
        else return Get_Kth(ch[r][1],k-t-1);
    }
    void CUT(int a,int b,int c){
        Splay(Get_Kth(root,a),0);
        Splay(Get_Kth(root,b+2),root);
        int temp=Key_Value;
        Key_Value=0;
        Push_Up(ch[root][1]);
        Push_Up(root);
    
        Splay(Get_Kth(root,c+1),0);
        Splay(Get_Kth(root,c+2),root);
        Key_Value=temp;
        pre[Key_Value]=ch[root][1];
        Push_Up(ch[root][1]);
        Push_Up(root);
    }
    void REVERSE(int l,int r){
        Splay(Get_Kth(root,l),0);
        Splay(Get_Kth(root,r+2),root);
        Update_Rev(Key_Value);
        Push_Up(ch[root][1]);
        Push_Up(root);
    }
    int cnt;
    void print(int r){
        if(r==0) return;
        Push_Down(r);
        print(ch[r][0]);
        if(cnt<n&&key[r]>0){
            cnt++;
            printf("%d%c",key[r],cnt==n?'
    ':' ');
        }
        print(ch[r][1]);
    }
    int main()
    {
        while(scanf("%d%d",&n,&q)==2){
            if(n<0&&q<0) break;
            Init();
            char op[10];
            int x,y,z;
            while(q--){
                scanf("%s",op);
                if(op[0]=='C'){
                    scanf("%d%d%d",&x,&y,&z);
                    CUT(x,y,z);
                }
                else if(op[0]=='F'){
                    scanf("%d%d",&x,&y);
                    REVERSE(x,y);
                }
            }
            cnt=0;
            print(root);
        }
        return 0;
    }
     
  • 相关阅读:
    【rust】Rust 的构建系统和包管理工具Cargo认识并初步使用(2)
    【rust】rust安装,运行第一个Rust 程序 (1)
    linux 双网卡桥接,实现网卡流量镜像与转发
    【原创】使用golang访问windows telnet服务器
    使用centos 7安装conpot
    用Redis作Mysql数据库缓存
    python解析处理snmp回显----snmp
    snmp自定义OID与文件下载----服务器端配置
    golang map输出排序
    计算机组成原理---第1章 计算机系统概述
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/7147433.html
Copyright © 2011-2022 走看看