HDU 3487 Play with Chain

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

splay区间反转+区间移动,彻底理解了splay以后这种操作就得心应手了

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<functional>
using namespace std;
typedef unsigned long long ull;
typedef long long LL;
const int maxn = 3e5 + 10;
int n, m, l, r, c, root, flag;
char s[10];

struct Splays
{
	const static int maxn = 3e5 + 10;			//节点个数
	const static int INF = 0x7FFFFFFF;			//int最大值
	int ch[maxn][2], F[maxn], sz;				//左右儿子,父亲节点和节点总个数
	int A[maxn], R[maxn], C[maxn];
	int Node(int f, int u) { R[sz] = ch[sz][0] = ch[sz][1] = 0; F[sz] = f; A[sz] = u; C[sz] = 1; return sz++; }//申请一个新节点
	void clear(){ sz = 1; ch[0][0] = ch[0][1] = F[0] = 0; C[0] = 0; }//清空操作
	void Pushdown(int x)
	{
		if (!R[x]) return;
		R[ch[x][0]] ^= 1;	R[ch[x][1]] ^= 1;
		swap(ch[x][0], ch[x][1]);   R[x] = 0;
	}
	void rotate(int x, int k)
	{
		int y = F[x]; ch[y][!k] = ch[x][k]; F[ch[x][k]] = y;
		if (F[y]) ch[F[y]][y == ch[F[y]][1]] = x;
		F[x] = F[y];    F[y] = x;	ch[x][k] = y;
		C[x] = C[y];	C[y] = C[ch[y][0]] + C[ch[y][1]] + 1;
	}
	void Splay(int x, int r)
	{
		for (int fa = F[r]; F[x] != fa;)
		{
			if (F[F[x]] == fa) { rotate(x, x == ch[F[x]][0]); return; }
			int y = x == ch[F[x]][0], z = F[x] == ch[F[F[x]]][0];
			y^z ? (rotate(x, y), rotate(x, z)) : (rotate(F[x], z), rotate(x, y));
		}
	}
	void build(int fa, int &x, int l, int r)
	{
		if (l > r) return;
		int mid = l + r >> 1;
		x = Node(fa, mid);
		build(x, ch[x][0], l, mid - 1);
		build(x, ch[x][1], mid + 1, r);
		C[x] += C[ch[x][0]] + C[ch[x][1]];
	}
	void find(int &x, int k)
	{
		for (int i = x, j = k;;)
		{
			Pushdown(i);
			if (C[ch[i][0]] == j) { Splay(i, x); x = i; break; }
			if (C[ch[i][0]] > j) { i = ch[i][0]; continue; }
			j -= C[ch[i][0]] + 1;	i = ch[i][1];
		}
	}
	void change(int&x, int l, int r)
	{
		find(x, l - 1);
		find(ch[x][1], r - l + 1);
		R[ch[ch[x][1]][0]] ^= 1;
	}
	void insert(int&x, int l, int r, int c)
	{
		find(x, l - 1);
		find(ch[x][1], r - l + 1);
		int add = C[ch[ch[x][1]][0]], temp = ch[ch[x][1]][0];
		C[x] -= add;	C[ch[x][1]] -= add;
		ch[ch[x][1]][0] = 0;
		find(x, c);
		find(ch[x][1], 0);
		ch[ch[x][1]][0] = temp;
		F[temp] = ch[x][1];
		C[ch[x][1]] += add;
		C[x] += add;
	}
	void putout(int x)
	{
		Pushdown(x);
		if (ch[x][0]) putout(ch[x][0]);
		if (A[x] != 0 && A[x] != n + 1)
		{
			printf("%s", flag ? " " : "");
			printf("%d", A[x]);
			flag = 1;
		}
		if (ch[x][1]) putout(ch[x][1]);
	}
}solve;

int main()
{
	while (scanf("%d%d", &n, &m) != EOF, n + m>0)
	{
		solve.clear();	root = 0;
		solve.build(0, root, 0, n + 1);
		while (m--)
		{
			scanf("%s%d%d", s, &l, &r);
			if (s[0] == 'C') scanf("%d", &c), solve.insert(root, l, r, c);
			else solve.change(root, l, r);
		}
		flag = 0;
		solve.putout(root);
		printf("\n");
	}
	return 0;
}


你可能感兴趣的:(HDU)