Play with Chain

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

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
 

Source

 
 

分析

splay功能,区间旋转,区间截取及插入。

区间截取及插入

1、截取区间[a, b]

把第a-1个数旋转到根,把第b+1个数旋转到根的右儿子,那么b+1的左儿子就是所要截取的区间,把b的左儿子记录下即可,更新。

2、插入一段区间到第c个数后

把第c个数旋转到根,把第c+1个数旋转到根的右儿子,那么b+1的左儿子一定是空的,然后将要插入的区间的根节点赋值给b的左儿子即可,更新。

区间翻转:

翻转区间[a, b]

把第a-1个数旋转到根,把第b+1个数旋转到根的右儿子,那么b+1的左儿子就是所要截取的区间,打个标记即可,用到了再下传。

code

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream> using namespace std; const int N = ; int ch[N][],fa[N],tag[N],siz[N],data[N];
int Root,n,m,cnt; inline int read() {
int x = ,f = ;char ch = getchar();
for (; ch<''||ch>''; ch = getchar()) if (ch=='-') f = -;
for (; ch>=''&&ch<=''; ch = getchar()) x = x * + ch - '';
return x * f;
}
inline void pushup(int x) {
siz[x] = siz[ch[x][]] + siz[ch[x][]] + ;
}
inline void pushdown(int x) {
if (tag[x]) {
tag[ch[x][]] ^= ;tag[ch[x][]] ^= ;
swap(ch[x][],ch[x][]);
tag[x] ^= ;
}
}
inline int son(int x) {
return x == ch[fa[x]][];
}
inline void rotate(int x) {
int y = fa[x],z = fa[y],b = son(x),c = son(y),a = ch[x][!b];
if (z) ch[z][c] = x;else Root = x;fa[x] = z;
ch[x][!b] = y;fa[y] = x;
ch[y][b] = a;if (a) fa[a] = y;
pushup(y);pushup(x);
}
inline void splay(int x,int rt) {
while (fa[x] != rt) {
int y = fa[x],z = fa[y];
if (z==rt) rotate(x);
else {
if (son(x) == son(y)) rotate(y),rotate(x);
else rotate(x),rotate(x);
}
}
}
inline int getkth(int k) {
int p = Root;
while (true) {
pushdown(p);
if (siz[ch[p][]] + == k) return p;
if (ch[p][] && k <= siz[ch[p][]] ) p = ch[p][];
else {
k -= ((ch[p][] ? siz[ch[p][]] : ) + );
p = ch[p][];
}
}
}
inline void rever(int l,int r) {
int L = getkth(l),R = getkth(r + );
splay(L,);splay(R,L);
tag[ch[R][]] ^= ;
}
inline void cut(int l,int r,int p) {
int L = getkth(l),R = getkth(r+);
splay(L,);splay(R,L);
int tmp = ch[R][];
fa[tmp] = ;ch[R][] = ;
pushup(R);pushup(L);
L = getkth(p+),R = getkth(p+);
splay(L,);splay(R,L);
fa[tmp] = R;ch[R][] = tmp;
pushup(R);pushup(L);
}
int build(int l,int r) {
if (l > r) return ;
int mid = (l + r) >> ;
int t = build(l,mid-);
ch[mid][] = t;fa[t] = mid;
t = build(mid+,r);
ch[mid][] = t;fa[t] = mid;
pushup(mid);
return mid;
}
void print(int x) {
if (!x) return ;
pushdown(x);
print(ch[x][]);
if (data[x] > && data[x] < n+) {
if (cnt==) printf("%d",data[x]),cnt = ;
else printf(" %d",data[x]);
}
print(ch[x][]);
}
inline void init() {
Root = cnt = ;
memset(ch,,sizeof(ch));
memset(fa,,sizeof(fa));
memset(tag,,sizeof(tag));
memset(siz,,sizeof(siz));
memset(data,,sizeof(data));
}
int main() {
int a,b,c;
char s[];
while (scanf("%d%d",&n,&m)!=EOF && !(n==-&&m==-)) {
init();
for (int i=; i<=n+; ++i) data[i] = i-;
Root = build(,n+);
while (m--) {
scanf("%s",s);
if (s[]=='C') {
a = read(),b = read(),c = read();
cut(a,b,c);
}
else {
a = read(),b = read();
rever(a,b);
}
}
print(Root);
printf("\n");
}
return ;
}

最新文章

  1. JAVA多态的定义
  2. ThinkPHP 3.2.3 自动加载公共函数文件的方法
  3. redis 持久化 如果 AOF 文件出错了,怎么办?
  4. dubbo通信协议之对比
  5. yii中rights安装
  6. 访问项目时,不能自动加载index.php文件
  7. C#集合之字典
  8. C# 三层架构之系统的登录验证与添加数据的实现
  9. Java并发编程之显式锁机制
  10. W3C的标准到底是啥?
  11. .net core 2.0 webapi部署iis操作
  12. hadoop HDFS常用文件操作命令
  13. 洛谷P1829 [国家集训队]Crash的数字表格
  14. Python 合并两个列表的多种方式,合并两个字典的多种方式
  15. css世界的学习笔记
  16. JsonWebToken Demo(转)
  17. Spring Security使用报错 No bean named &#39;springSecurityFilterChain&#39; is defined
  18. 【读书笔记】iOS-基带攻击
  19. Google TensorFlow 机器学习框架介绍和使用
  20. ORACLE-SQL(二)

热门文章

  1. NIO基础之Buffer
  2. 集合(Map、可变参数、Collections)
  3. spring事务的开启方式(编程式和声明式)
  4. 关于原生javascript的this,this真是个强大的东东
  5. 实现memcopy函数
  6. gd调试命令,gdb调试core文件
  7. Python3+Selenium3+webdriver学习笔记14(等待判断 鼠标事件 )
  8. python+selenium之中类/函数/模块的简单介绍和方法调用
  9. 参考消息 Android 读报
  10. Web端 session cookies Application viewstate