rt,输出全是-1
#include<bits/stdc++.h>
using namespace std;
const int N=100010;
int n,m,del;
int root,idx;
struct node{
int ch[2];
int fa,v,size;
void init(int _v,int _fa){
v=_v,fa=_fa;
size=1;
}
}tr[N];
inline int read(){
int x=0,f=0;
char c=getchar();
while(c<'0' or c>'9'){if(c=='-')f=1;c=getchar();}
while(c>='0' and c<='9')x=(x<<3)+(x<<1)+c-'0',c=getchar();
if(f)return -x;
else return x;
}
void pushup(int x){
tr[x].size=tr[tr[x].ch[0]].size+1+tr[tr[x].ch[1]].size;
}
void rotate(int x){
int y=tr[x].fa,z=tr[x].fa;
bool k=tr[y].ch[1]==x;
tr[z].ch[tr[z].ch[1]==y]=x,tr[x].fa=z;
tr[x].ch[k^1]=y,tr[y].fa=x;
tr[y].ch[k]=tr[x].ch[k^1],tr[tr[x].ch[k^1]].fa=y;
pushup(y),pushup(x);
}
void splay(int x,int k){
while(tr[x].fa!=k){
int y=tr[x].fa,z=tr[y].fa;
if(z!=k){
if((tr[y].ch[1]==x)^(tr[z].ch[1]==y))rotate(x);
else rotate(y);
}
rotate(x);
}
if(!k)root=x;
}
void insert(int v){
int u=root,fa=0;
while(u)fa=u,u=tr[u].ch[v>tr[u].v];
u=++idx;
if(fa)tr[fa].ch[v>tr[fa].v]=u;
tr[u].init(v,fa);
splay(v,0);
}
int get_k(int k){
int u=root;
while(tr[u].size>=k){
if(tr[tr[u].ch[0]].size>=k)u=tr[u].ch[0];
else if(tr[tr[u].ch[0]].size==k-1)return u;
else k-=tr[tr[u].ch[0]].size+1,u=tr[u].ch[1];
}
return -1;
}
int next(int v){
int u=root,res;
while(u){
if(tr[u].v>=v)res=u,u=tr[u].ch[0];
else u=tr[u].ch[1];
}
return res;
}
int main(){
n=read(),m=read();
del=0;
int ans=0,tot=0;
while(n--){
char op=getchar();
int k=read();
if(op=='I')if(k>=m)insert(k-del),tot++;
if(op=='A')del+=k;
if(op=='S'){
del-=k;
int R=next(m-del);
splay(R,0);
ans+=tr[tr[R].ch[0]].size;
tot-=tr[tr[R].ch[0]].size;
tr[R].ch[0]=0;
}
if(op=='F'){
cout<<get_k(tot-k+1)<<endl;
}
}
cout<<ans<<endl;
}