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

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
题意:
有n个数1~n,两种操作,a b c表示将第a~b的数拿出来放到第c个数后面,a b表示翻转a~b之间的数
输出最终的数列
代码:
#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的更多相关文章

  1. HDU 3487 Splay

    给定两种操作,一种是把一个数列的某一段切下来插到剩余数列的某一个位置上. 一种是翻转操作,把数列的某一段进行翻转. 都是Splay的基本操作.标准的Rotateto调整出 [a,b]区间.然后对[a, ...

  2. hdu 1754 splay tree伸展树 初战(单点更新,区间属性查询)

    题意:与区间查询点更新,点有20W个,询问区间的最大值.曾经用线段树,1000+ms,今天的伸展树,890没ms,差不多. 第一次学习伸展树,一共花了2个单位时间,感觉伸展树真很有用,也很好玩.现在只 ...

  3. HDU 3487 Play with Chain(Splay)

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

  4. HDU 4453 Looploop (伸展树splay tree)

    Looploop Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. HDU 1890 Robotic Sort (splay tree)

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  6. splay tree旋转操作 hdu 1890

    很神奇的旋转操作. 目前没看到其他数据结构能实现这个功能.平衡树不好处理区间操作,线段树很难旋转.splay tree搞这个就很简单了. 下面用的这个模板跑了700ms,好慢,估计是删除操作太费时了, ...

  7. 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 ...

  8. HDU-3436 Queue-jumpers 树状数组 | Splay tree删除,移动

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3436 树状数组做法<猛戳> Splay tree的经典题目,有删除和移动操作.首先要离散化 ...

  9. HDU1890 Robotic Sort Splay tree反转,删除

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 题目中涉及数的反转和删除操作,需要用Splay tree来实现.首先对数列排序,得到每个数在数列 ...

随机推荐

  1. 机器人控制tcp通信参数调优

    机器人使用WiFi通信,实现指令下传,状态上传.而WiFi信道平时带宽较稳定,但会在某些时候突然中断,造成ping的延时较高,但可以马上恢复.如果一直ping,则一般情况下ping值很小,但长时间(数 ...

  2. 【转】redis安装与配置

    一.安装 1.官方:http://www.redis.cn/download.html 2.下载.解压.编译 wget http://download.redis.io/releases/redis- ...

  3. android4.3 Bluetooth分析之扫描分析

    android4.3中引入了蓝牙低能耗le(low energy),相应的也有一些方法/类.不过代码里,并没有找到初始调用的地方.所以这里还是先只分析下bt普通的扫描流程(类似android 4.2) ...

  4. Alpha发布文案+美工

    文案: Alpha发布文稿 我们是Hello World!团队,下面由我来简要介绍一下我们组的作品,我们组做的是一个飞机射击类游戏,名字叫做空天猎.这个游戏是基于JAVA平台创建的,那么接下来让我给大 ...

  5. YaoLingJump开发者日志(四)

      这么有意思的游戏没有剧情怎么行?开始剧情的搭建.   用到了LGame中的AVGScreen,确实是个好东西呢,只需要准备图片和对话脚本就行了.   经过不断的ps,yy,ps,yy,游戏开头的剧 ...

  6. 【Linux】- ps 命令

    Linux ps命令用于显示当前进程 (process) 的状态. 语法 ps [options] [--help] 参数: -A     列出所有的行程 -w    显示加宽可以显示较多的资讯 -a ...

  7. python Django框架接入微信公众平台

    1.在接入微信公众平台之前,需要在微信公众平台配置好基本信息,如下: 这个时候点击“提交”按钮,会提示“Token校验失败”,不要着急,这是必然会出现的现象,先不要退出页面,保留各项输入的数据,按第二 ...

  8. jconsole工具监控java运行情况

    jconsole是jdk自带的工具.所以要先安装jdk  1.jconsole工具的路径: 通过which jconsole来查看 /usr/local/jdk1.7.0_79/bin/jconsol ...

  9. Web服务器性能压力测试工具

    一.http_load 程序非常小,解压后也不到100K http_load以并行复用的方式运行,用以测试web服务器的吞吐量与负载. 但是它不同于大多数压力测试工具,它可以以一个单一的进程运行,一般 ...

  10. docker中crontab无法运行

    yum install -y crontabssed -ri 's/.*pam_loginuid.so/#&/' /etc/pam.d/crond   vi /etc/pam.d/crond ...