1 /*Author:WNJXYK*/
2 #include<cstdio>
3 using namespace std;
4
5 int Delta=0;
6
7 const int Maxn=1000000;
8 struct SBT{
9 int left;
10 int right;
11 int size;
12 int key;
13 };
14 SBT tree[Maxn+10];
15 int root,cnt;
16
17 inline void rotate_l(int &x){
18 int y=tree[x].right;
19 tree[x].right=tree[y].left;
20 tree[y].left=x;
21 tree[y].size=tree[x].size;
22 tree[x].size=1+tree[tree[x].left].size+tree[tree[x].right].size;
23 x=y;
24 }
25
26 inline void rotate_r(int &x){
27 int y=tree[x].left;
28 tree[x].left=tree[y].right;
29 tree[y].right=x;
30 tree[y].size=tree[x].size;
31 tree[x].size=tree[tree[x].left].size+1+tree[tree[x].right].size;
32 x=y;
33 }
34
35 void maintain(int &x,bool flag){
36 //printf("MainTain %d\n",x);
37 if (flag==false){
38 if (tree[tree[tree[x].left].left].size>tree[tree[x].right].size){
39 rotate_r(x);
40 }
41 else if (tree[tree[tree[x].left].right].size>tree[tree[x].right].size){
42 rotate_l(tree[x].left);
43 rotate_r(x);
44 }else return ;
45 }else{
46 if (tree[tree[tree[x].right].right].size>tree[tree[x].left].size){
47 rotate_l(x);
48 }else if (tree[tree[tree[x].right].left].size>tree[tree[x].left].size){
49 rotate_r(tree[x].right);
50 rotate_l(x);
51 }else return ;
52 }
53 maintain(tree[x].left,false);
54 maintain(tree[x].right,true);
55 maintain(x,true);
56 maintain(x,false);
57 }
58 void insert(int &x,int sp){
59 if (!x){
60 x=++cnt;
61 tree[x].left=tree[x].right=0;
62 tree[x].size=1;
63 tree[x].key=sp;
64 }else{
65 tree[x].size++;
66 if (sp<tree[x].key){
67 insert(tree[x].left,sp);
68 }else{
69 insert(tree[x].right,sp);
70 }
71 maintain(x,sp>=tree[x].key);
72 }
73 }
74
75 /*
76 int del(int &x,int sp){
77 tree[x].size--;
78 if (sp==tree[x].key || ( sp<tree[x].key && tree[x].left==0) || (sp>tree[x].key && tree[x].right==0)){
79 int y=tree[x].key;
80 if (tree[x].left==0 ||tree[x].right==0){
81 x=tree[x].left+tree[x].right;
82 }else{
83 tree[x].key=del(tree[x].left,tree[x].key+1);
84 }
85 return y;
86 }else{
87 if (sp<tree[x].key){
88 return del(tree[x].left,sp);
89 }else{
90 return del(tree[x].right,sp);
91 }
92 }
93 }*/
94
95 void del(int &x,int mink){
96 if (x==0) return;
97 if (tree[x].key+Delta<mink)
98 x=tree[x].right,del(x,mink);
99 else
100 del(tree[x].left,mink),tree[x].size=tree[tree[x].left].size+1+tree[tree[x].right].size;
101 }
102
103 inline int getMax(){
104 int i;
105 for (i=root;tree[i].right;i=tree[i].right);
106 return tree[i].key;
107 }
108
109 inline int getMin(){
110 int i;
111 for (i=root;tree[i].left;i=tree[i].left);
112 return tree[i].key;
113 }
114
115 int rank(int &x,int sp){
116 if (sp<tree[x].key){
117 return rank(tree[x].left,sp);
118 }else if (sp>tree[x].key){
119 return rank(tree[x].right,sp)+tree[tree[x].left].size+1;
120 }
121 return tree[tree[x].left].size+1;
122 }
123
124 int select(int &x,int rak){
125 int rk=tree[tree[x].left].size+1;
126 if (rak<rk){
127 return select(tree[x].left,rak);
128 }else if (rak>rk){
129 return select(tree[x].right,rak-rk);
130 }
131 return tree[x].key;
132 }
133
134 int pred(int &x,int y,int sp){
135 if (x==0) return y;
136 if (tree[x].key<sp){
137 return pred(tree[x].right,x,sp);
138 }
139 return pred(tree[x].left,y,sp);
140 }
141
142 int succ(int &x,int y,int sp){
143 if (x==0) return y;
144 if (tree[x].key>sp){
145 return succ(tree[x].left,x,sp);
146 }
147 return succ(tree[x].right,y,sp);
148 }
149
150 inline void init(){
151 root=cnt=0;
152 }
153
154 int main(){
155 init();
156 int n,mink;
157 scanf("%d%d",&n,&mink);
158 for (int i=1;i<=n;i++){
159 char c[10];int num;
160 scanf("%s%d",&c,&num);
161 if (c[0]==‘A‘) Delta+=num;
162 if (c[0]==‘S‘) {
163 Delta-=num;
164 del(root,mink);
165 }
166 if (c[0]==‘I‘) {
167 if (num<mink) continue;
168 insert(root,num-Delta);
169 }
170 if (c[0]==‘F‘){
171 if (num>tree[root].size){
172 printf("-1\n");
173 continue;
174 }
175 int Ans=select(root,tree[root].size-num+1);
176 printf("%d\n",Ans+Delta);
177 }
178 }
179 printf("%d\n",cnt-tree[root].size);
180 return 0;
181 }