#include<bits/stdc++.h>
#define N 100005
using namespace std;
int rt,val[N],lch[N],rch[N],pro[N],sz[N],cnt;
int rnd()
{
static int sd=4234;
return sd*=3423;
}
int node(int v)
{
val[++cnt]=v;
pro[cnt]=rnd();
sz[cnt]=1;
return cnt;
}
void update(int t)
{
sz[t]=sz[lch[t]]+sz[rch[t]]+1;
}
void split(int t,int v,int & a,int & b)
{
if(!t)
{
a=b=0;
return;
}
if(v<=val[t])
{
b=t;
split(lch[t],v,a,lch[b]);
}
else
{
a=t;
split(rch[t],v,rch[a],b);
}
update(t);
}
int merge(int a,int b)
{
if(!(a&&b))
{
return a+b;
}
if(pro[a]<pro[b])
{
lch[b]=merge(a,lch[b]);
update(b);
return b;
}
else
{
rch[a]=merge(rch[a],b);//
update(a);
return a;
}
}
int find(int k)
{
int t=rt;
while(k!=sz[rch[t]]+1)
{
if(k<=sz[rch[t]]) t=rch[t];
else k-=sz[rch[t]]+1, t=lch[t];
}
return val[t];
}
int main()
{
int n,mn,x,y,z,o=0,ans=0;
char s[10];
scanf("%d%d",&n,&mn);
while(n--)
{
scanf("%s%d",s,&x);
if(s[0]=='A') o-=x;
else if(s[0]=='S') o+=x, split(rt,o+mn,y,rt), ans+=sz[y];
else if(s[0]=='I') if(x>=mn) split(rt,x+o,y,z), rt=merge(merge(y,node(x+o)),z); else;
else if(s[0]=='F') printf("%d\n",sz[rt]>=x ? find(x)-o : -1);
}
printf("%d\n",ans);
}
非旋treap
最新推荐文章于 2022-08-11 14:52:51 发布