Play with Chain

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
题意:FLIP操作就是区间的翻转,而CUT则是把某个区间移到某个点后面,本来打算用几次翻转来合成CUT函数,最后没有推出来。so借鉴kuangbin巨巨的作法
 #include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn = 3e5+;
int siz[maxn],pre[maxn],ch[maxn][],rev[maxn],key[maxn];
int tot,root,n,m;
void NewNode(int &r,int father,int k)
{
r = ++tot;
pre[r] = father;
ch[r][] = ch[r][] = ;
key[r] = k;
siz[r] = ;
rev[r] = ;
}
void update_rev(int r)
{
if (!r)
return ;
swap(ch[r][],ch[r][]);
rev[r] ^= ;
}
void push_up(int r)
{
siz[r] = siz[ch[r][]] + siz[ch[r][]] + ;
}
void push_down(int r)
{
if (rev[r])
{
update_rev(ch[r][]);
update_rev(ch[r][]);
rev[r] = ;
}
}
void build(int &x,int l,int r,int father)
{
if (l > r)
return;
int mid = (l + r) >> ;
NewNode(x,father,mid);
build(ch[x][],l,mid-,x);
build(ch[x][],mid+,r,x);
push_up(x);
}
void init()
{
root = tot = ;
NewNode(root,,-);
NewNode(ch[root][],root,-);
build(ch[ch[root][]][],,n,ch[root][]);
push_up(ch[root][]);
push_up(root);
} void Rotate(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]][] == y] = x;
pre[x] = pre[y];
ch[x][kind] = y;
pre[y] = x;
push_up(y);
} 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);
Rotate(r,ch[pre[r]][] == r);
}
else
{
int y = pre[r];
int kind = (ch[pre[y]][] == y);
push_down(pre[y]);
push_down(y);
push_down(r);
if (ch[y][kind] == r)
{
Rotate(y,!kind);
Rotate(r,!kind);
}
else
{
Rotate(r,kind);
Rotate(r,!kind);
}
}
}
push_up(r);
if (goal == )
root = r;
}
int Get_kth(int r,int k)
{
push_down(r);
int t = siz[ch[r][]] + ;
if (k == t)
return r;
if (k >= t)
return Get_kth(ch[r][],k-t);
else
return Get_kth(ch[r][],k);
}
void Reverse(int u,int v)
{
if (u > v)
return;
Splay(Get_kth(root,u),);
Splay(Get_kth(root,v+),root);
update_rev(ch[ch[root][]][]);
push_up(ch[root][]);
push_up(root);
}
void cut(int x,int y,int z) // cut函数本来想着用几次翻转来实现,推了半天没有推出来。so借鉴kuangbin巨巨的做法
{
Splay(Get_kth(root,x),);
Splay(Get_kth(root,y+),root);
int tmp = ch[ch[root][]][];
ch[ch[root][]][] = ;
push_up(ch[root][]);
push_up(root);
Splay(Get_kth(root,z+),);
Splay(Get_kth(root,z+),root);
ch[ch[root][]][] = tmp;
pre[ch[ch[root][]][]] = ch[root][];
push_up(ch[root][]);
push_up(root);
}
bool flag;
void dfs(int r)
{
if (!r)
return;
push_down(r);
dfs(ch[r][]);
if (r != - && key[r] != -)
{
printf(flag ? " %d":"%d",key[r]);
flag = ;
}
dfs(ch[r][]);
}
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
while (~scanf ("%d%d",&n,&m))
{
if (n < && m < )
break;
init();
for (int i = ; i < m; i++)
{
char op[];
int x,y,z;
scanf ("%s",op);
if (op[] == 'C')
{
scanf ("%d%d%d",&x,&y,&z);
cut(x,y,z);
}
else
{
scanf ("%d%d",&x,&y);
Reverse(x,y);
}
}
flag = ;
dfs(root);
printf("\n");
}
return ;
}

