hdu3487 Play with Chain(splay)
来源:互联网 发布:黄子华 知乎 编辑:程序博客网 时间:2024/05/18 18:44
Play with Chain
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2720 Accepted Submission(s): 1103
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
题目大意:对编号1-n的n个数,2种操作,CUT a,b,c表示将原序列中[a,b]区间的数剪切下来,贴在剩下的序列中的第c个数字之后。FLIP a,b表示将[a,b]区间翻转。
求m次操作后的序列。
题目分析:区间翻转好说,关键有区间的剪切操作,这时候伸展树强大的区间操作优势就体现出来了。取反操作也可以加lazy标记。
这是写的第二个splay,调了一下午。。。
代码比较长:
#include <iostream>#include<cstdio>#include<cstring>using namespace std;const int N = 300005;struct node{ int l,r,f,size,lazy,key;}tree[N];int next[N<<1];int m,n;int cnt;void init(){ tree[0].f = tree[0].size = 0; for(int i = 0;i < N - 1;i ++) next[i] = i + 1;}int newnode(int key){ int p = next[0]; next[0] = next[p]; tree[p].l = tree[p].f = tree[p].r = tree[p].lazy = 0; tree[p].size = 1; tree[p].key = key; return p;}void push_up(int rt){ if(rt) tree[rt].size = tree[tree[rt].l].size + tree[tree[rt].r].size + 1;}void push_down(int rt){ if(tree[rt].lazy) { swap(tree[rt].l,tree[rt].r); tree[tree[rt].l].lazy ^= 1; tree[tree[rt].r].lazy ^= 1; tree[rt].lazy = 0; }}void zig(int x){ push_down(x); int p = tree[x].f; bool lf; if(tree[p].f) { if(tree[tree[p].f].l == p) lf = true; else lf = false; } tree[p].l = tree[x].r; if(tree[x].r) tree[tree[x].r].f = p; push_up(p); tree[x].r = p; tree[x].f = tree[p].f; push_up(x); tree[p].f = x; if(tree[x].f == 0) return; if(lf) tree[tree[x].f].l = x; else tree[tree[x].f].r = x;}void zag(int x){ push_down(x); int p = tree[x].f; bool lf; if(tree[p].f) { if(tree[tree[p].f].l == p) lf = true; else lf = false; } tree[p].r = tree[x].l; if(tree[x].l) tree[tree[x].l].f = p; push_up(p); tree[x].l = p; tree[x].f = tree[p].f; push_up(x); tree[p].f = x; if(tree[x].f == 0) return; if(lf) tree[tree[x].f].l = x; else tree[tree[x].f].r = x;}int splay(int x,int goal){ int p; push_down(x); while(tree[x].f != goal) { p = tree[x].f; if(tree[p].f == goal) { if(tree[p].l == x) zig(x); if(tree[p].r == x) zag(x); } else { int g = tree[p].f; if(tree[g].l == p && tree[p].l == x) { zig(p); zig(x); } else if(tree[g].l == p && tree[p].r == x) { zag(x); zig(x); } else if(tree[g].r == p && tree[p].l == x) { zig(x); zag(x); } else if(tree[g].r == p && tree[p].r == x) { zag(p); zag(x); } } } push_up(x); return x;}int build(int l,int r,int fa){ if(l > r) return 0; int mid = (l + r)>>1; int p = newnode(mid); tree[p].f = fa; tree[p].l = build(l,mid - 1,p); tree[p].r = build(mid + 1,r,p); push_up(p); return p;}void prepare(int &root){ root = newnode(-1); tree[root].r = newnode(-1); tree[tree[root].r].f = root; tree[tree[root].r].l = build(1,n,tree[root].r);push_up(tree[root].r);push_up(root);}int find(int pos,int rt){ if(!rt) return 0;push_down(rt); if(tree[tree[rt].l].size == pos - 1) return rt; if(tree[tree[rt].l].size >= pos) return find(pos,tree[rt].l); else return find(pos - tree[tree[rt].l].size - 1,tree[rt].r);}int getnext(int rt){ push_down(rt); while(tree[rt].l) { rt = tree[rt].l; push_down(rt); } return rt;}void cut(int a,int b,int c,int &root){ int l = find(a,root); int r = find(b + 2,root); root = splay(l,0); tree[root].r = splay(r,root); int tmp = tree[tree[root].r].l; tree[tree[root].r].l = 0; push_up(tree[root].r); push_up(root); int t = find(c + 1,root); root = splay(t,0); int nt = getnext(tree[root].r); tree[root].r = splay(nt,root); tree[tree[root].r].l = tmp;tree[tmp].f = tree[root].r; push_up(tree[root].r); push_up(root);}void Reverse(int a,int b,int &root){ int l = find(a,root); int r = find(b + 2,root); root = splay(l,0); tree[root].r = splay(r,root); tree[tree[tree[root].r].l].lazy ^= 1;}void print(int rt){ if(!rt) return ;push_down(rt); print(tree[rt].l);if(cnt >= 1 && cnt <= n){ printf("%d",tree[rt].key);if(cnt < n)putchar(' ');elseputchar(10);}cnt ++; print(tree[rt].r);}int main(){ char op[8]; int a,b,c; int root; while(scanf("%d",&n),n > 0) { scanf("%d",&m); init(); prepare(root); while(m --) { scanf("%s",op); scanf("%d%d",&a,&b); if(*op == 'C') { scanf("%d",&c); cut(a,b,c,root); } else Reverse(a,b,root); } cnt = 0; print(root); } return 0;}//421MS8480K/*8 2CUT 3 5 4FLIP 2 68 1FLIP 2 68 1CUT 1 2 68 2FLIP 4 7FLIP 4 78 2FLIP 2 5FLIP 4 7-1 -1*/
- 【hdu3487】【splay】Play with Chain
- hdu3487 Play with Chain(splay)
- HDU3487 Play with Chain (Splay)
- hdu3487 Play with Chain splay
- hdu3487 Play with Chain (Splay)
- Splay hdu3487 Play with Chain
- HDU3487 Play with Chain(Splay)
- hdu3487 Play with Chain(Splay)
- hdu3487 Play with Chain 伸展树splay
- hdu3487:Play with Chain (splay)
- hdu3487 Play with Chain
- #HDU3487#Play the Chain
- 【HDU3487】Play with Chain——伸展树
- HDU 3487 Play with Chain (Splay tree)
- hdu 3487 Play with Chain(splay)
- hdu 3487 Play with Chain(Splay)
- HDOJ 3487 Play with Chain【splay】
- HDU 3487 Play with Chain(Splay)
- codeforce -14A A. Letter
- 同余定理
- jquery实现下拉动态菜单
- 使用jvisualvm监控JAVA程序,注意对程序性能的影响
- Pycharm的远程代码编辑
- hdu3487 Play with Chain(splay)
- boost源码剖析3----内存管理shared_ptr
- ccsprite注意事项
- Google Maps地图投影全解析(1):Web墨卡托投影
- 使用非阻塞ServerSocketChannel、SocketChannel代替ServerSocket和Socket
- Hibernate4 执行SQL语句
- 个人收集关于vb的一些知识,希望以后可以温故知新咯!
- 基于jQuery的横向连续滚动【原创】
- android学习之点击事件的四种写法