NOI2004 郁闷的出纳员

//http://www.lydsy.com/JudgeOnline/problem.php?id=1503
//http://blog.csdn.net/fp_hzq/article/details/6882519
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
 
const int maxn=2000005;
 
int L[maxn],R[maxn],S[maxn],V[maxn];
 
int cnt_node,root;
int sum,add,leave;
 
void Right_Rotate(int &t){
    int k=L[t];
    L[t]=R[k];
    R[k]=t;
    S[k]=S[t];
    S[t]=S[L[t]]+S[R[t]]+1;
    t=k;
}
 
void Left_Rotate(int &t){
    int k=R[t];
    R[t]=L[k];
    L[k]=t;
    S[k]=S[t];
    S[t]=S[L[t]]+S[R[t]]+1;
    t=k;
}
 
void maintain(int &t,bool flag){
    if(flag)
        if(S[R[R[t]]]>S[L[t]])
            Left_Rotate(t);
        else if(S[L[R[t]]]>S[L[t]])
            Right_Rotate(R[t]),Left_Rotate(t);
        else
            return ;
    else
        if(S[L[L[t]]]>S[R[t]])
            Right_Rotate(t);
        else if(S[R[L[t]]]>S[R[t]])
            Left_Rotate(L[t]),Right_Rotate(t);
        else
            return ;
    maintain(L[t],0);
    maintain(R[t],1);
    maintain(t,0);
    maintain(t,1);
}
 
void Init(){
    cnt_node=root=S[0]=0;
    add=sum=leave=0;
}
 
void Insert(int &t,int v){
    if(t){
        S[t]++;
        if(v<V[t])Insert(L[t],v);
        else Insert(R[t],v);
        maintain(t,v>=V[t]);
    }else{
        V[t=++cnt_node]=v;
        S[t]=1;
        L[t]=R[t]=0;
    }
}
/* v没有前驱的话就返回v 因为不知道是SBT中本身存在v
还是返回没有前驱,所以加一个p[maxn]标记一下*/
 
int Pred(int t,int v){
    if(!t)return v;
    if(v<=V[t]) return Pred(L[t],v);
    else{
        int tmp=Pred(R[t],v);
        return tmp==v?V[t]:tmp;
    }
}
 
int Succ(int t,int v){
    if(!t)return v;
    if(v>=V[t])return Succ(R[t],v);
    else{
        int tmp=Succ(L[t],v);
        return tmp==v?V[t]:tmp;
    }
}
 
int Find(int t,int v){
    while(t && v!=V[t]){
        t=v<V[t]?Find(L[t],v):Find(R[t],v);
    }
    return t;
}
 
int Rank(int t,int v){
    if(!t)return 1;
    if(v<=V[t])return Rank(L[t],v);
    else return S[L[t]]+1+Rank(R[t],v);
}
 
int Select(int t,int k){
    if(k==S[L[t]]+1)return V[t];
    if(k<=S[L[t]])return Select(L[t],k);
    else return Select(R[t],k-1-S[L[t]]);
}
//利用第一个比v小的前驱覆盖
int Delete(int &t ,int v){
    --S[t];
    if(v==V[t]|| v<V[t] && !L[t] ||v>V[t] && !R[t]){
        int tmp=V[t];
        if(!L[t] || !R[t])t=L[t]+R[t];
        else V[t]=Delete(L[t],V[t]+1);
        return tmp;
    }
    else if(v<V[t])return Delete(L[t],v);
    else return Delete(R[t],v);
}
 
int n,limit;
 
int main(){
    char ch[5];
	int k;
    int i,j,a,x,y;
    Init();
    scanf("%d%d",&n,&limit);
    for(i=0;i<n;i++){
        scanf("%s%d",ch,&k);
        if(ch[0]=='I'){
            if(k>=limit){
                Insert(root,k-add);
                sum++;
            }
        }else if(ch[0]=='A'){
            add+=k;
        }else if(ch[0]=='S'){
            add-=k;
            while(sum){
                a=Select(root,1);
                if(a+add>=limit)break;
                Delete(root,a);
                leave++;sum--;
            }
        }else if(ch[0]=='F'){
            if(sum<k){
                puts("-1");
                continue;
            }
            printf("%d\n",Select(root,sum-k+1)+add);
        }
    }
    printf("%d\n",leave);
    return 0;
}
/*
9 10
I 60
I 70
S 50
F 2
I 30
S 15
A 5
F 1
F 2
 
 
 
  */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值