HDU 3487——Play with Chain(splay tree)

Play with Chain

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


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
 


————————————————————————分割线——————————————


题目大意:

对一个序列进行两种操作

1:将一段区间剪切下来插入到另外一个位置

2:翻转区间


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
const int maxn=3*100000+10;
#define REP(i,n) for(int i=0;i<(n);++i)
#define Key_value ch[ch[root][1]][0]
#define MES(s,c) memset(s,c,sizeof(s))
using namespace std;
int tot1,root,key[maxn],pre[maxn],size[maxn],ch[maxn][2];
int s[maxn],tot2;
int rev[maxn];
int a[maxn];
int n,q;
void Treaval(int rt)
{
    if(rt){
        Treaval(ch[rt][0]);
        printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d ,key = %2d \n",rt,ch[rt][0],ch[rt][1],pre[rt],size[rt],key[rt]);
        Treaval(ch[rt][1]);
    }
}
void debug(){printf("%d\n",root);Treaval(root);}
void NewNode(int &rt,int f,int k)
{
    if(tot2) rt=s[tot2--];
    else rt=++tot1;
//    rt=++tot1;
    ch[rt][1]=ch[rt][0]=0;
    pre[rt]=f;
    key[rt]=k;
    size[rt]=1;
}
void update_rev(int rt)
{
    if(!rt) return ;
    swap(ch[rt][0],ch[rt][1]);
    rev[rt]^=1;
}
void push_up(int rt)
{
    int lson=ch[rt][0],rson=ch[rt][1];
    size[rt]=size[lson]+size[rson]+1;
}
void push_down(int rt)
{
    if(rev[rt]){
        update_rev(ch[rt][0]);
        update_rev(ch[rt][1]);
        rev[rt]=0;
    }
}
void build(int &rt,int l,int r,int f)
{
    if(l>r) return ;
    int m=(l+r)>>1;
    NewNode(rt,f,m);
    build(ch[rt][0],l,m-1,rt);
    build(ch[rt][1],m+1,r,rt);
    push_up(rt);
}
void Init()
{
    root=ch[root][1]=ch[root][0]=pre[root]=key[root]=size[root]=rev[root]=0;
    tot1=tot2=0;
    NewNode(root,0,-1);
    NewNode(ch[root][1],root,-1);
//    REP(i,n) a[i]=i+1;
    build(Key_value,1,n,ch[root][1]);
    push_up(ch[root][1]);
    push_up(root);
}
void Rotate(int x,int c)
{
    int y=pre[x];
    push_down(y);
    push_down(x);
    ch[y][!c]=ch[x][c];
    pre[ch[x][c]]=y;
    if(pre[y])
        ch[pre[y]][ch[pre[y]][1]==y]=x;
    pre[x]=pre[y];
    ch[x][c]=y;
    pre[y]=x;
    push_up(y);
}
void Splay(int x,int goal)
{
    push_down(x);
    while(pre[x]!=goal){
        if(pre[pre[x]]==goal){
            Rotate(x,ch[pre[x]][0]==x);
        }
        else{
            int y=pre[x];
            int c=ch[pre[y]][0]==y;
            if(ch[y][c]==x){
                Rotate(x,!c);
                Rotate(x,c);
            }
            else{
                Rotate(y,c);
                Rotate(x,c);
            }
        }
    }
    push_up(x);
    if(goal==0) root=x;
}
int Get_kth(int rt,int k)
{
    push_down(rt);
    int t=size[ch[rt][0]]+1;
    if(t==k) return rt;
    if(t>k) return Get_kth(ch[rt][0],k);
    else return Get_kth(ch[rt][1],k-t);
}
//void Insert(int pos,int tot)
//{
//    REP(i,tot) scanf("%d",&a[i]);
//    Splay(Get_kth(root,pos+1),0);
//    Splay(Get_kth(root,pos+2),root);
//    build(Key_value,0,tot-1,ch[root][1]);
//    push_up(ch[root][1]);
//    push_up(root);
//}
//void erase(int rt)
//{
//    if(!rt) return ;
//    s[++tot2]=rt;
//    erase(ch[rt][0]);
//    erase(ch[rt][1]);
//}
//void Delete(int pos,int tot)
//{
//    Splay(Get_kth(root,pos),0);
//    Splay(Get_kth(root,pos+tot+1),root);
//    erase(Key_value);
//    pre[Key_value]=0;
//    Key_value=0;
//    push_up(ch[root][1]);
//    push_up(root);
//}
void Reverse(int a,int b)
{
    Splay(Get_kth(root,a),0);
    Splay(Get_kth(root,b+2),root);
    update_rev(Key_value);
    push_up(ch[root][1]);
    push_up(root);
}
void  Cut(int a,int b,int c)
{
    Splay(Get_kth(root,a),0);
    Splay(Get_kth(root,b+2),root);
    int index=Key_value;
    pre[Key_value]=0;
    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=index;
    pre[Key_value]=ch[root][1];
    push_up(ch[root][1]);
    push_up(root);
}
int cnt;
void InOrder(int rt)
{
    if(!rt) return ;
    push_down(rt);
    InOrder(ch[rt][0]);
    if(cnt>=1&&cnt<=n){
        if(cnt>1) printf(" ");
        printf("%d",key[rt]);
    }
    cnt++;
    InOrder(ch[rt][1]);

}
int main()
{
//#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
//#endif // ONLINE_JUDGE
    while(scanf("%d %d",&n,&q)!=EOF){
        if(n==-1||q==-1) break;
        Init();
        char op[20];
        int l,r,c;
        while(q--){
            scanf("%s",op);
            if(op[0]=='C'){
                scanf("%d %d %d",&l,&r,&c);
                Cut(l,r,c);
            }
            else{
                scanf("%d %d",&l,&r);
                Reverse(l,r);
            }
        }
        cnt=0;
        InOrder(root);
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,这是一道关于二叉树的问题,需要输出每个二叉树的层序遍历。如果二叉树没有完全给出,则输出“not complete”。而且,这道题目是一个ACM竞赛的题目,需要使用Java语言进行编写。 以下是Java语言的代码实现: ```java import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String s = sc.nextLine(); if (s.equals("")) { continue; } String[] arr = s.split(" "); int len = arr.length; int[] tree = new int[len]; boolean[] flag = new boolean[len]; for (int i = 0; i < len; i++) { if (arr[i].equals("()")) { tree[i] = -1; flag[i] = true; } else { tree[i] = Integer.parseInt(arr[i].substring(1, arr[i].length() - 1)); } } int root = 0; for (int i = 0; i < len; i++) { if (!flag[i]) { root = i; break; } } boolean isComplete = true; Queue<Integer> queue = new LinkedList<>(); queue.offer(root); int index = 1; while (!queue.isEmpty()) { int size = queue.size(); for (int i = 0; i < size; i++) { int cur = queue.poll(); if (tree[cur] == -1) { if (index < len && !flag[index]) { isComplete = false; } } else { if (cur * 2 + 1 < len) { queue.offer(cur * 2 + 1); if (tree[cur * 2 + 1] != -1) { flag[cur * 2 + 1] = true; } } if (cur * 2 + 2 < len) { queue.offer(cur * 2 + 2); if (tree[cur * 2 + 2] != -1) { flag[cur * 2 + 2] = true; } } } index++; } } if (!isComplete) { System.out.println("not complete"); continue; } queue.offer(root); StringBuilder sb = new StringBuilder(); while (!queue.isEmpty()) { int size = queue.size(); for (int i = 0; i < size; i++) { int cur = queue.poll(); sb.append(tree[cur]).append(" "); if (cur * 2 + 1 < len && tree[cur * 2 + 1] != -1) { queue.offer(cur * 2 + 1); } if (cur * 2 + 2 < len && tree[cur * 2 + 2] != -1) { queue.offer(cur * 2 + 2); } } } System.out.println(sb.toString().trim()); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值