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
| #include <iostream> #include <cstdio> #include <cctype> #define maxn 100010 #define gc getchar #define INF 2000000000 using namespace std;
int read() { int x = 0, f = 0; char c = gc(); while (!isdigit(c)) f |= c == '-', c = gc(); while (isdigit(c)) x = x * 10 + c - '0', c = gc(); return f ? -x : x; }
int n, m;
#define lc T[i].ch[0] #define rc T[i].ch[1] struct Splay { int v, sz, ch[2]; bool rev; } T[maxn]; int top, rt, f[maxn]; inline void update(int i) { T[i].sz = 1 + T[lc].sz + T[rc].sz; }
inline void Update(int i) { 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].v = m; T[i].sz = 1; f[i] = fa; update(i); }
int findk(int k) { int i = rt; while (1) { pushdown(i); if (k <= T[lc].sz) { i = lc; continue; } k -= T[lc].sz; if (k <= 1) return Splay(i), i; --k; i = rc; } }
inline void solve() { int l = read(), r = read(), i; l = findk(l); r = findk(r + 2); Splay(l, 0); Splay(r, l); Update(T[r].ch[0]); }
void print(int i) { if (!i) return; pushdown(i); print(lc); if (1 <= T[i].v && T[i].v <= n) printf("%d ", T[i].v); print(rc); }
int main() { n = read(); m = read(); build(rt, 0, n + 1, 0); for (int i = 1; i <= m; ++i) solve(); print(rt); return 0; }
|