HDU--3487 Play with Chain (Splay伸展树)
Play with Chain
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?
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.
-1 -1
#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伸展树)的更多相关文章
- HDU 3487 Play with Chain | Splay
Play with Chain Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 3487 Play with Chain(Splay)
题目大意 给一个数列,初始时为 1, 2, 3, ..., n,现在有两种共 m 个操作 操作1. CUT a b c 表示把数列中第 a 个到第 b 个从原数列中删除得到一个新数列,并将它添加到新数 ...
- Splay伸展树学习笔记
Splay伸展树 有篇Splay入门必看文章 —— CSDN链接 经典引文 空间效率:O(n) 时间效率:O(log n)插入.查找.删除 创造者:Daniel Sleator 和 Robert Ta ...
- 【学时总结】◆学时·VI◆ SPLAY伸展树
◆学时·VI◆ SPLAY伸展树 平衡树之多,学之不尽也…… ◇算法概述 二叉排序树的一种,自动平衡,由 Tarjan 提出并实现.得名于特有的 Splay 操作. Splay操作:将节点u通过单旋. ...
- 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 ...
- Splay 伸展树
废话不说,有篇论文可供参考:杨思雨:<伸展树的基本操作与应用> Splay的好处可以快速分裂和合并. ===============================14.07.26更新== ...
- [Splay伸展树]splay树入门级教程
首先声明,本教程的对象是完全没有接触过splay的OIer,大牛请右上角.. 首先引入一下splay的概念,他的中文名是伸展树,意思差不多就是可以随意翻转的二叉树 PS:百度百科中伸展树读作:BoGa ...
- Splay伸展树入门(单点操作,区间维护)附例题模板
Pps:终于学会了伸展树的区间操作,做一个完整的总结,总结一下自己的伸展树的单点操作和区间维护,顺便给未来的自己总结复习用. splay是一种平衡树,[平均]操作复杂度O(nlogn).首先平衡树先是 ...
- Codeforces 675D Tree Construction Splay伸展树
链接:https://codeforces.com/problemset/problem/675/D 题意: 给一个二叉搜索树,一开始为空,不断插入数字,每次插入之后,询问他的父亲节点的权值 题解: ...
随机推荐
- ubuntu下,thinkphp验证码不能用
首先安装gd库 使用apt-get方式为Ubuntu安装PHP+MYSQL+Apache 默认是没有GD库的 1.安装gd库 命令:sudo apt-get install php5-gd 2.给权限 ...
- Nginx各个配置块功能详解
Nginx学习笔记-入门篇 nginx初探 ginx服务器是轻量级web服务器中广受好评的一款产品,常用功能有HTTP代理与反向代理(目前已支持七层与四层代理),负载均衡,web缓存. nginx配置 ...
- [置顶] Extjs4 异步刷新书的情况下 保持树的展开状态
前言:首先我觉得搞IT不管你是菜鸟还是特种兵,最重要的品质就是分享知识,因为可能在你这,这点知识不算什么,可是对于像我这样的菜鸟来说,无疑就可能会从中得到一点启发,甚至更大!此段代码,是我在某个网站上 ...
- CSU1659: Graph Center(最短路)
Description The center of a graph is the set of all vertices of minimum eccentricity, that is, the s ...
- tstring
是的,一旦知道 TCHAR 和_T 是如何工作的,那么这个问题很简单.基本思想是 TCHAR 要么是char,要么是 wchar_t,这取决于 _UNICODE 的值: // abridged f ...
- Qt install Phonon
sudo apt-get install libphonon-dev phonon-backend-gstreamer
- Linux系统中C&Cpp程序开发(一)
之前一直在Windows系统下进行程序的设计,近期开始学习使用Linux系统,因而打算将程序开发也转移到Linux系统下.今天先简单介绍一下该系统下的C程序开发步骤. 首先要预先安装vim和gcc工具 ...
- Tooltip(提示框)组件
一.加载方式 //class加载方式 <a href="http://www.ycku.com" title="这是一个提示信息!" class=&quo ...
- XAML 名称范围
XAML 名称范围存储 XAML 定义的对象名称和它们的对等实例之间的关系.此概念类似于其他编程语言和技术中的术语名称范围的更广泛的含义. 定义 XAML 名称范围的方式 XAML 名称范围中的名称使 ...
- linux 各种发行版及包管理器的关系
linux 各种发行版及包管理器的关系 Linux发行版列表 基于Kpkg(Debian 系) Debian GNU / Linux 及其派生发行版使用deb软件包格式,并使用dpkg及其前端作为包管 ...