hdu 3487Play with Chain(Splay)
来源:互联网 发布:西安交大网络教育学费 编辑:程序博客网 时间:2024/04/29 22:38
Play with Chain
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5815 Accepted Submission(s): 2356
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?
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.
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 2CUT 3 5 4FLIP 2 6-1 -1
Sample Output
1 4 3 7 6 2 5 8
Source
2010 ACM-ICPC Multi-University Training Contest(5)——Host by BJTU
Recommend
zhengfeng
#include<iostream>#include<algorithm>#include<cstdio>#define MAXN 600005#define inf 1<<29#define key_value (ch[ch[root][1]][0])int n,m,q,a[MAXN];int rev[MAXN],size[MAXN],pre[MAXN],ch[MAXN][2],val[MAXN];int tot1,tot2,root,s[MAXN];using namespace std;void newnode(int &r,int k,int father){ if(tot2) r=s[tot2--]; else r=++tot1; ch[r][0]=ch[r][1]=0; pre[r]=father; val[r]=k; size[r]=1;}void push_up(int x){ int l=ch[x][0],r=ch[x][1]; size[x]=size[l]+size[r]+1;}void push_down(int r){ if(rev[r]) { swap(ch[r][0],ch[r][1]); rev[ch[r][0]]^=1; rev[ch[r][1]]^=1; rev[r]=0;}}void build(int &r,int L,int R,int father){ if(L>R) return; int mid=(L+R)/2; newnode(r,a[mid],father); build(ch[r][0],L,mid-1,r); build(ch[r][1],mid+1,R,r); push_up(r);}void Init(){ tot1=tot2=root=0; ch[root][0]=ch[root][1]=pre[root]=rev[root]=size[root]=0; newnode(root,inf,0); newnode(ch[root][1],inf,root); push_up(root); build(key_value,1,n,ch[root][1]); push_up(ch[root][1]); 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]][1]==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) rotate(r,ch[pre[r]][0]==r); else { int y=pre[r]; int kind=(ch[pre[y]][0]==y); if(ch[y][kind]==r) { rotate(r,!kind); rotate(r,kind); } else { rotate(y,kind); rotate(r,kind); } } } push_up(r); if(goal==0) root=r;}int get_kth(int r,int k){ push_down(r); int t=size[ch[r][0]]+1; if(t==k) return r; if(t>k) return get_kth(ch[r][0],k); else return get_kth(ch[r][1],k-t);}int get_min(int r){ push_down(r); while(ch[r][0]) { r=ch[r][0]; push_down(r); } return r;}void reversal(int l,int r){ int x=get_kth(root,l); Splay(x,0); int y=get_kth(root,r+2); Splay(y,root); rev[key_value]^=1;}void revolve(int a,int b,int c){ int x=get_kth(root,a); Splay(x,0); x=get_kth(root,b+2); Splay(x,root); int tmp=key_value; key_value=0; push_up(ch[root][1]); push_up(root); Splay(get_kth(root,c+1),0); Splay(get_kth(root,c+2),root); key_value=tmp; pre[key_value]=ch[root][1]; push_up(ch[root][1]); push_up(root);}int cnt;void print(int r){if(!r)return;push_down(r);print(ch[r][0]);if(val[r]!=inf){if(cnt==n-1)printf("%d",val[r]);elseprintf("%d ",val[r]);cnt++;}print(ch[r][1]);}int main(){ while(scanf("%d %d",&n,&m)!=EOF) { cnt=0; if(n==-1&&m==-1) break; for(int i=1;i<=n;i++) a[i]=i; Init(); string str; for(int i=1;i<=m;i++) { cin>>str; if(str=="CUT") { int a,b,c; scanf("%d%d%d",&a,&b,&c); revolve(a,b,c); } else { int l,r; scanf("%d%d",&l,&r); reversal(l,r); } } print(root); printf("\n"); } return 0;}
0 0
- 【HDU 3487】Play with Chain(Splay)
- hdu 3487Play with Chain(Splay)
- HDU 3487 Play with Chain(Splay)
- HDU 3487 Play with Chain (Splay tree)
- hdu 3487 Play with Chain(splay)
- hdu 3487 Play with Chain(Splay)
- HDU 3487 Play with Chain(Splay)
- hdu-3487-Play with Chain (Splay tree)
- hdu 3487 Play with Chain splay tree
- 【HDU】3487 Play with Chain 【splay】
- hdu 3487 Play with Chain splay
- hdu 3487 Play with Chain splay tree
- HDU 3487 Play with Chain [splay]
- hdu 3487 Play with Chain splay
- 【HDU】3487 Play with Chain Splay
- hdu 3487 Play with Chain(splay tree)
- HDU 3487——Play with Chain(splay tree)
- HDU 3487 Play with Chain(Splay 经典操作)
- javascript正则学习——给自己温故用
- 序列点
- 关于struts2.5.2 初步学习 遇到的几点改变以及使用通配符调用无效的问题
- C++控制输出的字体颜色
- QT数据库学习和以连接Access为例
- hdu 3487Play with Chain(Splay)
- java编程思想读书笔记 第十二章 通过异常处理错误(下)
- hdu 2768 Cat vs. Dog 最大独立集
- 2016.10.17 杂思
- 51nod-1065:最小正子段和
- 标题栏显示与隐藏
- Codeforces 416C Booking System [贪心]
- 使用Groovy XmlParser 解析XML示例
- 死锁