_bzoj3223 Tyvj 1729 文艺平衡树【Splay】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3223
裸的,打个标记。
#include <cstdio> #include <algorithm> const int maxn = 100005; int n, m, stk[maxn], top, t1, t2; int fa[maxn], ch[maxn][2], root, siz[maxn]; char rev[maxn]; inline void pushdown(int x) { if (rev[x]) { rev[x] = 0; rev[ch[x][0]] ^= 1; rev[ch[x][1]] ^= 1; std::swap(ch[x][0], ch[x][1]); } } inline void pushup(int x) { siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + 1; } inline void rotate(int x) { int y = fa[x]; if (y == ch[fa[y]][0]) { ch[fa[y]][0] = x; } else { ch[fa[y]][1] = x; } fa[x] = fa[y]; int dir = x == ch[y][1]; ch[y][dir] = ch[x][dir ^ 1]; fa[ch[x][dir ^ 1]] = y; ch[x][dir ^ 1] = y; fa[y] = x; pushup(y); pushup(x); } inline void splay(int x, int rt) { int p; top = 0; for (int i = x; i != rt; i = fa[i]) { stk[top++] = i; } for (int i = top - 1; ~i; --i) { pushdown(stk[i]); } while (fa[x] != rt) { p = fa[x]; if (fa[p] == rt) { rotate(x); } else { if ((p == ch[fa[p]][1]) ^ (x == ch[p][1])) { rotate(x); } else { rotate(p); } rotate(x); } } if (!rt) { root = x; } } inline int kth(int k) { int x = root; pushdown(x); while (k != siz[ch[x][0]] + 1) { if (k <= siz[ch[x][0]]) { x = ch[x][0]; } else { k -= siz[ch[x][0]] + 1; x = ch[x][1]; } pushdown(x); } return x; } int make_tree(int left, int right) { if (left > right) { return 0; } int rt = (left + right) >> 1; ch[rt][0] = make_tree(left, rt - 1); fa[ch[rt][0]] = rt; ch[rt][1] = make_tree(rt + 1, right); fa[ch[rt][1]] = rt; pushup(rt); return rt; } void print(int r) { if (!r) { return; } pushdown(r); print(ch[r][0]); if (r != 1 && r != n + 2) { printf("%d ", r - 1); } print(ch[r][1]); } int main(void) { //freopen("in.txt", "r", stdin); scanf("%d%d", &n, &m); root = make_tree(1, n + 2); while (m--) { scanf("%d%d", &t1, &t2); ++t1; ++t2; t1 = kth(t1 - 1); t2 = kth(t2 + 1); splay(t1, 0); splay(t2, root); rev[ch[ch[root][1]][0]] ^= 1; } print(root); return 0; }