bzoj 1503 郁闷的出纳员

1503: [NOI2004]郁闷的出纳员

Time Limit: 5 Sec   Memory Limit: 64 MB
Submit: 9360   Solved: 3283
[ 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

题解:平衡树splay

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 300000
using namespace std;
int n,m,sum,sz,root,delta;
int ch[N][3],fa[N],size[N],key[N],cnt[N];
const int inf=1e9;
void clear(int x)
{
  ch[x][0]=ch[x][1]=fa[x]=size[x]=cnt[x]=key[x]=0;
}
int get(int x)
{
  return ch[fa[x]][1]==x;
}
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]];
   }
}
void rotate(int x)
{
  int y=fa[x]; int z=fa[y]; int which=get(x);
  ch[y][which]=ch[x][which^1]; fa[ch[y][which]]=y;
  ch[x][which^1]=y; fa[y]=x; fa[x]=z;
  if (z) ch[z][ch[z][1]==y]=x;
  update(y); update(x);
}
void splay(int x,int tar)
{
  for (int f;(f=fa[x])!=tar;rotate(x))
   if (fa[f]!=tar)
    rotate(get(f)==get(x)?f:x);
  if (!tar) root=x;
}
int find(int x)
{
  int now=root; int ans=0;
  while (true)
   {
    if (x<key[now])
     now=ch[now][0];
    else
     {
       ans+=(ch[now][0]?size[ch[now][0]]:0);
       if (x==key[now]) 
        {
         splay(now,0); return now;
        }
       ans+=cnt[now];
       now=ch[now][1];
     }
   }
}
int findx(int x)
{
  int now=root; int temp=0;
  while(true)
   {
     if (ch[now][0]&&x<=size[ch[now][0]])  now=ch[now][0];
     else
     {
      temp=(ch[now][0]?size[ch[now][0]]:0)+cnt[now];
      if (x<=temp) return key[now];
      x-=temp; now=ch[now][1];
     }
   }
}
void insert(int x)
{
  if (root==0)  
   {
   	 sz++; root=sz; clear(sz); size[sz]=cnt[sz]=1; key[sz]=x; return;
   }
  int now=root; int f=0;
  while (true)
   {
   	 if (key[now]==x)
   	  {
   	  	cnt[now]++; update(now); update(f); splay(now,0); break;
   	  }
   	 f=now;
   	 now=ch[now][x>key[now]];
   	 if (!now)
   	  {
   	  	sz++; clear(sz); size[sz]=cnt[sz]=1; key[sz]=x;
   	  	ch[f][x>key[f]]=sz; fa[sz]=f; update(f); splay(sz,0); break;
   	  }
   }
}
int pre(int x)
{
  int now=ch[x][0];
  while(ch[now][1]) now=ch[now][1];
  return now;
}
int next(int x)
{
  int now=ch[x][1];
  while(ch[now][0]) now=ch[now][0];
  return now;
}
void del(int x)
{
  int whatever=find(x);
  if (cnt[root]>1) 
   {
   	cnt[root]--; update(root); return;
   }
  if (!ch[root][1]&&!ch[root][0])
   {
   	 clear(root); root=0; return;
   }
  if(!ch[root][0])
   {
   	 int old=root; root=ch[root][1]; fa[root]=0; clear(old); return;
   }
  if (!ch[root][1])
   {
   	int old=root; root=ch[root][0]; fa[root]=0; clear(old); return;
   }
  int k=pre(root); int old=root; splay(k,0);
  ch[root][1]=ch[old][1]; fa[ch[root][1]]=k; clear(old);
  update(root);
}
int main()
{
 scanf("%d%d",&n,&m);
 insert(inf);  insert(-inf);
 for (int i=1;i<=n;i++)
  {
   char s[10]; int x; scanf("%s%d",s,&x);
   if (s[0]=='I')
    {
    if (x>=m) insert(x-delta);
    }
   else
    if (s[0]=='A')
     delta+=x;
    else
    if (s[0]=='S')
     {
      delta-=x;
      insert(m-delta);
      int l=find(-inf); 
	  int r=find(m-delta);
      splay(l,0); splay(r,root); 
      sum+=size[ch[ch[root][1]][0]];
	  clear(ch[ch[root][1]][0]);
      ch[ch[root][1]][0]=0; 
      update(ch[root][1]); update(root);
      del(m-delta);
     }
    else
     {
      if (x>size[root]-2) printf("-1\n");
      else printf("%d\n",findx(size[root]-x)+delta);
     }
  } 
 printf("%d\n",sum); 
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值