[NOI 2004]郁闷的出纳员

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

题解

这道题写的比较丑...

首先看到是动态的值的维护,由题面很容易想到用平衡树做。

由于需要批量删除区间中的数(工资低于下界),我们想到用$Splay$,

用差分来修改每个人的工资,当工资降低时,我们做一次删除操作。

但是注意会有数值相等的情况...这就比较麻烦啦..

我的做法是暴力去删除的数中找回误删的数再加回来...

效率就比较低了。

 

#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=100000;
const int M=200000;

struct node
{
    int key,up;
    node *father,*child[2];
} splay[N+5],*pos,*root,*Minr;
int n,x,m,tol,cnt;
char c;

void Insert(node* f,node* &r,int key);
void Upper(node* r,int key);
void Splay(node* r,node *end);
void NewNode(node* &f,node* &r,int key);
void Rotate(node* &r,bool t);
int Count(node* r);
node* Ans(node* r,int rank,int cnt);
int Read();
int C(node* r,int key)
{
    if (!r) return 0;
    if (r->key==key) return C(r->child[1],key)+1;
    if (r->key<key) return C(r->child[1],key);
    return C(r->child[0],key);
}

int main()
{
    int q=0;
    pos=splay;
    root=NULL;
    Insert(0,root,M+1);
    scanf("%d%d",&n,&m);
    while (n--)
    {
        c=0;
        while(c!='I'&&c!='S'&&c!='F'&&c!='A') c=getchar();
        if (c=='A')
        {
            x=Read();
            m-=x;
            tol+=x;
        }
        else if (c=='S')
        {
            x=Read();
            m+=x;
            tol-=x;
            Minr=NULL;
            Upper(root,m-1);
            if (Minr)
            {
                Splay(Minr,NULL);
                int o=C(root->child[0],root->key);
                        root->child[0]=NULL;
                for (int i=1;i<=o;i++) 
                {
                    
                Insert(0,root,root->key);
                }
        
            }
        }
        else if (c=='I')
        {
            x=Read();
            if (x-tol>=m)
            {
                Insert(0,root,x-tol);
                cnt++;
            }
        }
        else if (c=='F')
        {
            q++;
            x=Read();
            node* ans=Ans(root,x+1,0);
            printf("%d\n",ans ? ans->key+tol:-1);
        }
    }
    printf("%d\n",cnt-Count(root)+1);
    return 0;
}
 
int Read()
{
    int sum=0;
    char c=0;
    while (c<'0'||c>'9') c=getchar();
    while (c>='0'&&c<='9')
    {
        sum=sum*10+c-'0';
        c=getchar();
    }
    return sum;
}
void Insert(node* f,node* &r,int key)
{
    if (!r)
    {
        NewNode(f,r,key);
        Splay(r,NULL);
        return;
    }
    if (key>=r->key)
    {
        r->up++;
        Insert(r,r->child[1],key);
    }
    else Insert(r,r->child[0],key);
}
void Upper(node* r,int key)
{
    if (!r) return;
    if (key<r->key)
    {
        if (Minr==NULL||r->key<Minr->key) Minr=r;
        Upper(r->child[0],key);
    }
    else Upper(r->child[1],key);
}
void Splay(node* r,node *end)
{
    while(r->father!=end)
    {
        if (r->father->father==end) Rotate(r,r->father->child[0]==r);
        else
        {
            node* y=r->father;
            bool t=y->father->child[0]==y;
            if (y->child[t]==r) Rotate(r,!t);
            else Rotate(y,t);
            Rotate(r,t);
        }
    }
    if (!end) root=r;
}
void NewNode(node* &f,node* &r,int key)
{
    r=pos++;
    r->father=f;
    r->key=key;
    r->child[0]=r->child[1]=NULL;
    r->up=1;
}
void Rotate(node* &r,bool t)
{
    node *y=r->father;
    y->child[!t]=r->child[t];
    if (r->child[t]) r->child[t]->father=y;
    if (y->father) y->father->child[y->father->child[1]==y]=r;
    r->father=y->father;
    r->child[t]=y;
    y->father=r;
    r->up=Count(r->child[1])+1;
    y->up=Count(y->child[1])+1;
}
int Count(node* r)
{
    if (!r) return 0;
    return Count(r->child[0])+r->up;
}
node* Ans(node* r,int rank,int cnt)
{
    if (!r) return 0;
    if (cnt+r->up==rank) return r;
    if (cnt+r->up>rank) return Ans(r->child[1],rank,cnt);
    if (cnt+r->up<rank) return Ans(r->child[0],rank,cnt+r->up);
}

 

posted @ 2017-08-19 15:24  NaVi_Awson  阅读(185)  评论(0编辑  收藏  举报