SBT first question.
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <
Cmath> using namespace std;
#define N 200005 typedef long LL;
struct sbt{int Lson, Rson, SZ, key;
void Init () {Lson = Rson = key = 0;
SZ = 1;
}}s[n];
int root, tot;
void LR (int &x) {int k = S[x].rson;
S[x].rson = S[k].lson;
S[k].lson = x;
S[K].SZ = S[X].SZ;
S[X].SZ = s[s[x].lson].sz + s[s[x].rson].sz + 1;
x = k;
} void RR (int &x) {int k = S[x].lson;
S[x].lson = S[k].rson;
S[k].rson = x;
S[K].SZ = S[X].SZ;
S[X].SZ = s[s[x].lson].sz + s[s[x].rson].sz + 1;
x = k;
} void maintain (int &r, BOOL flag) {if (flag) {if (S[s[s[r].rson].rson].sz > S[s[r].lson].sz) {LR (R);
else if (S[s[s[r].rson].lson].sz > S[s[r].rson].sz) {RR (S[r].rson);
LR (R);
else return;
else {if (S[s[s[r].lson].lson].sz > S[s[r].rson].sz) {RR (R);
else if (S[s[s[r].lson].rson].sz > S[s[r].rson].sz) {LR (S[r].lson); RR (R);
else return;
} Maintain (S[r].lson, false);
Maintain (S[r].rson, true);
Maintain (R, false);
Maintain (R, true);
} void Insert (int &r, int k) {if (r = = 0) {r = ++tot; S[R].
Init ();
S[r].key = k;
else {s[r].sz++;
if (K < S[r].key) Insert (S[r].lson, k);
else Insert (S[r].rson, k);
Maintain (r, K >= S[r].key);
an int Remove (int &r, int k) {int d_key;
if (!r) return 0;
s[r].sz--; if (S[r].key = = k | | (S[r].lson = = 0 && k < s[r].key) | | (S[r].rson = = 0 && k > S[r].key))
{D_key = S[r].key;
if (S[r].lson && s[r].rson) S[r].key = Remove (S[r].lson, K + 1);
else R = S[r].lson + S[r].rson;
else Remove (K < S[r].key S[r].lson:s[r].rson, K);
} void Delete (int &r, int delay, int min_val) {if (!r) return;
if (S[r].key + delay < Min_val) {r = S[r].rson;
Delete (R, Delay, Min_val);
else {Delete (S[r].lson, delay, min_val);
S[R].SZ = s[s[r].rson].sz + s[s[r].lson].sz + 1; } int Gmax (inT &r) {while (S[r].rson) r = S[r].rson;
return R;
int gmin (int &r) {while (S[r].lson) r = S[r].lson;
return R;
int gpre (int &r, int y, int k) {if (r = = 0) return y;
if (k > S[r].key) gpre (S[r].rson, R, K);
else Gpre (S[r].lson, y, K);
int gnext (int &r, int y, int k) {if (r = = 0) return y;
if (K < S[r].key) GNext (S[r].lson, R, K);
else GNext (S[r].rson, y, K);
int gmax_kth (int &r, int k) {//non-repeated int t = S[S[R].RSON].SZ + 1;
if (t = = k) return s[r].key;
if (T < K) return gmax_kth (S[r].lson, k-t);
else return gmax_kth (S[r].rson, k);
int Grank (int &r, int k) {if (K < S[r].key) return Grank (S[r].lson, k);
else if (K > S[r].key) return Grank (S[r].rson, K) + S[s[r].lson].sz + 1;
else return S[S[R].LSON].SZ + 1;
} void Sort (int &r) {if (r = 0) return;
Sort (S[r].lson);
printf ("%d\n", S[r].key);
Sort (S[r].rson);
int main () {int q, delay, MV;
while (scanf ("%d%d", &q, &MV)!= EOF) {tot = delay = root = 0; while (q--) {char str[10];
int k;
scanf ("%s%d", str, &k);
if (str[0] = = ' I ') {if (K < MV) continue;
Insert (Root, K-delay);
else if (str[0] = = ' A ') delay + = k;
else if (str[0] = = ' F ') printf ("%d\n", S[root].sz < K?-1: (Gmax_kth (root, k) + delay));
else {delay = k;
Delete (root, delay, MV);
} printf ("%d\n", TOT-S[ROOT].SZ);
return 0;
}