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来实现.首先对数列排序,得到每个数在数列 ...
随机推荐
- 校招小白机考入坑之从键盘输入java的各种数据类型
//1.从键盘输入一个整型(其他基本类型类似) Scanner sc =new Scanner(System.in); sc.hasNextInt(); int str1 = sc.nextInt() ...
- 【RL系列】Multi-Armed Bandit笔记——UCB策略与Gradient策略
本篇主要是为了记录UCB策略与Gradient策略在解决Multi-Armed Bandit问题时的实现方法,涉及理论部分较少,所以请先阅读Reinforcement Learning: An Int ...
- SIG蓝牙mesh笔记3_网络结构
目录 3. Mesh Networking 3.1 Bearers 承载层 3.2 Network Layer 网络层 3.2.3 Address validity 地址有效性 3.2.4 Netwo ...
- HDU 2490 Parade(DPの单调队列)(2008 Asia Regional Beijing)
Description Panagola, The Lord of city F likes to parade very much. He always inspects his city in h ...
- Bower 显示‘bower ESUDO Cannot be run with sudo’的错误解决方法
使用 sudo 命令后或者当前用户为 root,执行 bower 相关命令会出现错误: 解决办法: 在命令后面加 --allow-root 例: bower init --allow-root bo ...
- 第三周的psp
PSP: 进度条: 累计进度图: 本周PSP饼状图:
- Thunder团队第一周 - Scrum会议4
Scrum会议4 小组名称:Thunder 项目名称:爱阅app Scrum Master:代秋彤 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传 ...
- UML设计(团队作业)
UML设计 一.团队信息 1.队名 读完文章再睡觉 2.团队成员的学号与姓名 学号 姓名 211606381 吴伟华(队长) 211606369 蔺皓雯 211606340 杨池宇 211606372 ...
- c#积累之测试
初来上班,免不了看别人代码.快速搞懂别人代码是我现在受到的一大挑战.寻摸着规律,发现一边进行调试,一边进行行行注释的逻辑判断不失为一种妙招. c#调试用的是vs2012.f11键和f10和f5键的应用 ...
- lintcode-187-加油站
187-加油站 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油gas[i],并且从第_i_个加油站前往第_i_+1个加油站需要消耗汽油cost[i]. 你有一辆油箱容量无限大的汽车,现在要从 ...