HDU 3487 Splay tree
Play with Chain
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6779 Accepted Submission(s): 2678
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.
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define Key_Value ch[ch[root][1]][0]
const int maxn=;
int n,q;
int pre[maxn],ch[maxn][],rev[maxn],size[maxn],key[maxn],root,tot1;
void Update_Rev(int r){
if(r==) return;
swap(ch[r][],ch[r][]);
rev[r]^=;
}
void Push_Down(int r){
if(rev[r]){
Update_Rev(ch[r][]);
Update_Rev(ch[r][]);
rev[r]=;
}
}
void Push_Up(int r){
size[r]=+size[ch[r][]]+size[ch[r][]];
}
void New_Node(int &r,int fa,int k){
r=++tot1;
pre[r]=fa;
key[r]=k;
rev[r]=ch[r][]=ch[r][]=;
size[r]=;
}
void Build(int &x,int l,int r,int fa){
if(l>r) return;
int mid=(l+r)>>;
New_Node(x,fa,mid);
Build(ch[x][],l,mid-,x);
Build(ch[x][],mid+,r,x);
Push_Up(x);
}
void Init(){
root=tot1=;
key[root]=ch[root][]=ch[root][]=size[root]=rev[root]=pre[root]=;
New_Node(root,,-);
New_Node(ch[root][],root,-);
Build(Key_Value,,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){
while(pre[r]!=goal){
if(pre[pre[r]]==goal)
Rotate(r,ch[pre[r]][]==r);
else{
int y=pre[r];
int kind=ch[pre[y]][]==y;
if(ch[y][kind]==r){
Rotate(r,!kind);
Rotate(r,kind);
}else{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
Push_Up(r);
if(goal==) root=r;
}
int Get_Kth(int r,int k){
Push_Down(r);
int t=size[ch[r][]];
if(k==t+) return r;
else if(k<=t) return Get_Kth(ch[r][],k);
else return Get_Kth(ch[r][],k-t-);
}
void CUT(int a,int b,int c){
Splay(Get_Kth(root,a),);
Splay(Get_Kth(root,b+),root);
int temp=Key_Value;
Key_Value=;
Push_Up(ch[root][]);
Push_Up(root); Splay(Get_Kth(root,c+),);
Splay(Get_Kth(root,c+),root);
Key_Value=temp;
pre[Key_Value]=ch[root][];
Push_Up(ch[root][]);
Push_Up(root);
}
void REVERSE(int l,int r){
Splay(Get_Kth(root,l),);
Splay(Get_Kth(root,r+),root);
Update_Rev(Key_Value);
Push_Up(ch[root][]);
Push_Up(root);
}
int cnt;
void print(int r){
if(r==) return;
Push_Down(r);
print(ch[r][]);
if(cnt<n&&key[r]>){
cnt++;
printf("%d%c",key[r],cnt==n?'\n':' ');
}
print(ch[r][]);
}
int main()
{
while(scanf("%d%d",&n,&q)==){
if(n<&&q<) break;
Init();
char op[];
int x,y,z;
while(q--){
scanf("%s",op);
if(op[]=='C'){
scanf("%d%d%d",&x,&y,&z);
CUT(x,y,z);
}
else if(op[]=='F'){
scanf("%d%d",&x,&y);
REVERSE(x,y);
}
}
cnt=;
print(root);
}
return ;
}
HDU 3487 Splay tree的更多相关文章
- HDU 3487 Splay
给定两种操作,一种是把一个数列的某一段切下来插到剩余数列的某一个位置上. 一种是翻转操作,把数列的某一段进行翻转. 都是Splay的基本操作.标准的Rotateto调整出 [a,b]区间.然后对[a, ...
- hdu 1754 splay tree伸展树 初战(单点更新,区间属性查询)
题意:与区间查询点更新,点有20W个,询问区间的最大值.曾经用线段树,1000+ms,今天的伸展树,890没ms,差不多. 第一次学习伸展树,一共花了2个单位时间,感觉伸展树真很有用,也很好玩.现在只 ...
- HDU 3487 Play with Chain(Splay)
题目大意 给一个数列,初始时为 1, 2, 3, ..., n,现在有两种共 m 个操作 操作1. CUT a b c 表示把数列中第 a 个到第 b 个从原数列中删除得到一个新数列,并将它添加到新数 ...
- HDU 4453 Looploop (伸展树splay tree)
Looploop Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 1890 Robotic Sort (splay tree)
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- splay tree旋转操作 hdu 1890
很神奇的旋转操作. 目前没看到其他数据结构能实现这个功能.平衡树不好处理区间操作,线段树很难旋转.splay tree搞这个就很简单了. 下面用的这个模板跑了700ms,好慢,估计是删除操作太费时了, ...
- 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 ...
- HDU-3436 Queue-jumpers 树状数组 | Splay tree删除,移动
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3436 树状数组做法<猛戳> Splay tree的经典题目,有删除和移动操作.首先要离散化 ...
- HDU1890 Robotic Sort Splay tree反转,删除
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 题目中涉及数的反转和删除操作,需要用Splay tree来实现.首先对数列排序,得到每个数在数列 ...
随机推荐
- CodeForces 838B Diverging Directions 兼【20180808模拟测试】t3
描述 给你一个图,一共有 N 个点,2*N-2 条有向边. 边目录按两部分给出 1. 开始的 n-1 条边描述了一颗以 1 号点为根的生成树,即每个点都可以由 1 号点到达. 2. 接下来的 N-1 ...
- 详解Jedis连接池报错处理
在使用Jedis连接池模式下,比较常见的报错如下: redis.clients.jedis.exceptions.JedisConnectionException:Could not get a re ...
- Ext JS 6学习文档-第5章-表格组件(grid)
Ext JS 6学习文档-第5章-表格组件(grid) 使用 Grid 本章将探索 Ext JS 的高级组件 grid .还将使用它帮助读者建立一个功能齐全的公司目录.本章介绍下列几点主题: 基本的 ...
- SPOJ 8073 The area of the union of circles(计算几何の圆并)(CIRU)
Description You are given N circles and expected to calculate the area of the union of the circles ! ...
- ZOJ 2760 How Many Shortest Path(最短路径+最大流)
Description Given a weighted directed graph, we define the shortest path as the path who has the sma ...
- apache不解析php文件遍历目录
程序目录下有index.php缺不能正常解析,直接刷出整个目录. 解决:在后面添加index.php的解析即可.. DirectoryIndex index.html index.html.var i ...
- Java学习个人备忘录之关键字static
被static标记的东西会放在内存中被共享的,对象用到时,就会来取的. class Person { String name; //成员变量,实例变量 static String country = ...
- NIO初探
NIO的前世今生 NIO又叫NonBlockingI/O,即非阻塞I/O.以此对应的,有一个更常见的IO(BIO),又叫Blocking I/O,即阻塞IO,两种都为Java的IO实现方案. NIO/ ...
- 对编码内容多次UrlDecode
对编码内容多次UrlDecode,并不会影响最终结果. 尝试阅读了微软的源代码,不过不容易读懂. 网址:https://referencesource.microsoft.com/#System/ne ...
- 对alpha发布的总结技术随笔
对于今天的alpha发布,首先需要自我检讨,因为我们组没有展示作品.主要的原因还是我们投入的时间不足.我们的项目是约跑App,首先选择做安卓平台的东西,我们大家都需要熟悉新的开发软件Android S ...