博客园 首页 私信博主 显示目录 隐藏目录 管理 动画

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
 
 
 
 
平衡树直接扔
 
SBT:
#include<cstdio>
#include<algorithm>
using namespace std;

struct na{
    int l,r,k,s;
    na(){
        l=r=k=s=0;
    }
};
int root=0,num=0,ans=0;
long n,m,k;
na t[100001];
inline void rir(int &p){
    int q=t[p].l;
    t[p].l=t[q].r;
    t[q].r=p;
    t[q].s=t[p].s;
    t[p].s=t[t[p].l].s+t[t[p].r].s+1;
    p=q;
}
inline void ler(int &p){
    int q=t[p].r;
    t[p].r=t[q].l;
    t[q].l=p;
    t[q].s=t[p].s;
    t[p].s=t[t[p].l].s+t[t[p].r].s+1;
    p=q;
}
void ph(int &p,bool bo){
    if (!bo){
        if (t[t[p].r].s<t[t[t[p].l].l].s) rir(p);else
        if (t[t[p].r].s<t[t[t[p].l].r].s){
            ler(t[p].l);
            rir(p);
        }else return;
    }else{
        if (t[t[p].l].s<t[t[t[p].r].r].s) ler(p);else
        if (t[t[p].l].s<t[t[t[p].r].l].s){
            rir(t[p].r);
            ler(p);
        }else return;
    }
    ph(t[p].l,0);
    ph(t[p].r,1);
    ph(p,0);
    ph(p,1);
}
void insert(int &p,int key){
    if (p==0){
        num++;
        p=num;
        t[num].k=key;
        t[num].s=1;
    }else{
        t[p].s++;
        if (t[p].k>key)insert(t[p].l,key);
        else insert(t[p].r,key);
    }
    ph(p,t[p].k<=key);
}
void del(int &p,int key){
    if (p==0){
        return;
    }
    if (t[p].k<key){
        ans+=t[t[p].l].s+1;
        del(p=t[p].r,key);
    }else{
        del(t[p].l,key);
        t[p].s=t[t[p].l].s+t[t[p].r].s+1;
    }
    ph(p,1);
    ph(p,0);
}
int que(int p,int key){
    if (key==t[t[p].r].s+1) return t[p].k;
    if (key<t[t[p].r].s+1) return que(t[p].r,key);
    if (key>t[t[p].r].s+1) return que(t[p].l,key-t[t[p].r].s-1);
}
int main(){
    scanf("%d%d",&n,&m);
    k=0;
    for (int i=0;i<n;i++){
        char c[2];int b;
        scanf("%s%d",c,&b);
        if (c[0]=='I'){
            if (b>=m) insert(root,b-k);
        }
        if (c[0]=='A'){
            k+=b;
        }
        if (c[0]=='S'){
            k-=b;
            del(root,m-k);
        }
        if (c[0]=='F'){
            if (b>t[root].s) puts("-1");else
            printf("%d\n",que(root,b)+k);
        }
    }
    printf("%d",ans);
    return 0;
}

 

SPLAY:

#include<cstdio>
#include<algorithm>
using namespace std;

