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
| #include <bits/stdc++.h> using namespace std;
namespace Legendgod {
namespace Read {
#ifdef Fread const int Siz = (1 << 21) + 5; char buf[Siz], *iS, *iT; #define gc() (iS == iT ? (iT = (iS = buf) + fread(buf, 1, Siz, stdin), iT == iS ? EOF : *iS ++ ) : *iS ++) #define getchar gc #endif
template <typename T> void r1(T &x) { x = 0; int f(1); char c(getchar()); for(; !isdigit(c); c = getchar()) if(c == '-') f = -1; for(; isdigit(c); c = getchar()) x = (x * 10) + (c ^ 48); x *= f; } template <typename T, typename...Args> void r1(T &x, Args&...arg) { r1(x), r1(arg...); }
}
using namespace Read;
const int maxn = 2e5 + 5;
namespace Splay { struct Node { int val, siz, ts, fa, ch[2]; }t[maxn]; int tot(0), rt(0); struct Seg { #define ls t[p].ch[0] #define rs t[p].ch[1] void Prepare(int p,int c) { ls = rs = 0, t[p].fa = 0, t[p].ts = t[p].siz = 1, t[p].val = c; } void Clear(int p) { ls = rs = t[p].fa = t[p].ts = t[p].siz = t[p].val = 0; } void pushup(int p) { if(!p) return ; t[p].siz = t[p].ts; if(ls) t[p].siz += t[ls].siz; if(rs) t[p].siz += t[rs].siz; } int pd(int p) { return t[t[p].fa].ch[1] == p; } void Rotate(int p) { int f = t[p].fa, ff = t[f].fa, k = pd(p); t[p].fa = ff; if(ff) t[ff].ch[pd(f)] = p; t[f].ch[k] = t[p].ch[!k], t[t[p].ch[!k]].fa = f; t[p].ch[!k] = f, t[f].fa = p; pushup(p), pushup(f); } void Splay(int p,int goal = 0) { for(int f; (f = t[p].fa) != goal; Rotate(p)) if(t[f].fa != goal) { Rotate(pd(f) == pd(p) ? f : p); } rt = p; }
void Insert(int c) { int p = rt, ff = 0; while(p && t[p].val != c) ff = p, p = t[p].ch[c > t[p].val]; if(t[p].val == c) ++ t[p].ts, Splay(p); else { int p = ++ tot; Prepare(p, c); if(ff) t[p].fa = ff, t[ff].ch[c > t[ff].val] = p; Splay(p); } }
void Find(int c) { int p = rt; if(!p) return ; while(t[p].val != c && t[p].ch[c > t[p].val]) p = t[p].ch[c > t[p].val]; return Splay(p); }
int Near(int c,int opt) { Find(c); int p = rt; if((opt && t[p].val > c) || (!opt && t[p].val < c)) return p; for(p = t[p].ch[opt]; t[p].ch[!opt]; p = t[p].ch[!opt]); return p; }
void Del(int c) { int pre = Near(c, 0), suf = Near(c, 1); Splay(pre, 0), Splay(suf, pre); int p = t[suf].ch[0];
if(t[p].ts > 1) -- t[p].ts, -- t[p].siz, Splay(p); else t[suf].ch[0] = 0, Clear(p), Splay(suf); }
int Rnk(int c) { return Find(c), t[t[rt].ch[0]].siz; }
int Kth(int K) { int p = rt; if(t[p].siz < K) return -1; while(1) { if(ls && t[ls].siz >= K) p = ls; else { K -= t[p].ts + (ls ? t[ls].siz : 0); if(K <= 0) return t[p].val; p = rs; } } }
#undef ls #undef rs }T; }
using namespace Splay;
int n; const int inf = 0x3f3f3f3f;
signed main() {
int i, j; r1(n); T.Insert(- inf), T.Insert(inf); for(i = 1; i <= n; ++ i) { int opt, c; r1(opt, c); switch(opt) { case 1: T.Insert(c); break; case 2: T.Del(c); break; case 3: printf("%d\n", T.Rnk(c)); break; case 4: printf("%d\n", T.Kth(c + 1)); break; case 5: printf("%d\n", t[T.Near(c, 0)].val); break; case 6: printf("%d\n", t[T.Near(c, 1)].val); break; default: break; } } return 0; }
}
signed main() { return Legendgod::main(), 0; }
|