zoukankan      html  css  js  c++  java
  • 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 ");
            else printf("%d ",query(a)+d);
        }
        //printf("%d %d ",val[root],sze[root]);
        }
        printf("%d",ans);
        return 0;
    }
  • 相关阅读:
    随机生成300道四则运算
    练习
    电梯演说模板练习
    敏捷开发
    团队模式
    思考
    build to win 观后感
    四则运算
    Code review
    Mutual review
  • 原文地址:https://www.cnblogs.com/mrsheep/p/8157130.html
Copyright © 2011-2022 走看看