bzoj 1503

1503: [NOI2004]郁闷的出纳员

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 12311  Solved: 4399
[Submit][Status][Discuss]

Description

OIER公司是一家大型专业化软件公司,有着数以万计的员工。作为一名出纳员,我的任务之一便是统计每位员工的工资。这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常调整员工的工资。如果他心情好,就可能把每位员工的工资加上一个相同的量。反之,如果心情不好,就可能把他们的工资扣除一个相同的量。我真不知道除了调工资他还做什么其它事情。工资的频繁调整很让员工反感,尤其是集体扣除工资的时候,一旦某位员工发现自己的工资已经低于了合同规定的工资下界,他就会立刻气愤地离开公司,并且再也不会回来了。每位员工的工资下界都是统一规定的。每当一个人离开公司,我就要从电脑中把他的工资档案删去,同样,每当公司招聘了一位新员工,我就得为他新建一个工资档案。老板经常到我这边来询问工资情况,他并不问具体某位员工的工资情况,而是问现在工资第k多的员工拿多少工资。每当这时,我就不得不对数万个员工进行一次漫长的排序,然后告诉他答案。好了,现在你已经对我的工作了解不少了。正如你猜的那样,我想请你编一个工资统计程序。怎么样,不是很困难吧?

Input

Output

输出文件的行数为F命令的条数加一。对于每条F命令,你的程序要输出一行,仅包含一个整数,为当前工资第k多的员工所拿的工资数,如果k大于目前员工的数目,则输出-1。输出文件的最后一行包含一个整数,为离开公司的员工的总数。

Sample Input

9 10
I 60
I 70
S 50
F 2
I 30
S 15
A 5
F 1
F 2

Sample Output

10
20
-1
2

HINT

 

I命令的条数不超过100000 A命令和S命令的总条数不超过100 F命令的条数不超过100000 每次工资调整的调整量不超过1000 新员工的工资不超过100000

 

Source

代码:

修改值时统一加一个标记。

继续模板

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=200009;
int n,mina;
int fa[MAXN],ch[MAXN][2],key[MAXN],cnt[MAXN],size[MAXN],root,sz;
void init()
{
    root=sz=0;
    memset(ch,0,sizeof(ch));
    memset(fa,0,sizeof(fa));
    memset(cnt,0,sizeof(cnt));
    memset(size,0,sizeof(size));
}
inline void clear(int x) { fa[x]=ch[x][0]=ch[x][1]=cnt[x]=size[x]=0; }
inline int get(int x) { return ch[fa[x]][1]==x; }
inline void update(int x)
{
    if(x){
        size[x]=cnt[x];
        if(ch[x][0]) size[x]+=size[ch[x][0]];
        if(ch[x][1]) size[x]+=size[ch[x][1]];
    }
}
inline void rotate(int x)
{
    int father=fa[x],ffather=fa[father],which=get(x);
    ch[father][which]=ch[x][!which];fa[ch[father][which]]=father;
    ch[x][!which]=father;fa[father]=x;
    fa[x]=ffather;
    if(ffather) ch[ffather][ch[ffather][1]==father]=x;
    update(father);
    update(x);
}
inline void splay(int x,int goal)
{
    for(int father;(father=fa[x])!=goal;rotate(x))
        if(fa[father]!=goal) rotate((get(x)==get(father)?father:x));
    if(goal==0) root=x;
}
inline void insert(int x)
{
    if(root==0) { root=++sz;fa[sz]=ch[sz][0]=ch[sz][1]=0;cnt[sz]=size[sz]=1;key[sz]=x;return; }
    int now=root,father=0;
    while(1){
        if(key[now]==x) { cnt[now]++;update(now);update(father);splay(now,0);return; }
        father=now;
        now=ch[father][key[now]<x];
        if(now==0){
            sz++;
            fa[sz]=father;
            ch[father][key[father]<x]=sz;
            ch[sz][0]=ch[sz][1]=0;
            cnt[sz]=size[sz]=1;
            key[sz]=x;
            update(father);
            splay(sz,0);
            return;
        }
    }
}
inline int findnode(int x)//找到x的节点的位置
{
    int now=root;
    while(1){
        if(x<key[now]) now=ch[now][0];
        else{
            if(key[now]==x) { splay(now,0);return now; }
            now=ch[now][1];
        }
    }
}
inline int find(int x)//找到x的排名
{
    int now=root,ans=0;
    while(1){
        if(x<key[now]) now=ch[now][0];
        else{
            if(ch[now][0]) ans+=size[ch[now][0]];
            if(x==key[now]) { splay(now,0);return ans+1; }
            ans+=cnt[now];
            now=ch[now][1];
        }
    }
}
inline int findx(int x)//找到排名为x的数
{
    int now=root;
    while(1){
        if(ch[now][0]&&x<=size[ch[now][0]]) now=ch[now][0];
        else{
            int tmp=(ch[now][0]?size[ch[now][0]]:0)+cnt[now];
            if(x<=tmp) return key[now];
            x=x-tmp;
            now=ch[now][1];
        }
    }
}
inline int pre()//找前驱
{
    int now=ch[root][0];
    while(ch[now][1]) now=ch[now][1];
    return now;
}
inline int suf()//找后继
{
    int now=ch[root][1];
    while(ch[now][0]) now=ch[now][0];
    return now;
}
inline void del(int x)//删去一个x
{
    find(x);
    if(cnt[root]>1) { cnt[root]--;update(root);return; }
    else if(!ch[root][0]&&!ch[root][1]) { root=sz=0;clear(root);return; }
    else if(!ch[root][0]){
        int oldroot=root;
        root=ch[root][1];
        fa[root]=0;
        clear(oldroot);
        return;
    }else if(!ch[root][1]){
        int oldroot=root;
        root=ch[root][0];
        fa[root]=0;
        clear(oldroot);
        return;
    }
    int leftbig=pre(),oldroot=root;
    splay(leftbig,0);
    ch[root][1]=ch[oldroot][1];
    fa[ch[root][1]]=root;
    clear(oldroot);
    update(root);
}
int main()
{
    //freopen("in.txt","r",stdin);
    init();
    insert(INF);
    insert(-INF);
    int tag=0,cnt=0;
    scanf("%d%d",&n,&mina);
    while(n--){
        char s[3];
        int x;
        scanf("%s%d",s,&x);
        if(s[0]=='I'){
            if(x<mina) continue;
            insert(x-tag);
            cnt++;
        }else if(s[0]=='A') tag+=x;
        else if(s[0]=='S'){
            tag-=x;
            insert(mina-tag);
            int tmp1=findnode(-INF);
            int tmp2=findnode(mina-tag);
            splay(tmp1,0);
            splay(tmp2,tmp1);
            ch[ch[root][1]][0]=0;
            del(mina-tag);
        }else{
            int tmp=find(INF)-2;
            if(tmp<x) puts("-1");
            else{
                int ans=findx(tmp+2-x);
                printf("%d\n",ans+tag);
            }
        }
    }
    int tmp=find(INF)-2;
    printf("%d\n",cnt-tmp);
    return 0;
}

 

转载于:https://www.cnblogs.com/--ZHIYUAN/p/7828315.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值