233

#include<cstdio>
#include<algorithm>
#define N 200010
#define which(u) (ls[f[(u)]]==(u))
using namespace std;
int n,m,a,f[N],ls[N],rs[N],val[N],d,tot,root,sze[N],ans;
char s[10];
int read()
{
    int ans=0,fu=1;
    char j=getchar();
    for (;j<'0' || j>'9';j=getchar()) if (j=='-') fu=-1;
    for (;j>='0' && j<='9';j=getchar()) ans*=10,ans+=j-'0';
    return ans*fu;
}
void updt(int x)
{
    sze[x]=sze[ls[x]]+sze[rs[x]]+1;
}
void Rotate(int u)
{
    int v = f[u], w = f[v], b = which(u) ? rs[u] : ls[u];
    if(w) which(v) ? ls[w] = u : rs[w] = u;
    which(u) ? (ls[v] = b, rs[u] = v) : (rs[v] = b, ls[u] = v);
    f[u] = w, f[v] = u;
    if(b) f[b] = v;
    updt(v), updt(u);
}
void Splay(int u, int tar)
{
    while(f[u]!=tar)
    {
    if(f[f[u]]!=tar)
    {
        if(which(u)==which(f[u])) Rotate(f[u]);
        else Rotate(u);
    }
    Rotate(u);
    }
    if(!tar) root=u;
}
void insert(int x)
{
    int u=root,v=0;
    while(u)
    {
    v=u;
    if(x<=val[u]) u=ls[u];
    else u=rs[u];
    }
    f[++tot]=v,val[tot]=x,sze[tot]=1;
    if(v) x<=val[v]?ls[v]=tot:rs[v]=tot;
    Splay(tot, 0);
}
int query(int x)
{
    int u=root;
    while (x && (x-1)!=sze[rs[u]])
    {
    if (sze[rs[u]]>=x) u=rs[u];
    else x-=(sze[rs[u]]+1),u=ls[u];
    }
    return val[u];
}
int getmn(int x)
{
    while (ls[x]) x=ls[x];
    return x;
}
int main()
{
    n=read();
    m=read();
    for (int i=1;i<=n;i++)
    {
    scanf("%s",s);
    a=read();
    if (s[0]=='I')
        if (a>=m) insert(a-d);
    if (s[0]=='S')
    {
        d-=a;
        insert(m-d);
        ans+=sze[ls[tot]];
        f[root=rs[root]]=0;
    }
    if (s[0]=='A') d+=a;
    if (s[0]=='F')
    {
        if (sze[root]<a) printf("-1\n");
        else printf("%d\n",query(a)+d);
    }
    //printf("%d %d\n",val[root],sze[root]);
    }
    printf("%d",ans);
    return 0;
}
posted @ 2017-12-31 09:46  MSPqwq  阅读(114)  评论(0编辑  收藏  举报