HDU3487 Play With Chain [Splay]
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?
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.
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iomanip>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=3e5+;
int n,m,root,tot;
int cnt,ans[N];
struct Node{
int ch[],val,fa;
int size,mark;
void add(int x,int father){
mark=ch[]=ch[]=;
fa=father;val=x;size=;}
}t[N];
inline int read()
{
char ch=getchar();int num=;bool flag=false;
while(ch<''||ch>''){if(ch=='-')flag=true;ch=getchar();}
while(ch>=''&&ch<=''){num=num*+ch-'';ch=getchar();}
return flag?-num:num;
}
inline void pushup(int x)
{
int l=t[x].ch[],r=t[x].ch[];
t[x].size=t[l].size+t[r].size+;
}
inline void pushdown(int x)
{
if(t[x].mark){
t[t[x].ch[]].mark^=;
t[t[x].ch[]].mark^=;
swap(t[x].ch[],t[x].ch[]);
t[x].mark=;}
}
inline void rotate(int x)
{
int y=t[x].fa;
int z=t[y].fa;
int k=(t[y].ch[]==x);
t[z].ch[t[z].ch[]==y]=x;
t[x].fa=z;
t[y].ch[k]=t[x].ch[k^];
t[t[x].ch[k^]].fa=y;
t[x].ch[k^]=y;
t[y].fa=x;
pushup(y);pushup(x);
}
inline void splay(int x,int tag)
{
while(t[x].fa!=tag){
int y=t[x].fa;
int z=t[y].fa;
if(z!=tag)
(t[y].ch[]==x)^(t[z].ch[]==y)?
rotate(x):rotate(y);
rotate(x);}
if(tag==)root=x;
}
inline void insert(int x)
{
int now=root,fa=;
while(now)
fa=now,now=t[now].ch[x>t[now].val];
now=++tot;
if(fa)t[fa].ch[x>t[fa].val]=now;
t[now].add(x,fa);
splay(now,);
}
inline int find(int x)
{
int now=root;
while(){
pushdown(now);
if(t[t[now].ch[]].size>=x)now=t[now].ch[];
else if(t[t[now].ch[]].size+==x)return now;
else x-=(t[t[now].ch[]].size+),now=t[now].ch[];
}
}
inline int getmax(int x)
{
pushdown(x);
while(t[x].ch[]){
x=t[x].ch[];
pushdown(x);}
return x;
}
inline void merge(int x,int y)
{
t[x].ch[]=y;
t[y].fa=x;
}
inline void flip(int l,int r)
{
l=find(l),r=find(r+);
splay(l,);splay(r,l);
t[t[t[root].ch[]].ch[]].mark^=;
}
inline void cut(int x,int y,int z)
{
int l=find(x),r=find(y+);
splay(l,);splay(r,l);
int root1=t[t[root].ch[]].ch[];
t[t[root].ch[]].ch[]=;
pushup(t[root].ch[]);
pushup(root);
int neo=find(z+);
splay(neo,);
int root2=t[root].ch[];
merge(root,root1);
pushup(root);
int maxx=getmax(root);
splay(maxx,);
merge(root,root2);
pushup(root);
}
inline void travel(int now)
{
pushdown(now);
if(t[now].ch[])travel(t[now].ch[]);
if(t[now].val>&&t[now].val<n+)
ans[++cnt]=t[now].val-;
if(t[now].ch[])travel(t[now].ch[]);
}
int main()
{
while(){
n=read();m=read();
if(n<||m<)break;
root=tot=;
for(int i=;i<=n+;i++)
insert(i);
char opt[];int x,y,z;
for(int i=;i<=m;i++){
scanf("%s",opt);
if(opt[]=='F'){
x=read();y=read();
flip(x,y);}
else{
x=read();y=read();z=read();
cut(x,y,z);}
}
cnt=;
travel(root);
for(int i=;i<n;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[n]);
}
return ;
}
HDU3487 Play With Chain [Splay]的更多相关文章
- HDU--3487 Play with Chain (Splay伸展树)
Play with Chain Problem Description YaoYao is fond of playing his chains. He has a chain containing ...
- HDU3487 Play with Chain splay 区间反转
HDU3487 splay最核心的功能是将平衡树中的节点旋转到他的某个祖先的位置,并且维持平衡树的性质不变. 两个操作(数组实现) cut l,r, c把[l,r]剪下来放到剩下序列中第c个后面的位置 ...
- HDU-3487 Play with Chain Splay tee区间反转,移动
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3487 对于一个数列有两种操作:1.CUT a b c,先取出a-b区间的数,然后把它们放在取出后的第c ...
- HDU 3487 Play with Chain | Splay
Play with Chain Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU3487 play with chain
题目大意:给出1到n的有序数列,现在有两个操作: 1.CUT a b c 把第a到第b个数剪切下来,放到剩下的第c个数的后边. 2.FLIP a b 把第a到第b个数反转. 经过总共m次操作后,求现 ...
- HDU3487 Play With Chains(Splay)
很裸的Splay,抄一下CLJ的模板当作复习,debug了一个下午,收获是终于搞懂了以前看这个模板里不懂的内容.以前用这个模板的时候没有看懂为什么get函数返回的前缀要加个引用,经过一下午的debug ...
- 【HDU 3487】Play with Chain Splay
题意 给定$n$个数序列,每次两个操作,将区间$[L,R]$拼接到去掉区间后的第$c$个数后,或者翻转$[L,R]$ Splay区间操作模板,对于区间提取操作,将$L-1$ Splay到根,再将$R+ ...
- hdu3487Play with Chain(splay)
链接 简单的两种操作,一种删除某段区间,加在第I个点的后面,另一个是翻转区间.都是splay的简单操作. 悲剧一:pushdown时候忘记让lz=0 悲剧二:删除区间,加在某点之后的时候忘记修改其父亲 ...
- 【HDU3487】【splay分裂合并】Play with Chain
Problem Description YaoYao is fond of playing his chains. He has a chain containing n diamonds on it ...
随机推荐
- table第一行合并,其余行宽度失效问题
<col>标签 http://www.w3school.com.cn/tags/tag_col.asp 由于表格中有一行合并过,所以其它没有合并的行的列宽就会平均化,对列宽的设置也会失效. ...
- 2017-2018-2 20179207 《网络攻防技术》python简明教程(1-10)
Python3简明教程(一) 开始python之旅 使用交互模式的 Python3解释器 简单使用 vim 编写 Python3 脚本 执行 Python3 脚本 Python3 代码风格建议 Pyt ...
- 关于MyBatis一些小错误,元素内容必须由格式正确的字符数据或标记组成.
今天在Mapper.xml文件写查询语句报了个奇怪的错误 Caused by: org.apache.ibatis.builder.BuilderException: Error creating d ...
- [Luogu 3966] TJOI 2013 单词
经典ACAM. 注意单词之间添加字符,以及对重复单词的处理. #include <cstdio> #include <cstring> #include <queue&g ...
- Asp.net Web Api 2 FORM Authentication Demo
最近看了一点 web api 2方面的书,对认证都是简单介绍了下,所以我在这里做个简单Demo,本文主要是FORM Authentication,顺带把基本认证也讲了. Demo 一.FORM Aut ...
- 【BZOJ1085】【SCOI2005】骑士精神 [A*搜索]
骑士精神 Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Description 在一个5×5的棋盘上有12个白色的 ...
- 【51NOD】斜率最大
[题解]通过画图易得结论:最大斜率一定出现在相邻两点之间. #include<cstdio> #include<algorithm> #include<cstring&g ...
- 伪病毒 Rp_test
第一个写的对电脑有破坏性的程序= =,然后发现写system的copy的时候不会用字符串替代路径,然后就萎了= =,只能写一个没有自身复制的伪病毒了,坑到了好多同学的电脑,23333.... //By ...
- perl6中的q/qq/qx/qqx
q不内插 qq内插 qx不内插 qqx内插
- win10远程桌面配置
Win10连接远程桌面的时候提示您的凭证不工作该怎么办? http://www.cnblogs.com/zhuimengle/p/6048128.html 二.服务器端 1.依旧进入组策略,不过是在服 ...