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
| #include <bits/stdc++.h> using namespace std; const int maxn = 2e6+10; const double alpha = 0.75;
struct scapegoat { int son[2],val,valid,total; bool exist; } e[maxn]; int memory[maxn],cur[maxn]; int root,pool,poi,to_rebuild;
bool isbad(int now) { int left = e[now].son[0]; int right = e[now].son[1]; if ((double)e[now].valid*alpha <= (double)max(e[left].valid,e[right].valid)) return true; return false; }
void dfs(int now) { if (!now) return ; dfs(e[now].son[0]); if (e[now].exist) cur[++poi] = now; else memory[++pool] = now; dfs(e[now].son[1]); }
void build(int l,int r,int &now) { int mid=(l+r)>>1; now = cur[mid]; if (l==r) { e[now].son[0] = e[now].son[1] = 0; e[now].total = e[now].valid = 1; return ; } if (l <= mid-1) build(l,mid-1,e[now].son[0]); else e[now].son[0] = 0; build(mid+1,r,e[now].son[1]); int left=e[now].son[0],right=e[now].son[1]; e[now].total = e[left].total + e[right].total + 1; e[now].valid = e[left].valid + e[right].valid + 1; }
void rebuild(int &now) { poi = 0; dfs(now); if (poi) build(1,poi,now); else now = 0; }
void insert(int &now,int val) { if (!now) { now = memory[pool--]; e[now].val = val; e[now].exist = e[now].total = e[now].valid = 1; e[now].son[0] = e[now].son[1] = 0; return ; } e[now].total++,e[now].valid++; if (val <= e[now].val) insert(e[now].son[0],val); else insert(e[now].son[1],val); if (!isbad(now)) { if (to_rebuild) { if (e[now].son[0] == to_rebuild) rebuild(e[now].son[0]); else rebuild(e[now].son[1]); to_rebuild = 0; } } else to_rebuild = now; if (isbad(root)) rebuild(root); }
void delete_pos(int &now,int tar) { int left=e[now].son[0],right=e[now].son[1]; if (e[now].exist && e[left].valid+1 == tar) { e[now].exist = 0; e[now].valid--; return ; } e[now].valid--; if (tar <= e[left].valid + e[now].exist) delete_pos(e[now].son[0],tar); else delete_pos(e[now].son[1],tar-e[left].valid-e[now].exist); }
int find_kth(int k) { int now = root; while (now) { int left=e[now].son[0],right=e[now].son[1]; if (e[now].exist && e[left].valid+1 == k) return e[now].val; else if (k <= e[left].valid) now = left; else { k -= e[left].valid + e[now].exist; now = right; } } }
int find_rank(int val) { int now = root,ans = 1; while (now) { if (val <= e[now].val) now = e[now].son[0]; else { int left = e[now].son[0],right = e[now].son[1]; ans += e[left].valid + e[now].exist; now = right; } } return ans; }
void delete_val(int x) { delete_pos(root,find_rank(x)); if((double)e[root].total*alpha > e[root].valid) rebuild(root); }
void init() { for (int i=maxn-1; i>=1; i--) memory[++pool] = i; }
int main() { init(); int n,x,val; cin >> n; while (n--) { cin >> x >> val; if (x == 1) insert(root,val); if (x == 2) delete_val(val); if (x == 3) cout << find_rank(val) << endl; if (x == 4) cout << find_kth(val) << endl; if (x == 5) cout << find_kth(find_rank(val)-1) << endl; if (x == 6) cout << find_kth(find_rank(val+1)) << endl; } return 0; }
|