1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| #define lc T[i].ch[0] #define rc T[i].ch[1] struct Splay { int v, val, sz, ch[2]; bool rev; } T[maxn]; int top, rt, f[maxn]; void init_Splay() { T[0].v = -1; } inline void update(int i) { if (!i) return ; T[i].sz = 1 + T[lc].sz + T[rc].sz; } inline void Update(int i) { if (!i) return ; swap(lc, rc); T[i].rev ^= 1; } inline void pushdown(int i) { bool &rev = T[i].rev; if (!rev) return ; Update(lc); Update(rc); rev = 0; } inline int get(int i) { return i == T[f[i]].ch[1]; } inline void rotate(int x) { int fa = f[x], ffa = f[f[x]], wx = get(x); f[x] = ffa; f[T[x].ch[wx ^ 1]] = fa; f[fa] = x; if (ffa) T[ffa].ch[T[ffa].ch[1] == fa] = x; T[fa].ch[wx] = T[x].ch[wx ^ 1]; T[x].ch[wx ^ 1] = fa; update(fa); update(x); } void Splay(int i, int k = 0) { for (int fa = f[i]; fa != k; rotate(i), fa = f[i]) if (f[fa] != k) rotate(get(i) == get(fa) ? fa : i); if (!k) rt = i; } void build(int &i, int l, int r, int fa) { if (l > r) return ; i = ++top; int m = l + r >> 1; build(lc, l, m - 1, i); build(rc, m + 1, r, i); T[i].val = a[m], f[i] = fa, update(i); } int find(int k) { int i = rt; while (1) { pushdown(i); if (k <= T[lc].sz) i = lc; else { k -= T[lc].sz + 1; if (!k) return Splay(i), i; i = rc; } } } int split(int x, int y) { x = find(x), y = find(y + 2); Splay(x), Splay(y, x); return T[y].ch[0]; }
|