1 #include <bits/stdc++.h>
2 using namespace std;
3 struct treap
4 {
5 int l, r, siz, pri, key, val;
6 }a[100005];
7 int ans, ptot;
8
9 void push_up(int k)
10 {
11 a[k].siz = a[a[k].l].siz + a[a[k].r].siz + a[k].val;
12 }
13
14 void lturn(int &k)
15 {
16 int tmp = a[k].r;
17 a[k].r = a[tmp].l, a[tmp].l = k;
18 a[tmp].siz = a[k].siz, push_up(k), k = tmp;
19 }
20
21 void rturn(int &k)
22 {
23 int tmp = a[k].l;
24 a[k].l = a[tmp].r, a[tmp].r = k;
25 a[tmp].siz = a[k].siz, push_up(k), k = tmp;
26 }
27
28 void insert(int &k, int x)
29 {
30 if(!k)
31 {
32 k = ++ptot, a[k].siz = a[k].val = 1;
33 a[k].key = x, a[k].pri = rand();
34 return;
35 }
36 ++a[k].siz;
37 if(x == a[k].key) ++a[k].val;
38 else if(x > a[k].key)
39 {
40 insert(a[k].l, x);
41 if(a[a[k].l].pri > a[k].pri) rturn(k);
42 }
43 else
44 {
45 insert(a[k].r, x);
46 if(a[a[k].r].pri > a[k].pri) lturn(k);
47 }
48 }
49
50 void del(int &k, int x)
51 {
52 if(!k) return;
53 if(x <= a[k].key) del(a[k].r, x), push_up(k);
54 else
55 {
56 ans += a[a[k].r].siz + a[k].val;
57 a[k].r = 0, k = a[k].l;
58 del(k, x), push_up(k);
59 }
60 }
61
62 int find_kth(int k, int x)
63 {
64 if(!k) return -1;
65 if(x <= a[a[k].l].siz) return find_kth(a[k].l, x);
66 if(x <= a[a[k].l].siz + a[k].val) return k;
67 return find_kth(a[k].r, x - a[a[k].l].siz - a[k].val);
68 }
69
70 int main()
71 {
72 int n, m, k, root = 0, lazy = 0;
73 char op[5];
74 scanf("%d%d", &n, &m);
75 srand(n);
76 while(n--)
77 {
78 scanf("%s%d", op, &k);
79 if(op[0] == ‘I‘)
80 {
81 if(k < m) continue;
82 insert(root, k - lazy);
83 }
84 if(op[0] == ‘A‘) lazy += k;
85 if(op[0] == ‘S‘)
86 {
87 lazy -= k;
88 del(root, m - lazy);
89 }
90 if(op[0] == ‘F‘)
91 {
92 k = find_kth(root, k);
93 printf("%d\n", ~k ? a[k].key + lazy : -1);
94 }
95 }
96 printf("%d\n", ans);
97 return 0;
98 }