bzoj1503郁闷的出纳员 Splay

Splay维护插入,删除,由排名找值,裸题。
但是比较坑的一点是最后统计离开人数的时候,刚进来就离开的人是不计入答案的(再次重复我语文不好)。直接上代码。

#include<iostream>
#include<cstdio>
using namespace std;
int ch[100005][2];
int fa[100005];
int sz[100005];
int key[100005];
int cnt[100005];
bool dir(int x)
{   return ch[fa[x]][1]==x;}
int root,tot;
void up(int x)
{sz[x]=sz[ch[x][1]]+sz[ch[x][0]]+cnt[x];}
void rotate(int x)
{
    int y=fa[x];
    int z=fa[y];
    bool b=dir(x);
    if(z==0)
        root=x;
    else
        ch[z][dir(y)]=x;
    int a=ch[x][!b];
    fa[x]=z;ch[x][!b]=y;
    fa[y]=x;ch[y][b]=a;
    if(a)
        fa[a]=y;
    up(y);up(x);
}
void splay(int x,int i)
{
    while(fa[x]!=i)
    {
        int y=fa[x];
        int z=fa[y];
        bool b=dir(x),c=dir(y);
        if(z==i)
            rotate(x);
        else
        {
            if(b^c)
            {
                rotate(x);
                rotate(x);
            }
            else
            {
                rotate(y);
                rotate(x);
            }
        }
    }
}
int t1;
void find_pre(int k,int x)
{
    if(k==0)
        return ;
    if(key[k]<x)
    {
        t1=k;
        find_pre(ch[k][1],x);
    }
    else
        find_pre(ch[k][0],x);
}
int T;
void insert(int &k,int x,int last)
{
    if(k==0)
    {
        k=++tot;
        cnt[k]=1;
        sz[k]=1;
        fa[k]=last;
        if(!last)
            root=k;
        key[k]=x;
        splay(k,0);
        return ;
    }
    if(key[k]==x)
    {
        sz[k]++;
        cnt[k]++;
        splay(k,0);
        return ;
    }
    if(key[k]<x)
        insert(ch[k][1],x,k);
    else
        insert(ch[k][0],x,k);
}
int ans;
void del(int x)
{
    t1=0;
    find_pre(root,x);
    if(t1)
    {
        splay(t1,0);
        root=ch[t1][1];
        fa[root]=0;
        T-=sz[ch[t1][0]];
        ans+=sz[ch[t1][0]];
        T-=cnt[t1];
        ans+=cnt[t1];
        return ;
    }
}
int ask(int k,int rank)
{
    if(sz[ch[k][0]]>=rank)
        return ask(ch[k][0],rank);
    if(rank-sz[ch[k][0]]<=cnt[k])
        return key[k];
    return ask(ch[k][1],rank-sz[ch[k][0]]-cnt[k]);
}
char q[5];
int main()
{
    int n,minn=0,add=0;
    int x;
    scanf("%d%d",&n,&minn);
    for(int i=1;i<=n;i++)
    {
        scanf("%s",q);
        scanf("%d",&x);
        if(q[0]=='I')
        {
            x+=add;
            if(x>=minn)
            {
                insert(root,x,0);
                T++;
            }
        }
        if(q[0]=='A')
        {
            minn-=x;
            add-=x;
        }
        if(q[0]=='S')
        {
            minn+=x;
            add+=x;
            del(minn);
        }
        if(q[0]=='F')
        {
            if(x>T)
                printf("-1\n");
            else
                printf("%d\n",ask(root,T-x+1)-add);
        }
    }
    printf("%d",ans);
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值