struct na{
    int l,r,k,s,f;
    na(){
        l=r=k=s=0;
    }
};
int root=0,num=0,ans=0,p,qq;
long n,m,k;
na t[100001];
char ch;
inline int read(){
    p=0;ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    while (ch>='0'&&ch<='9') p=p*10+ch-48, ch=getchar();
    return p;
}
inline bool getc(int x){
    return t[t[x].f].l==x;
}
inline void lturn(int &p){
    int k=t[p].r;
    t[k].f=t[p].f;
    t[p].f=k;
    t[t[k].l].f=p;
    t[p].r=t[k].l;
    t[k].l=p;
    t[k].s=t[p].s;
    t[p].s=t[t[p].l].s+t[t[p].r].s+1;
    p=k;
}
inline void rturn(int &p){
    int k=t[p].l;
    t[k].f=t[p].f;
    t[p].f=k;
    t[t[k].r].f=p;
    t[p].l=t[k].r;
    t[k].r=p;
    t[k].s=t[p].s;
    t[p].s=t[t[p].l].s+t[t[p].r].s+1;
    p=k;
}
inline void ph(int &x,bool bo){
    if (bo) rturn(x);else lturn(x);
}
inline void splay(int l){
    while (l!=root&&l){
        if (t[l].f==root){
            ph(root,getc(l));
            break;
        }
        int p=t[t[l].f].f;
        if (getc(l)==getc(t[l].f)){
            if (p==root){
                ph(root,getc(t[l].f));
                ph(root,getc(l));
                break;
            }else{
                if (getc(p)) ph(t[t[p].f].l,getc(t[l].f));else ph(t[t[p].f].r,getc(t[l].f));
                if (getc(t[l].f)) ph(t[t[t[l].f].f].l,getc(l));else ph(t[t[t[l].f].f].r,getc(l));
            }
        }else{
            if (p==root){
                if (getc(t[l].f)) ph(t[t[t[l].f].f].l,getc(l));else ph(t[t[t[l].f].f].r,getc(l));
                ph(root,getc(l));
                break;
            }else{
                if (getc(t[l].f)) ph(t[t[t[l].f].f].l,getc(l));else ph(t[t[t[l].f].f].r,getc(l));
                if (getc(p)) ph(t[t[p].f].l,getc(l));else ph(t[t[p].f].r,getc(l));
            }
        }
    }
}
inline void insert(int &p,int k,int f){
    if (p==0){
        num++;
        t[num].k=k;
        t[num].f=f;
        t[num].s=1;
        p=num;
        splay(p);
    }else{
        t[p].s++;
        if (k>t[p].k) insert(t[p].r,k,p);else insert(t[p].l,k,p);
    }
}
inline void del(int &p,int key){
    if (p==0){
        t[p].s=0;
        return;
    }
    if (t[p].k<key){
        ans+=t[t[p].l].s+1;
        t[t[p].r].f=t[p].f;
        del(p=t[p].r,key);
    }else{
        del(t[p].l,key);
    }
    if (p!=0) t[p].s=t[t[p].l].s+t[t[p].r].s+1;
}
inline int que(int p,int key){
    qq=p;
    if (key==t[t[p].r].s+1) return t[p].k;
    if (key<t[t[p].r].s+1) return que(t[p].r,key);
    if (key>t[t[p].r].s+1) return que(t[p].l,key-t[t[p].r].s-1);
}
int main(){
    n=read();m=read();
    k=0;
    for (register int i=0;i<n;i++){
        char c[2];int b;
        scanf("%s",c);b=read();
        if (c[0]=='I'){
            if (b>=m) insert(root,b-k,0);
        }else
        if (c[0]=='A'){
            k+=b;
        }else
        if (c[0]=='S'){
            k-=b;
            del(root,m-k);
        }else
        if (c[0]=='F'){
            if (b>t[root].s) puts("-1");else
            printf("%d\n",que(root,b)+k),splay(qq);
        }
    }
    printf("%d",ans);
    return 0;
}

 

TREAP:

#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;

struct na{
    int l,r,k,s,ra;
    na(){
        l=r=k=s=0;
    }
};
int root=0,num=0,ans=0;
long n,m,k;
na t[100001];
inline void rir(int &p){
    int q=t[p].l;
    t[p].l=t[q].r;
    t[q].r=p;
    t[q].s=t[p].s;
    t[p].s=t[t[p].l].s+t[t[p].r].s+1;
    p=q;
}
inline void ler(int &p){
    int q=t[p].r;
    t[p].r=t[q].l;
    t[q].l=p;
    t[q].s=t[p].s;
    t[p].s=t[t[p].l].s+t[t[p].r].s+1;
    p=q;
}
inline void insert(int &p,int key){
    if (p==0){
        num++;
        p=num;
        t[num].k=key;
        t[num].s=1;
        t[num].ra=rand();
    }else{
        t[p].s++;
        if (t[p].k>key){
            insert(t[p].l,key);
            if (t[t[p].l].ra<t[p].ra) rir(p);
        }
        else{
            insert(t[p].r,key);
            if (t[t[p].r].ra<t[p].ra) ler(p);
        }
    }
}
inline void del(int &p,int key){
    if (p==0){
        return;
    }
    if (t[p].k<key){
        ans+=t[t[p].l].s+1;
        del(p=t[p].r,key);
    }else{
        del(t[p].l,key);
        t[p].s=t[t[p].l].s+t[t[p].r].s+1;
    }
}
inline int que(int p,int key){
    if (key==t[t[p].r].s+1) return t[p].k;
    if (key<t[t[p].r].s+1) return que(t[p].r,key);
    if (key>t[t[p].r].s+1) return que(t[p].l,key-t[t[p].r].s-1);
}
int main(){
    scanf("%d%d",&n,&m);
    srand(n+m);
    k=0;
    for (int i=0;i<n;i++){
        char c[2];int b;
        scanf("%s%d",c,&b);
        if (c[0]=='I'){
            if (b>=m) insert(root,b-k);
        }
        if (c[0]=='A'){
            k+=b;
        }
        if (c[0]=='S'){
            k-=b;
            del(root,m-k);
        }
        if (c[0]=='F'){
            if (b>t[root].s) puts("-1");else
            printf("%d\n",que(root,b)+k);
        }
    }
    printf("%d",ans);
    return 0;
}

 

 

posted @ 2016-02-02 09:10  swm_sxt  阅读(210)  评论(0编辑  收藏  举报