1 program hehe;
2 type
3 shu=record
4 l,r,h,w,s,y:longint;
5 end;
6 var
7 c:char;
8 leave,add,min,size,root,n,i,j,k:longint;
9 x:array[0..100000] of shu;
10
11 procedure update(a:longint);
12 begin
13 x[a].s:=x[x[a].l].s+x[x[a].r].s+x[a].w;
14 end;
15
16 procedure lturn(var a:longint);
17 var
18 t:longint;
19 begin
20 t:=x[a].r;
21 x[a].r:=x[t].l;
22 x[t].l:=a;
23 x[t].s:=x[a].s;
24 update(a);
25 a:=t;
26 end;
27
28 procedure rturn(var a:longint);
29 var
30 t:longint;
31 begin
32 t:=x[a].l;
33 x[a].l:=x[t].r;
34 x[t].r:=a;
35 x[t].s:=x[a].s;
36 update(a);
37 a:=t;
38 end;
39
40 procedure insert(var a:longint;b:longint);
41 begin
42 if a=0 then
43 begin
44 inc(size);
45 a:=size;
46 x[a].s:=1;
47 x[a].w:=1;
48 x[a].h:=b;
49 x[a].y:=random(maxlongint);
50 exit;
51 end;
52 inc(x[a].s);
53 if x[a].h=b then
54 begin
55 inc(x[a].w);
56 exit;
57 end;
58 if b>x[a].h then
59 begin
60 insert(x[a].r,b);
61 if x[x[a].r].y<x[a].y then lturn(a);
62 exit;
63 end;
64 insert(x[a].l,b);
65 if x[x[a].l].y<x[a].y then rturn(a);
66 end;
67
68 function delete(var a:longint;b:longint):longint;
69 var
70 t:longint;
71 begin
72 if a=0 then exit(0);
73 if x[a].h<b then
74 begin
75 t:=x[x[a].l].s+x[a].w;
76 a:=x[a].r;
77 exit(t+delete(a,b));
78 end;
79 t:=delete(x[a].l,b);
80 x[a].s:=x[a].s-t;
81 exit(t);
82 end;
83
84 function find(a,b:longint):longint;
85 begin
86 if x[x[a].l].s+x[a].w<b then exit(find(x[a].r,b-x[x[a].l].s-x[a].w));
87 if x[x[a].l].s>=b then exit(find(x[a].l,b));
88 exit(x[a].h+add);
89 end;
90
91 begin
92 readln(n,min);
93 for i:=1 to n do
94 begin
95 read(c);
96 readln(k);
97 case c of
98 ‘I‘:if k>=min then insert(root,k-add);
99 ‘A‘:add:=add+k;
100 ‘S‘:begin
101 add:=add-k;
102 leave:=leave+delete(root,min-add);
103 end;
104 ‘F‘:if x[root].s<k then writeln(‘-1‘)
105 else writeln(find(root,x[root].s-k+1));
106 end;
107 end;
108 writeln(leave);
109 end.