Link: 2018 Northeast Agricultural University Spring School race: L-wyh's Swan
Test Instructions: insert element, delete element, find K large.
Puzzle : Treap.
#include <bits/stdc++.h> using namespace std; #define LC (O-ch[0]) #define RC (O-and ch[1]) #define VAL (o-V) #define PRE (O-P) #define SIZ (O-
S) Const Double EPS = 1e-8;
const INT mod = 1e9 + 7;
const int INF = 0X3F3F3F3F;
const int MAXN = 5e5 + 10;
struct Treap {treap* ch[2];
int p, V, S;
};
int root = 0, n, m, X;
void Update (treap* &o) {siz = 1;
if (LC! = NULL) Siz + = LC-S;
if (rc! = NULL) Siz + = RC-S;
} void Rotate (treap* &o, int d) {treap* P = o-ch[d ^ 1];
O-ch[d ^ 1] = P--ch[d];
P--ch[d] = O; Update (o);
Update (P);
o = P;
} void Insert (treap* &o, int x) {if (o = = NULL) {o = new treap ();
Lc = Rc = NULL;
Pre = rand ();
val = x;
} else{int d = x < val;
Insert (O-ch[d], x);
if (Pre > O-ch[d], p) Rotate (o, d^1);
} Update (o);
} void Delete (treap* &o, int x) { if (val = = x) {if (Lc = = NULL) o = Rc;
else if (Rc = = NULL) o = Lc;
else{int T = (Lc, p) < (RC-p);
Rotate (o, T);
Delete (O-ch[t], x);
}} else Delete (O-ch[x < Val], X);
if (o! = NULL) Update (o);
} int Kth_max (treap* o, int k) {if (o = = NULL | | k <= 0 | | k > siz) return 0; int S = (Lc = = NULL)?
0: (Lc-S);
if (k = = S + 1) return Val;
else if (k <= S) return Kth_max (Lc, k);
else return Kth_max (Rc, k-s-1);
} int main () {int T;
scanf ("%d", &t);
Char s[10];
while (t--) {Srand (Time (NULL));
scanf ("%d%d", &n, &m);
treap* Root = NULL;
for (int i = 1, x; i <= n; i++) {scanf ("%d", &x);
Insert (Root, x);
} while (m--) {scanf ("%s%d", S, &x);
if (s[0] = = ' Q ') printf ("%d\n", Kth_max (Root, x)); else if (s[0] = = ' I ')Insert (Root, x);
else Delete (Root, x);
}} return 0; }