郁闷的出纳员

splay真的很慢
#include<stdio.h>
#include<ctype.h>
#define N 100005
#define INF 100000007
int m,lowst,o,tmp,leave;
char c;
int root,fa[N],ch[N][2],val[N],size[N],cnt;
int son(int t) {
    return t==ch[fa[t]][1];
}
void pushup(int t) {
    size[t]=size[ch[t][0]]+size[ch[t][1]]+1;
}
void node(int v,int p) {
    cnt++;
    val[cnt]=v, fa[cnt]=p, size[cnt]=1;
}
void rotate(int t) {
    int p=fa[t], d=son(t);
    ch[p][d]=ch[t][!d];
    if(ch[t][!d]) fa[ch[t][!d]]=p;
    fa[t]=fa[p];
    if(fa[p]) ch[fa[p]][son(p)]=t;
    else root=t;
    fa[p]=t, ch[t][!d]=p;
    pushup(p), pushup(t);
}
void splay(int t,int r) {
    while(fa[t]!=r) {
        int p=fa[t];
        if(fa[p]==r) rotate(t);
        else {
            if(son(t)==son(p)) rotate(p), rotate(t);
            else rotate(t), rotate(t);
        }
    }
}
void insert(int v) {
    if(!root) {
        node(v,0);
        root=cnt;
        return;
    }
    int t=root;
    while(ch[t][v>val[t]]) t=ch[t][v>val[t]];
    node(v,t);
    splay(ch[t][v>val[t]]=cnt,0);
}
int suc(int v) {
    int t=root,ret=0;
    while(t) {
        if(v>val[t]) t=ch[t][1];
        else ret=t, t=ch[t][0];
    }
    return ret;
}
void erase() {
    int t=suc(lowst-o);
    splay(t,0);
    leave+=size[ch[t][0]];
    size[t]-=size[ch[t][0]];
    ch[t][0]=0;
}
int select(int k) {
    int t=root,sz;
    while(sz=size[ch[t][1]],k!=sz+1) {
        if(k<=sz) t=ch[t][1];
        else k-=sz+1, t=ch[t][0];
    }
    return t ? val[t]+o : -1;
}
int main() {
    scanf("%d%d",&m,&lowst);
    insert(INF);
    while(m--) {
        while(isspace(c=getchar()));
        scanf("%d",&tmp);
        switch(c) {
            case 'I': if(tmp>=lowst) insert(tmp-o); break;
            case 'A': o+=tmp; break;
            case 'S': o-=tmp; erase(); break;
            case 'F': printf("%d\n",select(tmp+1)); break;
        }
    }
    printf("%d\n",leave);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值