【模板】有旋Treap
阿新 • • 發佈:2018-12-03
如題,這是一個模板。。。
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 #include <cctype> 6 7 inline void read(int & x) 8 { 9 x = 0; 10 int k = 1; 11 char c = getchar(); 12 while (!isdigit(c))13 if (c == '-') c = getchar(), k = -1; 14 else c = getchar(); 15 while (isdigit(c)) 16 x = (x << 1) + (x << 3) + (c ^ 48), 17 c = getchar(); 18 x *= k; 19 } 20 21 const int MAXN = 101100; 22 int n, tot = 0, rt = 0; 23 int cnt[MAXN], siz[MAXN], val[MAXN], rnd[MAXN];24 int son[MAXN][2]; 25 26 inline int Rand(void) 27 { 28 static int seed = 1024; 29 return seed = (int)seed * 482711LL % 2147483647; 30 } 31 32 inline void Pushup(int u) 33 { 34 siz[u] = siz[son[u][0]] + siz[son[u][1]] + cnt[u]; 35 } 36 37 inline void Rotate(int&u, int ch) 38 { 39 int b = son[u][ch ^ 1]; 40 son[u][ch ^ 1] = son[b][ch]; 41 son[b][ch] = u; 42 Pushup(u), Pushup(u = b); 43 } 44 45 inline void Insert(int &u, int x) 46 { 47 if (!u) 48 { 49 u = ++tot; 50 cnt[u] = siz[u] = 1; 51 val[u] = x, rnd[u] = Rand(); 52 son[u][0] = son[u][1] = 0; 53 return; 54 } 55 ++siz[u]; 56 if (val[u] == x) { ++cnt[u]; return; } 57 int ch = val[u] < x; Insert(son[u][ch], x); 58 if (rnd[u] > rnd[son[u][ch]]) Rotate(u, ch ^ 1); 59 } 60 61 inline void Delete(int &u, int x) 62 { 63 if (!u) return; 64 if (val[u] == x) 65 { 66 if (cnt[u] > 1) { --cnt[u], --siz[u]; return; } 67 int ch = rnd[son[u][0]] < rnd[son[u][1]]; 68 if (!son[u][0] || !son[u][1]) u = son[u][0] + son[u][1]; 69 else Rotate(u, ch), Delete(u, x); 70 } 71 else --siz[u], Delete(son[u][val[u] < x], x); 72 } 73 74 inline int Getrank(int u, int x) 75 { 76 if (!u) return 0; 77 if (val[u] == x) return siz[son[u][0]] + 1; 78 if (val[u] > x) return Getrank(son[u][0], x); 79 else return Getrank(son[u][1], x) + siz[son[u][0]] + cnt[u]; 80 } 81 82 inline int Getkth(int k) 83 { 84 int u = rt; 85 while (true) 86 if (k <= siz[son[u][0]]) u = son[u][0]; 87 else if (k > siz[son[u][0]] + cnt[u]) 88 k -= siz[son[u][0]] + cnt[u], u = son[u][1]; 89 else return val[u]; 90 } 91 92 inline int Pre(int x) 93 { 94 int u = rt, pre = -2147483600; 95 while (u) 96 if (x > val[u]) pre = val[u], u = son[u][1]; 97 else u = son[u][0]; 98 return pre; 99 } 100 101 inline int Suf(int x) 102 { 103 int u = rt, suf = 2147483600; 104 while (u) 105 if (x < val[u]) suf = val[u], u = son[u][0]; 106 else u = son[u][1]; 107 return suf; 108 } 109 110 int opt, x; 111 112 signed main() 113 { 114 read(n); 115 for (int i = 1; i <= n; ++i) 116 { 117 read(opt), read(x); 118 if (opt == 1) Insert(rt, x); 119 if (opt == 2) Delete(rt, x); 120 if (opt == 3) printf("%d\n", Getrank(rt, x)); 121 if (opt == 4) printf("%d\n", Getkth(x)); 122 if (opt == 5) printf("%d\n", Pre(x)); 123 if (opt == 6) printf("%d\n", Suf(x)); 124 } 125 return 0; 126 }