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;
}

  

hdu3487 splay树的更多相关文章

  1. Hdu-3487 Splay树,删除,添加,Lazy延迟标记操作

    HDU_3487 题意:给出n和q,n代表1-n的序列,接下来q有两种操作,Cut a b c:表示把区间[a,b]截掉然后放在第c个数的后面,Flip a b 表示把区间[a,b]反转,经过一系列的 ...

  2. Splay树-Codevs 1296 营业额统计

    Codevs 1296 营业额统计 题目描述 Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司 ...

  3. ZOJ3765 Lights Splay树

    非常裸的一棵Splay树,需要询问的是区间gcd,但是区间上每个数分成了两种状态,做的时候分别存在val[2]的数组里就好.区间gcd的时候基本上不支持区间的操作了吧..不然你一个区间里加一个数gcd ...

  4. Splay树再学习

    队友最近可能在学Splay,然后让我敲下HDU1754的题,其实是很裸的一个线段树,不过用下Splay也无妨,他说他双旋超时,单旋过了,所以我就敲来看下.但是之前写的那个Splay越发的觉得不能看,所 ...

  5. 暑假学习日记:Splay树

    从昨天开始我就想学这个伸展树了,今天花了一个上午2个多小时加下午2个多小时,学习了一下伸展树(Splay树),学习的时候主要是看别人博客啦~发现下面这个博客挺不错的http://zakir.is-pr ...

  6. 1439. Battle with You-Know-Who(splay树)

    1439 路漫漫其修远兮~ 手抄一枚splay树 长长的模版.. 关于spaly树的讲解   网上很多随手贴一篇 貌似这题可以用什么bst啦 堆啦 平衡树啦 等等 这些本质都是有共同点的 查找.删除特 ...

  7. 伸展树(Splay树)的简要操作

    伸展树(splay树),是二叉排序树的一种.[两个月之前写过,今天突然想写个博客...] 伸展树和一般的二叉排序树不同的是,在每次执行完插入.查询.删除等操作后,都会自动平衡这棵树.(说是自动,也就是 ...

  8. [Splay伸展树]splay树入门级教程

    首先声明,本教程的对象是完全没有接触过splay的OIer,大牛请右上角.. 首先引入一下splay的概念,他的中文名是伸展树,意思差不多就是可以随意翻转的二叉树 PS:百度百科中伸展树读作:BoGa ...

  9. hdu 3436 splay树+离散化*

    Queue-jumpers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

随机推荐

  1. 201421123042 《Java程序设计》第6周学习总结

    1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰 ...

  2. idea搭建springdata+mongodb+maven+springmvc

    idea搭建springdata+mongodb+maven+springmvc 今天我们来学习一下SpringData操作MongoDB. 项目环境:IntelliJ IDEA2017+maven3 ...

  3. [USACO13JAN] Seating

    https://www.luogu.org/problem/show?pid=3071 题目描述 To earn some extra money, the cows have opened a re ...

  4. python解释NTFS runlist的代码(文章转自北亚数据恢复张宇工程师)

    代码如下: 执行效果如下:root@zhangyu-VirtualBox:~/NTFS-5# python3 read_runlist.py mft_source.img ***参数数量或格式错误! ...

  5. Mybatis入门程序

    作为一个java的学习者,我相信JDBC是大家最早接触也是入门级别的数据库连接方式,所以我们先来回忆一下JDBC作为一种用于执行SQL语句的Java API是如何工作的.下面的一段代码就是最基本的JD ...

  6. Python-模块使用-Day6

    Python 之路 Day6 - 常用模块学习 本节大纲: 模块介绍time &datetime模块randomossysshutiljson & picleshelvexml处理ya ...

  7. 写一个vue组件

    写一个vue组件 我下面写的是以.vue结尾的单文件组件的写法,是基于webpack构建的项目.如果还不知道怎么用webpack构建一个vue的工程的,可以移步到vue-cli. 一个完整的vue组件 ...

  8. emqtt 试用(三)mqtt 知识

    一.概念 MQTT 协议客户端库: https://github.com/mqtt/mqtt.github.io/wiki/libraries 例如,mosquitto_sub/pub 命令行发布订阅 ...

  9. CentOS7.4下的 JDK1.8 安装

    一.卸载老的JDK 如果需要卸载OpenJDK,执行以下操作: [root@localhost ~]# rpm -e --nodeps tzdata-java-2014i-1.el7.noarch[r ...

  10. FatMouse's Speed ~(基础DP)打印路径的上升子序列

    FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take ...