HDU--3487 Play with Chain (Splay伸展树)的更多相关文章

  1. HDU 3487 Play with Chain | Splay

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

  2. HDU 3487 Play with Chain(Splay)

    题目大意 给一个数列,初始时为 1, 2, 3, ..., n,现在有两种共 m 个操作 操作1. CUT a b c 表示把数列中第 a 个到第 b 个从原数列中删除得到一个新数列,并将它添加到新数 ...

  3. Splay伸展树学习笔记

    Splay伸展树 有篇Splay入门必看文章 —— CSDN链接 经典引文 空间效率:O(n) 时间效率:O(log n)插入.查找.删除 创造者:Daniel Sleator 和 Robert Ta ...

  4. 【学时总结】◆学时·VI◆ SPLAY伸展树

    ◆学时·VI◆ SPLAY伸展树 平衡树之多,学之不尽也…… ◇算法概述 二叉排序树的一种,自动平衡,由 Tarjan 提出并实现.得名于特有的 Splay 操作. Splay操作:将节点u通过单旋. ...

  5. hdu 3487 Play with Chain

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3487 YaoYao is fond of playing his chains. He has a c ...

  6. Splay 伸展树

    废话不说,有篇论文可供参考:杨思雨:<伸展树的基本操作与应用> Splay的好处可以快速分裂和合并. ===============================14.07.26更新== ...

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

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

  8. Splay伸展树入门(单点操作,区间维护)附例题模板

    Pps:终于学会了伸展树的区间操作,做一个完整的总结,总结一下自己的伸展树的单点操作和区间维护,顺便给未来的自己总结复习用. splay是一种平衡树,[平均]操作复杂度O(nlogn).首先平衡树先是 ...

  9. Codeforces 675D Tree Construction Splay伸展树

    链接:https://codeforces.com/problemset/problem/675/D 题意: 给一个二叉搜索树,一开始为空,不断插入数字,每次插入之后,询问他的父亲节点的权值 题解: ...

随机推荐

  1. DataGrid横向滚动条无法拖动的问题

    项目中经常遇到一些问题,这些问题可能很简单,但是之前从未遇到,可能经过了一番谷歌,也可能是查阅了MSDN,或是借鉴了大牛博客,逐渐有了些眉目,为了将这些东西落地,也为了将来之不易的东西记录下来,以备今 ...

  2. 用PlistBuddy修改Plist文件

    Plist文件是以.plist为结尾的文件的总称. 众所周知, Plist在Mac OSX系统中起着举足轻重的作用,就如同Windows里面的Registry一样,系统和程序使用Plist文件来存储自 ...

  3. html+jq实现简单的图片轮播

    今天上班没事,就自己琢磨着写一下图片轮播,可是没想到,哈哈竟然写出来啦,下面就贴出来代码,作为纪念保存下下哈: <body style="text-align: center;&quo ...

  4. 文件操作2 cp mv rm

    1.cp命令 [root@rusky /]# cp 123 /test  #在linux系统中,如果文件123已经存在,则提示用户确认,在unix系统中则不提示,除非使用参数-i 交互式操作.cp: ...

  5. 图形混合模式 PorterDuff Xfermodes

    16种图形混合模式示例 首先绘制Dst(黄色的),然后绘制Src(蓝色的) 问题来了:为何还会有部分黄色?不应该是把src和dst都清除了吗? 图形混合模式简介 ProterDuff的含义 Prote ...

  6. DataGrid( 数据表格) 组件[1]

    本节课重点了解 EasyUI 中 DataGrid(数据表格)组件的使用方法,这个组件依赖于Panel(面板).Resizeable(调整大小).LinkButton(按钮).Pageination( ...

  7. 黑马程序员——HTML语言

    ------<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS ...

  8. C#6.0语法糖

    using System; using static System.Math;//using static,仅仅引入类中的静态方法 namespace _6._0Syntax { class Prog ...

  9. ReportViewer导出功能筛选

    ReportViewer只能导出Excel,把导出Word和PDF功能去掉 <rsweb:ReportViewer ID="ReportViewer1" runat=&quo ...

  10. 反射那些事儿——Java动态装载和反射技术

    一直以来反射都是只闻其声,却无法将之使用,近日尽心下来学习下,发现了很多精妙之处. Java动态装载和反射技术 一.类的动态装载 1.Java代码编译和执行的整个过程包含了以下三个重要的机制: ● J ...