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 ...
随机推荐
- Mybatis 参考
1:Mybatis最入门---ResultMaps基本用法 2:Mybatis最入门---ResultMaps高级用法(上) 3:Mybatis最入门---ResultMaps高级用法(下) 4:My ...
- jQuery对象初始化的传参方式
jQuery对象初始化的传参方式包括: 1.$(DOMElement) 2.$(' ... '), $('#id'), $('.class') 传入字符串, 这是最常见的形式, 这种传参数经常也传入第 ...
- 使用python脚本配置zabbix发送报警邮件
#前提得在zabbix_server配置文件中配置触发脚本的目录,例如,我配置的在/usr/local/zabbix/server/scripts目录下 编写python脚本如下 因为我的服务器在腾讯 ...
- 【bzoj3387-跨栏训练】线段树+dp
我们可以想到一个dp方程:f[i][0]表示当前在i个栅栏的左端点,f[i][1]表示在右端点. 分两种情况: 第一种:假设现在要更新线段gh的左端点g,而它下来的路径被ef挡住了,那么必定是有ef来 ...
- 「6月雅礼集训 2017 Day4」寻找天哥
[题目大意] 给出$n$个三维向量,设当前向量长度为$L$,每次沿着向量等概率走$[0,L]$个长度.一个球每秒半径增加1个长度,直到覆盖位置,每秒耗能为球体积,求总耗能的期望. 设最后半径为R,那么 ...
- 51nod1245 Binomial Coefficients Revenge
题目来源: HackerRank 基准时间限制:2 秒 空间限制:131072 KB 分值: 640 C(M,N) = M! / N! / (M - N)! (组合数).给出M和质数p,求C(M,0 ...
- python基础===装饰器@property 的扩展
以下来自Python 3.6.0 Document: class property(fget=None, fset=None, fdel=None, doc=None) Return a proper ...
- interrupted()和isInterrupted()比较+终止线程的正确方法+暂停线程
interrupted():测试当前线程[运行此方法的当前线程]是否已经是中断状态,执行后具有将状态标志清除为false的功能. isInterrupted():测试线程对象是否已经是中断状态,但不清 ...
- 2015多校第6场 HDU 5361 并查集,最短路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...
- linux的基本的命令行操作
linux的基本的命令行操作 第一步前登陆你的服务器 //创建文件夹的方法 mkdir 文件名 //进入指定文件夹 cd 文件名 //查看文件夹下的内容 ls or ll // 查看当前的路径 pwd ...