[NOI2004]郁闷的出纳员

Description

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

Input

Output

输出文件的行数为F命令的条数加一。 对于每条F命令,你的程序要输出一行,仅包含一个整数,为当前工资第k多的员工所拿的工资数,如果k大于目前员工的数目,则输出-1。 输出文件的最后一行包含一个整数,为离开公司的员工的总数。
 
 
 
 
 
 
刚开始学splay,就用了splay解法。
技巧:用de记录加了的工资,则初始工资t插入树中工资可表示成x=t-de,取出时最终工资为x+de.
<1>插入工资t   在伸展树中找大于t-de的最小值或小于t-de的最大值,splay该节点为root,将t-de插入root左子树或右子树,splay该节点为root,便于改各子树节点数。
                      若存在t-de节点x,splay该节点为root,cnt[x]++,sum[x]++.
<2>减工资  在树中插入min-de-1,删除root和左子树,处理好节点数。
<3>查询  二叉查询就OK了
 
RQ一开始cin超时,scanf就AC了,但BZOJ死活RE,不知为何.....
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstdlib>
  4 #include<cmath>
  5 #include<string>
  6 #include<cstring>
  7 #include<fstream>
  8 using namespace std;
  9 //ifstream fin("fin.in");
 10 
 11 int key[200005],sum[200005]={0},cnt[200005]={0},pre[200005],ch[200005][2];
 12 int n,mi,de,root=0,tot=0,ans=0;
 13 
 14 void Newnode(int &num,int father,int k){
 15     num=++tot;pre[num]=father;key[num]=k;
 16     sum[num]=cnt[num]=1;ch[num][0]=ch[num][1]=0;
 17     return ;
 18     }
 19 
 20 void Rotate(int x,int kind){
 21     int y=pre[x];
 22     ch[y][!kind]=ch[x][kind];
 23     pre[ch[x][kind]]=y;
 24     
 25     if(pre[y]) ch[pre[y]][key[pre[y]]<key[y]]=x;
 26     pre[x]=pre[y];
 27     
 28     pre[y]=x;
 29     ch[x][kind]=y;
 30     
 31     sum[y]=sum[ch[y][0]]+sum[ch[y][1]]+cnt[y];
 32     sum[x]=sum[ch[x][0]]+sum[ch[x][1]]+cnt[x];
 33 
 34     return ;
 35     }
 36 
 37 void Splay(int x,int goal){
 38     while(pre[x]!=goal)
 39     {
 40         int y=pre[x];
 41         if(pre[y]==goal) Rotate(x,ch[y][0]==x);
 42         else
 43         {
 44             int z=pre[y];
 45             int kind=y==ch[z][0];
 46             if(x==ch[y][!kind])  {Rotate(y,kind);Rotate(x,kind);}
 47             else {Rotate(x,!kind);Rotate(x,kind);}
 48             }
 49         }
 50     root=x;return ;
 51     }
 52 
 53 int Search(int k){
 54     int x=root;
 55 
 56     while(ch[x][key[x]<k])
 57     {
 58         if(key[x]==k) return x;
 59         x=ch[x][key[x]<k];
 60         }
 61     
 62     return x;
 63     }
 64 
 65 void Insert(int k){
 66     if(root==0) {Newnode(root,0,k);return ;}
 67     Splay(Search(k),0);
 68     if(key[root]==k) {cnt[root]++;sum[root]++;return ;}
 69 
 70     int kind=(key[root]<k);int y=ch[root][kind];
 71     Newnode(ch[root][kind],root,k);
 72     int x=ch[root][kind];
 73     ch[x][kind]=y;
 74     pre[y]=x;
 75     sum[x]=sum[y]+cnt[x];
 76     sum[root]+=cnt[x];  
 77     Splay(x,0);  ///////
 78     }
 79 
 80 void Delete(){
 81     Insert(mi-de-1);
 82     ans=ans+sum[root]-sum[ch[root][1]]-1;
 83     root=ch[root][1];
 84     pre[root]=0;
 85     }
 86 
 87 int Find(int t){
 88     if(t>sum[root]) return -1;
 89     
 90     t=sum[root]-t+1;
 91     int x=root;
 92     while(t)
 93     {
 94         if(t>sum[ch[x][0]]+cnt[x]) {t=t-sum[ch[x][0]]-cnt[x];x=ch[x][1];continue;}
 95         if(t<=sum[ch[x][0]]+cnt[x]&&t>sum[ch[x][0]]) {Splay(x,0);return key[x]+de;}
 96         x=ch[x][0];
 97         }
 98     }
 99 
100 int main()
101 {
102     cin>>n>>mi;de=0;
103     
104     char a;int b;
105     scanf("%c",&a);
106     for(int i=1;i<=n;++i)
107     {
108         char a;int b;
109         scanf("%c %d",&a,&b);
110         if(a=='I'&&b>=mi) Insert(b-de);
111         if(a=='A') de+=b;
112         if(a=='S') {de-=b;Delete();}
113         if(a=='F') {cout<<Find(b)<<endl;}
114         scanf("%c",&a);
115         }
116     
117     cout<<ans<<endl;
118     return 0;
119     
120     
121     }
 
原文地址:https://www.cnblogs.com/noip/p/3114274.html