hdu3487 splay树

 

Play with Chain

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

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
/*
hdu3487 splay树
cut是将一串数字复制到另外一个位置,flip是将一串数字逆序
cut直接进行一次删除插入即可,flip则是给个转换标记然后更新
hhh-2016-02-20 06:50:15
*/

#include <functional>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long  double ld;
#define key_value ch[ch[root][1]][0]
const int maxn = 300010;

int ch[maxn][2];
int rev[maxn],a[maxn];
int pre[maxn],key[maxn],siz[maxn];
int root,TOT,cnt,n;

void push_up(int r)
{
    int lson = ch[r][0],rson = ch[r][1];
    siz[r] = siz[lson] + siz[rson] + 1;
}

void update_rev(int r)
{
    if(!r) 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 inOrder(int r)
{
    if(!r)return;
    push_down(r);
    inOrder(ch[r][0]);
    if(cnt >=1 && cnt <= n)
    {
        printf("%d",key[r]);
        if(cnt < n)printf(" ");
        else printf("\n");
    }
    cnt++;
    inOrder(ch[r][1]);
}

void debug()
{
    cnt = 0;
    inOrder(root);
}

void NewNode(int &r,int far,int k)
{
    r = ++TOT;
    pre[r] = far;
    ch[r][0] = ch[r][1] = 0;
    key[r] = k;
    siz[r] = 1;
    rev[r] = 0;
}


void rotat(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 build(int &x,int l,int r,int far)
{
    if(l > r) return ;
    int mid = (l+r) >>1;
    NewNode(x,far,mid);
    build(ch[x][0],l,mid-1,x);
    build(ch[x][1],mid+1,r,x);
    push_up(x);
}

void splay(int r,int goal)
{
    push_down(r);
    while(pre[r] != goal)
    {
        if(pre[pre[r]] == goal)
        {
            push_down(pre[r]);
            push_down(r);
            rotat(r,ch[pre[r]][0] == r);
        }
        else
        {
            push_down(pre[pre[r]]);
            push_down(pre[r]);
            push_down(r);
            int y = pre[r];
            int kind = ch[pre[y]][0] == y;
            if(ch[y][kind] == r)
            {
                rotat(r,!kind);
                rotat(r,kind);
            }
            else
            {
                rotat(y,kind);
                rotat(r,kind);
            }
        }
    }
    push_up(r);
    if(goal == 0)
        root = r;
}

int get_kth(int r,int k)
{
    push_down(r);
    int t = siz[ch[r][0]]+1;
    if(t == k) return r;
    if(t > k) return get_kth(ch[r][0],k);
    else return  get_kth(ch[r][1],k-t);
}


int CUT(int l,int r,int b)
{
    splay(get_kth(root,l),0);
    splay(get_kth(root,r+2),root);
    int tmp = key_value;
    pre[key_value] = 0;
    key_value = 0;
    push_up(ch[root][1]);
    push_up(root);

    splay(get_kth(root,b+1),0);
    splay(get_kth(root,b+2),root);
    pre[tmp] = ch[root][1];
    key_value = tmp;
    push_up(ch[root][1]);
    push_up(root);
    //debug();
}

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);
    //debug();
}


void ini(int n)
{
    TOT = root = 0;
    ch[root][0] = ch[root][1] = pre[root] = siz[root] = 0;
    rev[root] = 0;
    NewNode(root,0,-1);
    NewNode(ch[root][1],root,-1);
    build(key_value,1,n,ch[root][1]);
    //inOrder(root);
     //printf("\n");
    push_up(ch[root][1]);
    push_up(root);
}

int main()
{
    int q;
    while(scanf("%d%d",&n,&q) != EOF)
    {

        if(n == -1 && q == -1)
            break;
        ini(n);
        for(int i =1; i <= q; i++)
        {
            char qry[10];
            scanf("%s",qry);
            int a,b,len;
            if(qry[0] == 'C')
            {
                scanf("%d%d%d",&a,&len,&b);
                CUT(a,len,b);
            }
            else
            {
                scanf("%d%d",&a,&b);
                Reverse(a,b);
            }
        }
        cnt = 0;
        inOrder(root);
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/Przz/p/5409623.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值