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
| const int maxn = 1e5 + 10; int n, m;
namespace Splay { class node { public: int son[2], pa; int sz, tag, v;
node() = default; void init(int _v, int _pa) { v = _v, pa = _pa; sz = 1; } };
int root = 0; int idx = 0; vector<node> t(maxn, node());
inline void pull(int p) { t[p].sz = t[t[p].son[0]].sz + t[t[p].son[1]].sz + 1; }
inline void push(int p) { if (!t[p].tag) return; swap(t[p].son[0], t[p].son[1]); t[t[p].son[0]].tag ^= 1, t[t[p].son[1]].tag ^= 1; t[p].tag = 0; } void rotate(int x) { int y = t[x].pa, z = t[y].pa; int k = t[y].son[1] == x;
t[z].son[t[z].son[1] == y] = x, t[x].pa = z; t[y].son[k] = t[x].son[k^1], t[t[x].son[k^1]].pa = y; t[x].son[k^1] = y, t[y].pa = x; pull(y), pull(x); }
void splay(int x, int k) { while (t[x].pa != k) { int y = t[x].pa, z = t[y].pa; if (z != k) { if ((t[z].son[1] == y) ^ (t[y].son[1] == x)) rotate(x); else rotate(y); } rotate(x); } if (!k) root = x; }
void insert(int val) { int u = root, p = 0; while (u) p = u, u = t[u].son[val > t[u].v]; u = ++idx; if (p) t[p].son[val > t[p].v] = u; t[u].init(val, p);
splay(u, 0); }
int get_k(int k) { int u = root; while (u) { push(u); if (t[t[u].son[0]].sz >= k) u = t[u].son[0]; else if (t[t[u].son[0]].sz + 1 == k) return u; else k -= t[t[u].son[0]].sz + 1, u = t[u].son[1]; } return -1; }
void print(int u) { push(u); if (t[u].son[0]) print(t[u].son[0]); if (t[u].v >= 1 && t[u].v <= n) printf("%d ", t[u].v); if (t[u].son[1]) print(t[u].son[1]); } }
using namespace Splay;
int main() { freopen("input.txt", "r", stdin); scanf("%d%d", &n, &m);
for (int i = 0; i <= n+1; i++) insert(i); while (m--) { int li, ri; scanf("%d%d", &li, &ri); int l = get_k(li), r = get_k(ri+2); splay(l, 0), splay(r, l); t[t[r].son[0]].tag ^= 1; } print(root); }
|