hdu 3487 play with chain

来源:互联网 发布:网络架构师认证 编辑:程序博客网 时间:2024/05/04 06:58

splay。两个操作,删除某个区间,翻转某个区间。因为边界情况不好删除,在头和尾加多两个没用的数, build(0, n + 1, 0);

//#include <bits/stdc++.h>#include <algorithm>#include <cstdio>#include <iostream>#include <cstring>#include <cmath>using namespace std;#define LL long long#define inf 0x3f3f3f3f#define MP make_pair#define pii pair<int, int>#define md (ll + rr >> 1)#define N 300010#define M 200020int pre[N], key[N], sz[N], ch[N][2], down[N];int tot, root;int creat(int val, int p){int k = ++tot;pre[k] = p;sz[k] = 1;ch[k][0] = ch[k][1] = 0;down[k] = 0;key[k] = val;return k;}void push_up(int k){sz[k] = sz[ch[k][0]] + sz[ch[k][1]] + 1;}void push_fan(int x){swap(ch[x][0], ch[x][1]);down[x] ^= 1;}void push_down(int k){if(down[k]){if(ch[k][0]) push_fan(ch[k][0]);if(ch[k][1]) push_fan(ch[k][1]);down[k] = 0;}}void rot(int x){int y = pre[x], d = ch[y][1] == x;ch[y][d] = ch[x][!d];if(ch[x][!d]) pre[ch[x][!d]] = y;ch[x][!d] = y;pre[x] = pre[y];pre[y] = x;if(pre[x]) ch[pre[x]][ch[pre[x]][1] == y] = x;push_up(y);}void P(int x){if(pre[x]) P(pre[x]);push_down(x);}void splay(int x, int goal){P(x);while(pre[x] != goal){int f = pre[x], ff = pre[f];if(ff == goal)rot(x);else if((ch[ff][1] == f) == (ch[f][1] == x))rot(f), rot(x);else rot(x), rot(x);}push_up(x);if(goal == 0) root = x;}int kth(int k){int x = root;while(x){push_down(x);if(sz[ch[x][0]] >= k) x = ch[x][0];else{k -= sz[ch[x][0]] + 1;if(k == 0)return x;x = ch[x][1];}}return x;}int build(int ll, int rr, int p){if(ll > rr) return 0;int x = creat(md, p);ch[x][0] = build(ll, md - 1, x);ch[x][1] = build(md + 1, rr, x);push_up(x);return x;}int n, m;bool ok;void output(int x){if(!x) return ;push_down(x);output(ch[x][0]);if(key[x] > 0 && key[x] <= n){if(ok) ok = 0;else printf(" ");printf("%d", key[x]);}output(ch[x][1]);}int main(){while(scanf("%d%d", &n, &m) != EOF){if(n < 0 && m < 0) break;tot = 0;root = build(0, n + 1, 0);char s[5];int l, r, c;while(m--){scanf("%s", s);if(s[0] == 'C'){scanf("%d%d%d", &l, &r, &c);splay(kth(l), 0);splay(kth(r+2), root);int f = ch[root][1];int x = ch[f][0];ch[f][0] = 0;push_up(f), push_up(root);splay(kth(c+1), 0);splay(kth(c+2), root);f = ch[root][1];ch[f][0] = x;pre[x] = f;push_up(f), push_up(root);}else{scanf("%d%d", &l, &r);splay(kth(l), 0);splay(kth(r+2), root);int x = ch[ch[root][1]][0];push_fan(x);}}ok = 1;output(root);puts("");}return 0;}


0 0