NOI 2004 郁闷的出纳员

时间:2023-11-18 09:53:32

Description

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

Input

NOI 2004 郁闷的出纳员

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=;
}
};
int root=,num=,ans=;
long n,m,k;
na t[];
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+;
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+;
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,);
ph(t[p].r,);
ph(p,);
ph(p,);
}
void insert(int &p,int key){
if (p==){
num++;
p=num;
t[num].k=key;
t[num].s=;
}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==){
return;
}
if (t[p].k<key){
ans+=t[t[p].l].s+;
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+;
}
ph(p,);
ph(p,);
}
int que(int p,int key){
if (key==t[t[p].r].s+) return t[p].k;
if (key<t[t[p].r].s+) return que(t[p].r,key);
if (key>t[t[p].r].s+) return que(t[p].l,key-t[t[p].r].s-);
}
int main(){
scanf("%d%d",&n,&m);
k=;
for (int i=;i<n;i++){
char c[];int b;
scanf("%s%d",c,&b);
if (c[]=='I'){
if (b>=m) insert(root,b-k);
}
if (c[]=='A'){
k+=b;
}
if (c[]=='S'){
k-=b;
del(root,m-k);
}
if (c[]=='F'){
if (b>t[root].s) puts("-1");else
printf("%d\n",que(root,b)+k);
}
}
printf("%d",ans);
return ;
}

SPLAY:

#include<cstdio>
#include<algorithm>
using namespace std; struct na{
int l,r,k,s,f;
na(){
l=r=k=s=;
}
};
int root=,num=,ans=,p,qq;
long n,m,k;
na t[];
char ch;
inline int read(){
p=;ch=getchar();
while (ch<''||ch>'') ch=getchar();
while (ch>=''&&ch<='') p=p*+ch-, 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+;
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+;
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==){
num++;
t[num].k=k;
t[num].f=f;
t[num].s=;
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==){
t[p].s=;
return;
}
if (t[p].k<key){
ans+=t[t[p].l].s+;
t[t[p].r].f=t[p].f;
del(p=t[p].r,key);
}else{
del(t[p].l,key);
}
if (p!=) t[p].s=t[t[p].l].s+t[t[p].r].s+;
}
inline int que(int p,int key){
qq=p;
if (key==t[t[p].r].s+) return t[p].k;
if (key<t[t[p].r].s+) return que(t[p].r,key);
if (key>t[t[p].r].s+) return que(t[p].l,key-t[t[p].r].s-);
}
int main(){
n=read();m=read();
k=;
for (register int i=;i<n;i++){
char c[];int b;
scanf("%s",c);b=read();
if (c[]=='I'){
if (b>=m) insert(root,b-k,);
}else
if (c[]=='A'){
k+=b;
}else
if (c[]=='S'){
k-=b;
del(root,m-k);
}else
if (c[]=='F'){
if (b>t[root].s) puts("-1");else
printf("%d\n",que(root,b)+k),splay(qq);
}
}
printf("%d",ans);
return ;
}

TREAP:

#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std; struct na{
int l,r,k,s,ra;
na(){
l=r=k=s=;
}
};
int root=,num=,ans=;
long n,m,k;
na t[];
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+;
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+;
p=q;
}
inline void insert(int &p,int key){
if (p==){
num++;
p=num;
t[num].k=key;
t[num].s=;
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==){
return;
}
if (t[p].k<key){
ans+=t[t[p].l].s+;
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+;
}
}
inline int que(int p,int key){
if (key==t[t[p].r].s+) return t[p].k;
if (key<t[t[p].r].s+) return que(t[p].r,key);
if (key>t[t[p].r].s+) return que(t[p].l,key-t[t[p].r].s-);
}
int main(){
scanf("%d%d",&n,&m);
srand(n+m);
k=;
for (int i=;i<n;i++){
char c[];int b;
scanf("%s%d",c,&b);
if (c[]=='I'){
if (b>=m) insert(root,b-k);
}
if (c[]=='A'){
k+=b;
}
if (c[]=='S'){
k-=b;
del(root,m-k);
}
if (c[]=='F'){
if (b>t[root].s) puts("-1");else
printf("%d\n",que(root,b)+k);
}
}
printf("%d",ans);
return ;
}