_bzoj3223 Tyvj 1729 文艺平衡树【Splay】

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3223

裸的,打个标记。

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
116
117
118
119
120
121
#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;
    pushup(y);
    pushup(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, right);
    fa[ch[rt][1]] = rt;
    pushup(rt);
    return rt;
}
void print(int r) {
    if (!r) {
        return;
    }
    pushdown(r);
    print(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--) {
        scanf("%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;
}

  

posted @   ciao_sora  阅读(348)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示