1 /**************************************************************
2 Problem: 1503
3 User: Tunix
4 Language: C++
5 Result: Accepted
6 Time:660 ms
7 Memory:3644 kb
8 ****************************************************************/
9
10 //BZOJ 1503
11 #include<vector>
12 #include<cstdio>
13 #include<cstring>
14 #include<cstdlib>
15 #include<iostream>
16 #include<algorithm>
17 #define rep(i,n) for(int i=0;i<n;++i)
18 #define F(i,j,n) for(int i=j;i<=n;++i)
19 #define D(i,j,n) for(int i=j;i>=n;--i)
20 #define pb push_back
21 using namespace std;
22 inline int getint(){
23 int v=0,sign=1; char ch=getchar();
24 while(ch<‘0‘||ch>‘9‘){ if (ch==‘-‘) sign=-1; ch=getchar();}
25 while(ch>=‘0‘&&ch<=‘9‘){ v=v*10+ch-‘0‘; ch=getchar();}
26 return v*sign;
27 }
28 const int N=1e5+1000,INF=~0u>>2;
29 typedef long long LL;
30 /******************tamplate*********************/
31 int c[N][2],fa[N],v[N],add[N],size[N],n,m,root,cnt;
32 #define L c[x][0]
33 #define R c[x][1]
34 void Push_up(int x){
35 size[x]=size[L]+size[R]+1;
36 }
37 void Rotate(int x){
38 int y=fa[x],z=fa[y],l= c[y][1]==x, r=l^1;
39 c[z][c[z][1]==y]=x;
40 fa[x]=z; fa[y]=x; fa[c[x][r]]=y;
41 c[y][l]=c[x][r]; c[x][r]=y;
42 Push_up(y);
43 }
44 void splay(int x,int y=0){
45 for(;fa[x];Rotate(x)){
46 y=fa[x];
47 if (y!=root)
48 Rotate( c[y][1]==x^c[fa[y]][1]==y ? x : y );
49 }
50 Push_up(x);
51 root=x;
52 }
53 int Find(int w){
54 int x=root,y;
55 while(x){
56 y=x;
57 if (v[x]>=w) x=L;
58 else x=R;
59 }
60 return y;
61 }
62 void Insert(int w){
63 if (!root){
64 v[++cnt]=w; root=cnt;
65 fa[cnt]=0; size[cnt]=1;
66 return;
67 }
68 int x=Find(w);
69 v[++cnt]=w;
70 fa[cnt]=x; size[cnt]=1;
71 if (v[x]>=w) L=cnt;
72 else R=cnt;
73 splay(cnt);
74 }
75 void Del(int w){
76 Insert(w);
77 root=c[root][1];
78 fa[root]=0;
79 }
80 int kth(int x,int k){
81 if (k==size[L]+1) return v[x];
82 else if (k<=size[L]) return kth(L,k);
83 else return kth(R,k-size[L]-1);
84 }
85 int main(){
86 #ifndef ONLINE_JUDGE
87 freopen("1503.in","r",stdin);
88 freopen("1503.out","w",stdout);
89 #endif
90 n=getint(); m=getint();
91 char cmd[5]; int x=0,ans=0,temp=0;
92 F(i,1,n){
93 scanf("%s",cmd); x=getint();
94 if (cmd[0]==‘I‘){
95 if (x>=m) Insert(x-temp),ans++;
96 }else if (cmd[0]==‘A‘){
97 temp+=x;
98 }else if (cmd[0]==‘S‘){
99 temp-=x;
100 Del(m-temp);
101 }else if (cmd[0]==‘F‘){
102 if (x>size[root]) puts("-1");
103 else printf("%d\n",kth(root,size[root]-x+1)+temp);
104 }
105 }
106 printf("%d\n",ans-size[root]);
107 return 0;
108 }