1 #include <cstdio>
2 #include <cstring>
3 #include <ctime>
4 #include <iostream>
5 #include <cstdlib>
6 using namespace std;
7 int minn,ans;
8 struct Treap
9 {
10 Treap *ch[2];
11 int size,val,key;
12 Treap(){val=size=0;key=rand();ch[1]=ch[0]=NULL;}
13 inline void update()
14 {size=ch[0]->size+ch[1]->size+1;}
15 }*null=new Treap,*root=null;
16 typedef pair<Treap*,Treap*> D;
17 inline Treap* newTreap(int v)
18 {
19 Treap *o=new Treap();
20 o->ch[0]=o->ch[1]=null;
21 o->size=1;o->val=v;
22 return o;
23 }
24 Treap* merge(Treap* a,Treap* b)
25 {
26 if(a==null)return b;
27 if(b==null)return a;
28 if(a->key < b->key)
29 {a->ch[1]=merge(a->ch[1],b);a->update();return a;}
30 else
31 {b->ch[0]=merge(a,b->ch[0]);b->update();return b;}
32 }
33 D split(Treap *o,int k)
34 {
35 if(o==null)return D(null,null);
36 D y;
37 if(o->ch[0]->size >= k)
38 {y=split(o->ch[0],k);o->ch[0]=y.second;o->update();y.second=o;}
39 else
40 {y=split(o->ch[1],k-o->ch[0]->size-1);o->ch[1]=y.first;o->update();y.first=o;}
41 return y;
42 }
43 int getrank(Treap *o,int val)
44 {
45 if(o==null)return 0;
46 return o->val >= val?getrank(o->ch[0],val):getrank(o->ch[1],val)+o->ch[0]->size+1;
47 }
48 inline int getval(int rank)
49 {
50 D x=split(root,rank-1);
51 D y=split(x.second,1);
52 int ans=y.first->val;
53 root=merge(merge(x.first,y.first),y.second);
54 return ans;
55 }
56 inline void insert(int val)
57 {
58 int k=getrank(root,val);
59 D x=split(root,k);
60 Treap *o=newTreap(val);
61 root=merge(merge(x.first,o),x.second);
62 }
63 inline void erase()
64 {
65 D x=split(root,1);
66 root=x.second;ans++;
67 }
68 int main()
69 {
70 int m,x,tmp=0;char s[5];
71 scanf("%d%d",&m,&minn);
72 while(m--)
73 {
74 scanf("%s%d",s,&x);
75 switch(s[0])
76 {
77 case ‘I‘:if(x>=minn)insert(x-tmp);break;
78 case ‘F‘:
79 {
80 if(root==null||root->size<x)printf("-1\n");
81 else printf("%d\n",getval(root->size-x+1)+tmp);
82 break;
83 }
84 case ‘A‘:tmp+=x;break;
85 case ‘S‘:
86 {tmp-=x;while(root!=null&&getval(1)+tmp<minn)erase();break;}
87 }
88 }
89 printf("%d",ans);
90 }