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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
| #include <bits/stdc++.h> using namespace std; const int maxn = 1e5+10;
int rt,tot,fa[maxn],ch[maxn][2],val[maxn],cnt[maxn],size[maxn];
struct Splay { void maintain(int x) { size[x] = size[ch[x][0]] + size[ch[x][1]] + cnt[x]; } bool get(int x) { return x == ch[fa[x]][1]; } void clear(int x) { ch[x][0] = ch[x][1] = fa[x] = val[x] = size[x] = cnt[x] = 0; }
void rotate(int x) { int y = fa[x],z=fa[y],chk=get(x); ch[y][chk] = ch[x][chk^1]; fa[ch[x][chk^1]] = y;
ch[x][chk^1] = y; fa[y] = x;
fa[x] = z; if (z) ch[z][y == ch[z][1]] = x;
maintain(y); maintain(x); } void splay(int x) { for (int f=fa[x]; f=fa[x],f; rotate(x)) if (fa[f]) rotate(get(x) == get(f) ? f : x); rt = x; } void ins(int k) { if (!rt) { val[++tot] = k; cnt[tot]++; rt = tot; maintain(rt); return ; } int cnr = rt,f = 0; while (1) { if (val[cnr] == k) { cnt[cnr]++; maintain(cnr); maintain(f); splay(cnr); break; } f = cnr; cnr = ch[cnr][val[cnr] < k]; if (!cnr) { val[++tot] = k; cnt[tot]++; fa[tot] = f; ch[f][val[f] < k] = tot;
maintain(tot); maintain(f); splay(tot); break; } } } int rk(int k) { int res = 0,cnr = rt; while (1) { if (k < val[cnr]) cnr = ch[cnr][0]; else { res += size[ch[cnr][0]]; if (k == val[cnr]) { splay(cnr); return res+1; } res += cnt[cnr]; cnr = ch[cnr][1]; } } } int kth(int k) { int cnr = rt; while (1) { if (ch[cnr][0] && k <= size[ch[cnr][0]]) cnr = ch[cnr][0]; else { k -= size[ch[cnr][0]] + cnt[cnr]; if (k <= 0) return val[cnr]; cnr = ch[cnr][1]; } } } int pre() { int cnr = ch[rt][0]; while(ch[cnr][1]) cnr = ch[cnr][1]; return cnr; } int nxt() { int cnr = ch[rt][1]; while(ch[cnr][0]) cnr = ch[cnr][0]; return cnr; } void del(int k) { rk(k); if (cnt[rt] > 1) { cnt[rt]--; maintain(rt); return ; } if (!ch[rt][0] && !ch[rt][1]) { clear(rt); rt = 0; return ; } if (!ch[rt][0]) { int cnr = rt; rt = ch[rt][1]; fa[rt] = 0; clear(cnr); return ; } if (!ch[rt][1]) { int cnr = rt; rt = ch[rt][0]; fa[rt] = 0; clear(cnr); return ; } int x = pre(),cnr = rt; splay(x); fa[ch[cnr][1]] = x; ch[x][1] = ch[cnr][1]; clear(cnr); maintain(rt); } } tree;
int n;
int main() { cin >> n; while (n--) { int index,temp; cin >> index >> temp; if (index == 1) { tree.ins(temp); } else if (index == 2) { tree.del(temp); } else if (index == 3) { cout << tree.rk(temp) << endl; } else if (index == 4) { cout << tree.kth(temp) << endl; } else if (index == 5) { tree.ins(temp); cout << val[tree.pre()] << endl; tree.del(temp); } else if (index == 6) { tree.ins(temp); cout << val[tree.nxt()] << endl; tree.del(temp); } } return 0; }
|