题目传送门

题目描述
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,3,...n,然后有两种操作,一种是将一段区间取下来再接到另一个位置,另一种是区间反转。
  不难看出这是一道平衡树的题目,题目中的区间反转操作很明显的是Splay,用下放标记的方法很容易实现。再看另一种操作,要求将一段数列截下来然后放入另一段中,这个其实可以旋转以后把要移动的一段直接取下,然后在重新调整平衡树,记录要放入的区间的右端点,再把要放入的区间的左端点作为树根,把截下来的一段直接放上去,再调整,把右端点也接上去就可以了。具体看代码。
 
  Code:
  

#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]的更多相关文章

  1. HDU--3487 Play with Chain (Splay伸展树)

    Play with Chain Problem Description YaoYao is fond of playing his chains. He has a chain containing ...

  2. HDU3487 Play with Chain splay 区间反转

    HDU3487 splay最核心的功能是将平衡树中的节点旋转到他的某个祖先的位置,并且维持平衡树的性质不变. 两个操作(数组实现) cut l,r, c把[l,r]剪下来放到剩下序列中第c个后面的位置 ...

  3. HDU-3487 Play with Chain Splay tee区间反转,移动

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3487 对于一个数列有两种操作:1.CUT a b c,先取出a-b区间的数,然后把它们放在取出后的第c ...

  4. HDU 3487 Play with Chain | Splay

    Play with Chain Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. HDU3487 play with chain

    题目大意:给出1到n的有序数列,现在有两个操作: 1.CUT a b c 把第a到第b个数剪切下来,放到剩下的第c个数的后边. 2.FLIP a b  把第a到第b个数反转. 经过总共m次操作后,求现 ...

  6. HDU3487 Play With Chains(Splay)

    很裸的Splay,抄一下CLJ的模板当作复习,debug了一个下午,收获是终于搞懂了以前看这个模板里不懂的内容.以前用这个模板的时候没有看懂为什么get函数返回的前缀要加个引用,经过一下午的debug ...

  7. 【HDU 3487】Play with Chain Splay

    题意 给定$n$个数序列,每次两个操作,将区间$[L,R]$拼接到去掉区间后的第$c$个数后,或者翻转$[L,R]$ Splay区间操作模板,对于区间提取操作,将$L-1$ Splay到根,再将$R+ ...

  8. hdu3487Play with Chain(splay)

    链接 简单的两种操作,一种删除某段区间,加在第I个点的后面,另一个是翻转区间.都是splay的简单操作. 悲剧一:pushdown时候忘记让lz=0 悲剧二:删除区间,加在某点之后的时候忘记修改其父亲 ...

  9. 【HDU3487】【splay分裂合并】Play with Chain

    Problem Description YaoYao is fond of playing his chains. He has a chain containing n diamonds on it ...

随机推荐

  1. 百度地图定位API,精度提高

    我使用百度定位API DEMO上面好像就可以setCoorType("bd09ll");//百度地图坐标. 然后我找了下从其它坐标体系迁移到百度坐标. 问下: 1.那我还能不能在百 ...

  2. centos7.2编译安装zabbix-3.0.4

    安装zabbix-3.0.4 #安装必备的包 yum -y install gcc* make php php-gd php-mysql php-bcmath php-mbstring php-xml ...

  3. mysql无法通过ip地址链接

    用BitNami搭建个站点(内含mysql数据库服务.phpmyadmin和Apache web Service), 用localhost或127.0.0.1及用户名密码连接没有问题.但是本机或远程通 ...

  4. 【Foreign】咏叹 [模拟退火]

    咏叹 Time Limit: 100 Sec  Memory Limit: 256 MB Description 有n根木棍,第i根长度为ai.你要贴着墙围出一个矩形区域,木棍围成的矩形边缘必须平行或 ...

  5. 大聊Python-----网络编程

    什么是Socket? socket本质上就是在2台网络互通的电脑之间,架设一个通道,两台电脑通过这个通道来实现数据的互相传递. 我们知道网络 通信 都 是基于 ip+port 方能定位到目标的具体机器 ...

  6. hdfs基本思想

    1.hdfs的优缺点 (1)不适合大量小文件存储: (2)不适合并发写入,不支持文件随机修改:(只能append追加) (3)不支持随机读等低延时的访问方式 2.基本思想 主从结构 主节点, name ...

  7. php中的base64写shell

    <?php system(base64_decode($_GET['info'])); #http://localhost/1.php?info=d2hvYW1p #这只是一个例子 ?>

  8. python中的ftplib模块

    前言 Python中默认安装的ftplib模块定义了FTP类. ftplib模块相关参数: 加载ftp模块:from ftplib import FTP ftp = FTP()#设置变量ftp.set ...

  9. SD卡spi读写流程

    SD卡spi读写流程 1.SD卡的命令格式: SD卡的指令由6字节(Byte)组成,如下: Byte1:0 1 x x x x x x(命令号,由指令标志定义,如CMD39为100111即16进制0x ...

  10. windows7 能连接移动硬盘 无法显示盘符

    右键点我的电脑,管理里,点磁盘管理,看盘认到没,有时候认到了但是没给盘符,需要自己手动给一个