Play with Chain
Time Limit: 2000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 348764-bit integer IO format: %I64d Java class name: Main
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 真是会play啊

1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 300010; 4 struct SplayTree { 5 int val[maxn],fa[maxn],ch[maxn][2]; 6 int flip[maxn],sz[maxn],tot,root; 7 bool flag; 8 inline void pushdown(int x) { 9 if(flip[x]) { 10 flip[ch[x][0]] ^= 1; 11 flip[ch[x][1]] ^= 1; 12 swap(ch[x][0],ch[x][1]); 13 flip[x] = 0; 14 } 15 } 16 inline void pushup(int x) { 17 sz[x] = 1 + sz[ch[x][0]] + sz[ch[x][1]]; 18 } 19 void newnode(int &x,int key,int f) { 20 x = ++tot; 21 val[x] = key; 22 fa[x] = f; 23 sz[x] = 1; 24 flip[x] = ch[x][0] = ch[x][1] = 0; 25 } 26 void rotate(int x,int kd) { 27 int y = fa[x]; 28 pushdown(y); 29 pushdown(x); 30 ch[y][kd^1] = ch[x][kd]; 31 fa[ch[x][kd]] = y; 32 fa[x] = fa[y]; 33 ch[x][kd] = y; 34 fa[y] = x; 35 if(fa[x]) ch[fa[x]][y == ch[fa[x]][1]] = x; 36 pushup(y); 37 } 38 void splay(int x,int goal) { 39 pushdown(x); 40 while(fa[x] != goal) { 41 pushdown(fa[fa[x]]); 42 pushdown(fa[x]); 43 pushdown(x); 44 if(fa[fa[x]] == goal) rotate(x,x == ch[fa[x]][0]); 45 else { 46 int y = fa[x],z = fa[y],s = (y == ch[z][0]); 47 if(x == ch[y][s]) { 48 rotate(x,s^1); 49 rotate(x,s); 50 } else { 51 rotate(y,s); 52 rotate(x,s); 53 } 54 } 55 } 56 pushup(x); 57 if(!goal) root = x; 58 } 59 void select(int k,int goal){ 60 int x = root; 61 pushdown(x); 62 while(sz[ch[x][0]] + 1 != k){ 63 if(k < sz[ch[x][0]] + 1) x = ch[x][0]; 64 else{ 65 k -= sz[ch[x][0]] + 1; 66 x = ch[x][1]; 67 } 68 pushdown(x); 69 } 70 splay(x,goal); 71 } 72 void build(int &x,int L,int R,int f){ 73 if(L > R) return; 74 int mid = (L + R)>>1; 75 newnode(x,mid,f); 76 build(ch[x][0],L,mid-1,x); 77 build(ch[x][1],mid + 1,R,x); 78 pushup(x); 79 } 80 void init(int n){ 81 sz[0] = tot = root = 0; 82 flip[0] = ch[0][0] = ch[0][1] = 0; 83 val[0] = fa[0] = 0; 84 newnode(root,-1,0); 85 newnode(ch[root][1],-1,root); 86 sz[root] = 2; 87 build(ch[ch[root][1]][0],1,n,ch[root][1]); 88 } 89 void OUT(int x){ 90 if(!x) return; 91 pushdown(x); 92 OUT(ch[x][0]); 93 if(~val[x]){ 94 if(flag) putchar(' '); 95 printf("%d",val[x]); 96 flag = true; 97 } 98 OUT(ch[x][1]); 99 } 100 void out(){ 101 flag = false; 102 OUT(root); 103 putchar(' '); 104 } 105 void cut(int a,int b,int c){ 106 select(a-1 + 1,0); 107 select(b + 1 + 1,root); 108 int tmp = ch[ch[root][1]][0]; 109 ch[ch[root][1]][0] = 0; 110 pushup(ch[root][1]); 111 pushup(root); 112 select(c+1,0); 113 select(c+1+1,root); 114 ch[ch[root][1]][0] = tmp; 115 fa[tmp] = ch[root][1]; 116 pushup(ch[root][1]); 117 pushup(root); 118 } 119 void reverse(int L,int R){ 120 select(L - 1 + 1,0); 121 select(R + 1 + 1,root); 122 flip[ch[ch[root][1]][0]] ^= 1; 123 } 124 }spt; 125 int main() { 126 char op[10]; 127 int n,m,a,b,c; 128 while(~scanf("%d%d",&n,&m)){ 129 if(n == -1 && m == -1) break; 130 spt.init(n); 131 for(int i = 1; i <= m; ++i){ 132 scanf("%s",op); 133 if(op[0] == 'C'){ 134 scanf("%d%d%d",&a,&b,&c); 135 spt.cut(a,b,c); 136 }else{ 137 scanf("%d%d",&a,&b); 138 spt.reverse(a,b); 139 } 140 } 141 spt.out(); 142 } 143 return 0; 144 }