题目
OIER公司是一家大型专业化软件公司,有着数以万计的员工。作为一名出纳员,我的任务之一便是统计每位员工的工资。这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常调整员工的工资。如果他心情好,就可能把每位员工的工资加上一个相同的量。反之,如果心情不好,就可能把他们的工资扣除一个相同的量。我真不知道除了调工资他还做什么其它事情。工资的频繁调整很让员工反感,尤其是集体扣除工资的时候,一旦某位员工发现自己的工资已经低于了合同规定的工资下界,他就会立刻气愤地离开公司,并且再也不会回来了。每位员工的工资下界都是统一规定的。每当一个人离开公司,我就要从电脑中把他的工资档案删去,同样,每当公司招聘了一位新员工,我就得为他新建一个工资档案。老板经常到我这边来询问工资情况,他并不问具体某位员工的工资情况,而是问现在工资第k多的员工拿多少工资。每当这时,我就不得不对数万个员工进行一次漫长的排序,然后告诉他答案。好了,现在你已经对我的工作了解不少了。正如你猜的那样,我想请你编一个工资统计程序。怎么样,不是很困难吧?
分析
基本靠抄+脑补的第一道splay
code
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
const int N=100005;
const int inf=0X7fffffff;
struct tree{
int l,r; int fa,k; int s;
}t[N];
int n,m;
int delta,ans;
int root,tot;
void rttl(int x)
{
int y=t[x].r;
if (x==root) root=y;
t[x].r=t[y].l; t[t[y].l].fa=x; t[y].l=x; t[y].fa=t[x].fa;
if (x==t[t[x].fa].l) t[t[x].fa].l=y;
else t[t[x].fa].r=y;
t[x].fa=y; t[x].s=t[t[x].l].s+t[t[x].r].s+1; t[y].s=t[t[y].l].s+t[t[y].r].s+1;
}
void rttr(int x)
{
int y=t[x].l;
if (x==root) root=y;
t[x].l=t[y].r; t[t[y].r].fa=x; t[y].r=x; t[y].fa=t[x].fa;
if (x==t[t[x].fa].l) t[t[x].fa].l=y;
else t[t[x].fa].r=y;
t[x].fa=y; t[x].s=t[t[x].l].s+t[t[x].r].s+1; t[y].s=t[t[y].l].s+t[t[y].r].s+1;
}
void splay(int x)
{
while (x!=root)
{
int f=t[x].fa;
if (f==root)
if (x==t[f].l) rttr(f);
else rttl(f);
else
{
int p=t[f].fa;
if (x==t[f].l)
{
if (f==t[p].l)
{rttr(p); rttr(f);}
else
{rttr(f); rttl(p);}
}
else
{
if (f==t[p].l)
{rttl(f); rttr(p);}
else
{rttl(p); rttl(f);}
}
}
}
}
void insert(int k)
{
if (!root)
{
root=++tot; t[tot].s=1; t[tot].k=k;
return;
}
int x=root,y;
while (x)
{
y=x; t[x].s++;
if (k<t[x].k) x=t[x].l;
else x=t[x].r;
}
t[++tot].s=1; t[tot].k=k; t[tot].fa=y;
if (k<t[y].k) t[y].l=tot;
else t[y].r=tot;
splay(tot);
}
void del()
{
int x=root,y=0;
while (x)
{
if (t[x].k+delta<m)
x=t[x].r;
else
{
y=x; x=t[x].l;
}
}
if (!y)
{
ans+=t[root].s; root=0;
}
else
{
splay(y);
ans+=t[t[y].l].s; t[y].l=0; t[y].s=t[t[y].r].s+1;
}
}
int find(int xx)
{
int x=root;
while (t[t[x].l].s+1!=xx)
{
if (t[t[x].l].s+1<xx)
{
xx-=t[t[x].l].s+1; x=t[x].r;
}
else
{
x=t[x].l;
}
}
splay(x);
return t[x].k;
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
{
char ch[2];
int x;
scanf("%s%d",ch,&x);
if (ch[0]=='I')
{
if (x>=m) insert(x-delta);
}
else
if (ch[0]=='A')
{
delta+=x;
}
else
if (ch[0]=='S')
{
delta-=x; del();
}
else
{
if (x==0||x>t[root].s)
printf("%d\n",-1);
else
printf("%d\n",find(t[root].s-x+1)+delta);
}
}
printf("%d\n",ans);
}