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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| #include <iostream> #include <cstdio> using namespace std; const int N = 1e5 + 10; int n, m, root, idx; struct Node { int s[2], p, v; int sz, flag; void init(int a, int b) { v = a, p = b; sz = 1; } }tr[N]; void pushup(int x) { tr[x].sz = tr[tr[x].s[0]].sz + tr[tr[x].s[1]].sz + 1; } void pushdown(int x) { if (tr[x].flag) { swap(tr[x].s[0], tr[x].s[1]); tr[tr[x].s[0]].flag ^= 1; tr[tr[x].s[1]].flag ^= 1; tr[x].flag = 0; } } void rotate(int x) { int y = tr[x].p, z = tr[y].p; int k = tr[y].s[1] == x; tr[z].s[tr[z].s[1] == y] = x; tr[x].p = z; tr[y].s[k] = tr[x].s[k ^ 1]; tr[tr[x].s[k ^ 1]].p = y; tr[x].s[k ^ 1] = y, tr[y].p = x; pushup(y); pushup(x);
} void splay(int x, int k) { while (tr[x].p != k) { int y = tr[x].p, z = tr[y].p; if (z != k) { if ((tr[y].s[1] == x) ^ (tr[z].s[1] == y)) rotate(x); else rotate(y); } rotate(x); } if (!k) root = x; } void insert(int v) { int u = root, p = 0; while (u) p = u, u = tr[u].s[v > tr[u].v]; u = ++idx; if (p) tr[p].s[v > tr[p].v] = u; tr[u].init(v, p); splay(u, 0); } int get_k(int k) { int u = root; while (true) { pushdown(u); if (tr[tr[u].s[0]].sz >= k) u = tr[u].s[0]; else if (tr[tr[u].s[0]].sz + 1 >= k) return u; else { k -= tr[tr[u].s[0]].sz + 1; u = tr[u].s[1]; } } return -1; } void output(int u) { pushdown(u); if (tr[u].s[0]) output(tr[u].s[0]); if (tr[u].v >= 1 && tr[u].v <= n) printf("%d ", tr[u].v); if (tr[u].s[1]) output(tr[u].s[1]); } int main() { cin >> n >> m; for (int i = 0; i <= n + 1; i++) insert(i); while (m--) { int a, b; cin >> a >> b; a = get_k(a), b = get_k(b + 2); splay(a, 0); splay(b, a); tr[tr[b].s[0]].flag ^= 1; } output(root); return 0; }
|