Portal: http://www.lydsy.com/JudgeOnline/problem.php?id=3223
Naked, with a mark.
#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;push Up (y);p ushup (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, RI ght); fa[ch[rt][1]] = rt;pushup (RT); return RT;} void print (int r) {if (!r) {return;} Pushdown (R);p rint (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--) {scan F ("%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;}
_bzoj3223 tyvj 1729 Literary balance tree "splay"