hdu3487Play with Chain
Play with Chain
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7129 Accepted Submission(s):
2831
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 2
CUT 3 5 4
FLIP 2 6
-1 -1
Sample Output
1 4 3 7 6 2 5 8
Source
分析
splay功能,区间旋转,区间截取及插入。
区间截取及插入
1、截取区间[a, b]
把第a-1个数旋转到根,把第b+1个数旋转到根的右儿子,那么b+1的左儿子就是所要截取的区间,把b的左儿子记录下即可,更新。
2、插入一段区间到第c个数后
把第c个数旋转到根,把第c+1个数旋转到根的右儿子,那么b+1的左儿子一定是空的,然后将要插入的区间的根节点赋值给b的左儿子即可,更新。
区间翻转:
翻转区间[a, b]
把第a-1个数旋转到根,把第b+1个数旋转到根的右儿子,那么b+1的左儿子就是所要截取的区间,打个标记即可,用到了再下传。
code
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<cmath> 5 #include<iostream> 6 7 using namespace std; 8 9 const int N = 500100; 10 11 int ch[N][2],fa[N],tag[N],siz[N],data[N]; 12 int Root,n,m,cnt; 13 14 inline int read() { 15 int x = 0,f = 1;char ch = getchar(); 16 for (; ch<'0'||ch>'9'; ch = getchar()) if (ch=='-') f = -1; 17 for (; ch>='0'&&ch<='9'; ch = getchar()) x = x * 10 + ch - '0'; 18 return x * f; 19 } 20 inline void pushup(int x) { 21 siz[x] = siz[ch[x][1]] + siz[ch[x][0]] + 1; 22 } 23 inline void pushdown(int x) { 24 if (tag[x]) { 25 tag[ch[x][0]] ^= 1;tag[ch[x][1]] ^= 1; 26 swap(ch[x][0],ch[x][1]); 27 tag[x] ^= 1; 28 } 29 } 30 inline int son(int x) { 31 return x == ch[fa[x]][1]; 32 } 33 inline void rotate(int x) { 34 int y = fa[x],z = fa[y],b = son(x),c = son(y),a = ch[x][!b]; 35 if (z) ch[z][c] = x;else Root = x;fa[x] = z; 36 ch[x][!b] = y;fa[y] = x; 37 ch[y][b] = a;if (a) fa[a] = y; 38 pushup(y);pushup(x); 39 } 40 inline void splay(int x,int rt) { 41 while (fa[x] != rt) { 42 int y = fa[x],z = fa[y]; 43 if (z==rt) rotate(x); 44 else { 45 if (son(x) == son(y)) rotate(y),rotate(x); 46 else rotate(x),rotate(x); 47 } 48 } 49 } 50 inline int getkth(int k) { 51 int p = Root; 52 while (true) { 53 pushdown(p); 54 if (siz[ch[p][0]] + 1 == k) return p; 55 if (ch[p][0] && k <= siz[ch[p][0]] ) p = ch[p][0]; 56 else { 57 k -= ((ch[p][0] ? siz[ch[p][0]] : 0) + 1); 58 p = ch[p][1]; 59 } 60 } 61 } 62 inline void rever(int l,int r) { 63 int L = getkth(l),R = getkth(r + 2); 64 splay(L,0);splay(R,L); 65 tag[ch[R][0]] ^= 1; 66 } 67 inline void cut(int l,int r,int p) { 68 int L = getkth(l),R = getkth(r+2); 69 splay(L,0);splay(R,L); 70 int tmp = ch[R][0]; 71 fa[tmp] = 0;ch[R][0] = 0; 72 pushup(R);pushup(L); 73 L = getkth(p+1),R = getkth(p+2); 74 splay(L,0);splay(R,L); 75 fa[tmp] = R;ch[R][0] = tmp; 76 pushup(R);pushup(L); 77 } 78 int build(int l,int r) { 79 if (l > r) return 0; 80 int mid = (l + r) >> 1; 81 int t = build(l,mid-1); 82 ch[mid][0] = t;fa[t] = mid; 83 t = build(mid+1,r); 84 ch[mid][1] = t;fa[t] = mid; 85 pushup(mid); 86 return mid; 87 } 88 void print(int x) { 89 if (!x) return ; 90 pushdown(x); 91 print(ch[x][0]); 92 if (data[x] > 0 && data[x] < n+2) { 93 if (cnt==0) printf("%d",data[x]),cnt = 1; 94 else printf(" %d",data[x]); 95 } 96 print(ch[x][1]); 97 } 98 inline void init() { 99 Root = cnt = 0; 100 memset(ch,0,sizeof(ch)); 101 memset(fa,0,sizeof(fa)); 102 memset(tag,0,sizeof(tag)); 103 memset(siz,0,sizeof(siz)); 104 memset(data,0,sizeof(data)); 105 } 106 int main() { 107 int a,b,c; 108 char s[10]; 109 while (scanf("%d%d",&n,&m)!=EOF && !(n==-1&&m==-1)) { 110 init(); 111 for (int i=2; i<=n+1; ++i) data[i] = i-1; 112 Root = build(1,n+2); 113 while (m--) { 114 scanf("%s",s); 115 if (s[0]=='C') { 116 a = read(),b = read(),c = read(); 117 cut(a,b,c); 118 } 119 else { 120 a = read(),b = read(); 121 rever(a,b); 122 } 123 } 124 print(Root); 125 printf("\n"); 126 } 127 return 0; 128 }