Description
Input
Output
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; }