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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
int root, tot;
const int MAXN = 1e5 + 10;
struct node {
    int ch[2],  // 儿子
        f,      // 父亲
        siz,    // 子树大小
        val,    // 值
        cnt;    // 个数
} nd[MAXN];
void update(int x) {
    nd[x].siz = nd[nd[x].ch[0]].siz + nd[nd[x].ch[1]].siz + nd[x].cnt;
}
void rotate(int x) {
    int y = nd[x].f;
    int z = nd[y].f;
    int k = nd[y].ch[1] == x;
    nd[z].ch[nd[z].ch[1] == y] = x;
    nd[x].f = z;
    nd[y].ch[k] = nd[x].ch[k ^ 1];
    nd[nd[x].ch[k ^ 1]].f = y;
    nd[x].ch[k ^ 1] = y;
    nd[y].f = x;
    update(y), update(x);
}
void splay(int x, int goal) {
    while(nd[x].f != goal) {
        int y = nd[x].f;
        int z = nd[y].f;
        if(z != goal) {
            (nd[z].ch[1] == y) == (nd[y].ch[1] == x) ?
                rotate(y) : rotate(x);
        }
        rotate(x);
    }
    if(goal == 0) {
        root = x;
    }
}
void find(int x) {
    int u = root;
    if(!u) {
        return;
    }
    while(nd[u].ch[x > nd[u].val] && x != nd[u].val) {
        u = nd[u].ch[x > nd[u].val];
    }
    splay(u, 0);
}
void insert(int x) {
    int u = root, f = 0;
    while(u && nd[u].val != x) {
        f = u;
        u = nd[u].ch[x > nd[u].val];
    }
    if(u) {
        nd[u].cnt ++;
    } else {
        u = ++ tot;
        if(f) {
            nd[f].ch[x > nd[f].val] = u;
        }
        nd[u].ch[0] = nd[u].ch[1] = 0;
        nd[u].f = f;
        nd[u].val = x;
        nd[u].cnt = 1;
        nd[u].siz = 1;
    }
    splay(u, 0);
}
int Next(int x) {
    find(x);
    int u = root;
    if(nd[u].val > x) return u;
    u = nd[u].ch[1];
    while(nd[u].ch[0]) u = nd[u].ch[0];
    return u;
}
int Pre(int x) {
    find(x);
    int u = root;
    if(nd[u].val < x) return u;
    u = nd[u].ch[0];
    while(nd[u].ch[1]) u = nd[u].ch[1];
    return u;
}
void del(int x) {
    int pre = Pre(x), next = Next(x);
    splay(pre, 0), splay(next, pre);
    int del = nd[next].ch[0];
    if(nd[del].cnt > 1) {
        nd[del].cnt --;
        splay(del, 0);
    } else {
        nd[next].ch[0] = 0;
    }
}
int kth(int k) {
    int u = root;
    if(nd[u].siz < k) {
        return 0;
    }
    while(1) {
        int y = nd[u].ch[0];
        if(k > nd[y].siz + nd[u].cnt) {
            k -= nd[y].siz + nd[u].cnt;
            u = nd[u].ch[1];
        } else {
            if(k <= nd[y].siz) {
                u = y;
            } else {
                return nd[u].val;
            }
        }
    }
}
#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int q;
    cin >> q;
    insert(1e9), insert(-1e9);
    while(q --) {
        int op, x;
        cin >> op >> x;
        switch (op) {
            case 1:
                insert(x);
                break;
            case 2:
                del(x);
                break;
            case 3:
                find(x);
                cout << nd[nd[root].ch[0]].siz << '\n';
                break;
            case 4:
                cout << kth(x + 1) << '\n';
                break;
            case 5:
                cout << nd[Pre(x)].val << '\n';
                break;
            case 6:
                cout << nd[Next(x)].val << '\n';
                break;
        }
    }
}