1 #include<cstdio>
2 #include<cstdlib>
3 using namespace std;
4 #define maxn 100010
5 struct SBT
6 {
7 int l,r,sz,key;
8 void init(){l = r = key = 0;sz = 1;}
9 }tree[maxn];
10 int root,tot;
11 inline void zag(int &rt)
12 {
13 int k = tree[rt].r;
14 tree[rt].r = tree[k].l;
15 tree[k].l = rt;
16 tree[k].sz = tree[rt].sz;
17 tree[rt].sz = tree[tree[rt].l].sz+tree[tree[rt].r].sz+1;
18 rt = k;
19 }
20 inline void zig(int &rt)
21 {
22 int k = tree[rt].l;
23 tree[rt].l = tree[k].r;
24 tree[k].r = rt;
25 tree[k].sz = tree[rt].sz;
26 tree[rt].sz = tree[tree[rt].l].sz+tree[tree[rt].r].sz+1;
27 rt = k;
28 }
29 inline void maintain(int &rt,int flag)
30 {
31 if (flag)
32 {
33 if (tree[tree[tree[rt].r].r].sz > tree[tree[rt].l].sz)
34 zag(rt);
35 else if (tree[tree[tree[rt].r].l].sz > tree[tree[rt].l].sz)
36 {
37 zig(tree[rt].r); zag(rt);
38 }
39 else return;
40 }
41 else
42 {
43 if (tree[tree[tree[rt].l].l].sz > tree[tree[rt].r].sz)
44 zig(rt);
45 else if (tree[tree[tree[rt].l].r].sz > tree[tree[rt].r].sz)
46 {
47 zag(tree[rt].l); zig(rt);
48 }
49 else return;
50 }
51 maintain(tree[rt].l,false);
52 maintain(tree[rt].r,true);
53 maintain(rt,false);
54 maintain(rt,true);
55 }
56 inline void insert(int &rt,int k)
57 {
58 if (rt == 0)
59 {
60 rt = ++tot;
61 tree[rt].init();
62 tree[rt].key = k;
63 }
64 else
65 {
66 tree[rt].sz++;
67 if (k < tree[rt].key)
68 insert(tree[rt].l,k);
69 else insert(tree[rt].r,k);
70 maintain(rt,k>=tree[rt].key);
71 }
72 }
73 inline void del(int &rt,int delay,int min_val)
74 {
75 if (!rt) return;
76 if (tree[rt].key+delay<min_val)
77 {
78 rt = tree[rt].r;
79 del(rt,delay,min_val);
80 }
81 else
82 {
83 del(tree[rt].l,delay,min_val);
84 tree[rt].sz=tree[tree[rt].l].sz+tree[tree[rt].r].sz+1;
85 }
86
87 }
88 inline int kth(int &rt,int k)
89 {
90 int tmp = tree[tree[rt].r].sz + 1;
91 if (tmp == k)
92 return tree[rt].key;
93 else if (tmp < k)
94 return kth(tree[rt].l,k - tmp);
95 return kth(tree[rt].r,k);
96 }
97 int main()
98 {
99 freopen("1503.in","r",stdin);
100 freopen("1503.out","w",stdout);
101 int n,min_val,delay = 0;
102 scanf("%d %d\n",&n,&min_val);
103 int x;
104 char cmd;
105 while (n--)
106 {
107 scanf("%c %d\n",&cmd,&x);
108 if (cmd == ‘I‘) {if (x >= min_val)insert(root,x-delay);}
109 else if (cmd == ‘A‘) delay += x;
110 else if (cmd == ‘F‘) printf("%d\n",tree[root].sz<x?-1:kth(root,x)+delay);
111 else
112 {
113 delay -= x;
114 del(root,delay,min_val);
115 }
116 }
117 printf("%d\n",tot - tree[root].sz);
118 return 0;
119 }