您的位置:首页 > 其它

【bzoj1503】[NOI2004]郁闷的出纳员 Splay

2015-12-04 20:55 537 查看

Description

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

Input



Output

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

Sample Input

[code]9 10

I 60

I 70

S 50

F 2

I 30

S 15

A 5

F 1

F 2


Sample Output

[code]10

20

-1

2


HINT

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

Source

Splay

splay裸题

学会splay后,难点在main函数…

记一个delta表示当前全员工资的增减。splay保存相对大小。

插入x时插入x-delta,每次扣工资,插入把小于minn-delta的元素删掉。

查询时输出ans+delta

代码:

[code]#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int SZ = 1000010;
const int INF = 1000000010;

struct node{
    node *ch[2],*f;
    int sz,cnt,v;

    void maintain()
    {
        sz = ch[0] -> sz + ch[1] -> sz + cnt; 
    }

    void setc(node *x,int d)
    {
        (ch[d] = x) -> f = this;
    }

    int cmp(int x)
    {
        if(x == v) return -1;
        return x < v ? 0 : 1; 
    }

    int dir()
    {
        return f -> ch[1] == this;
    }   
}T[SZ], *root, *null;

int Tcnt = 0;

node* newnode(int x,node *f)
{
    node *k = T + (Tcnt ++);
    k -> f = f;
    k -> v = x;
    k -> ch[0] = k -> ch[1] = null;
    k -> sz = k -> cnt = 1;
    return k;
}

void rotate(node *p)
{
    node *fa = p -> f;
    int d = p -> dir();
    fa -> f -> setc(p,fa -> dir());
    fa -> setc(p -> ch[d ^ 1],d); fa -> maintain();
    p -> setc(fa,d ^ 1); p -> maintain();
    if(root == fa) root = p;
}

void splay(node *p,node *rt = null)
{
    while(p -> f != rt)
    {
        if(p -> f -> f == rt) rotate(p);
        else
        {
            if(p -> dir() == p -> f -> dir()) rotate(p -> f),rotate(p);
            else rotate(p),rotate(p);
        }
    }
}

void insert(node *p,int x)
{
    if(root == null) { root = newnode(x,null); return ; }
    while(p != null)
    {
        p -> sz ++;
        int d = p -> cmp(x);
        if(d == -1) { p -> cnt ++; break; }
        if(p -> ch[d] == null)
        {
            p -> ch[d] = newnode(x,p);
            p = p -> ch[d];
            break;
        }
        p = p -> ch[d];
    }
    splay(p);
}

void erase(node *p,int x)
{
    while(p != null)
    {
        p -> sz --;
        int d = p -> cmp(x);
        if(d == -1) { p -> cnt --; break; }
        p = p -> ch[d];
    }
    if(p -> cnt) return ;
    splay(p);

    if(p -> ch[0] == null) { root = p -> ch[1]; root -> f = null; return; }
    if(p -> ch[1] == null) { root = p -> ch[0]; root -> f = null; return; }

    p = p -> ch[0];
    while(p -> ch[1] != null) p = p -> ch[1];
    splay(p,root);
    p -> ch[1] = root -> ch[1]; p -> ch[1] -> f = p;
    p -> f = null; p -> maintain();
    root = p;
}

int ask_num(node *p,int k)
{
    while(p != null)
    {
        int l = p -> ch[1] -> sz + 1;
        int r = p -> ch[1] -> sz + p -> cnt;
        if(l <= k && k <= r) return p -> v;
        if(k > r) k -= r,p = p -> ch[0];
        else p = p -> ch[1];
    }
    return -1;
}

void init()
{
    null = newnode(-INF,null);
    null -> sz = null -> cnt = 0;
    root = null;
}

int main()
{
    init();
    int n,minn,tot = 0,delta = 0;
    scanf("%d%d",&n,&minn);
    while(n --)
    {
        char s[5];
        int x;
        scanf("%s%d",s,&x);
        if(s[0] == 'I')
        {
            if(x >= minn)
            {
                insert(root,x - delta);
                tot ++;
            }
        }
        else if(s[0] == 'A')
            delta += x;
        else if(s[0] == 'S')
        {
            delta -= x;
            insert(root,minn - delta);
            root -> ch[0] = null;
            root -> maintain();
            erase(root,minn - delta);
        }
        else
        {
            int ans = ask_num(root,x);
            printf("%d\n",ans == -1 ? -1 : ans + delta);
        }
    }
    printf("%d",tot - root -> sz);
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: