1503: [NOI2004]郁闷的出纳员
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 8015 Solved: 2819
[ Submit][ Status][ Discuss]
Description
OIER公司是一家大型专业化软件公司,有着数以万计的员工。作为一名出纳员,我的任务之一便是统计每位员工的工资。这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常调整员工的工资。如果他心情好,就可能把每位员工的工资加上一个相同的量。反之,如果心情不好,就可能把他们的工资扣除一个相同的量。我真不知道除了调工资他还做什么其它事情。工资的频繁调整很让员工反感,尤其是集体扣除工资的时候,一旦某位员工发现自己的工资已经低于了合同规定的工资下界,他就会立刻气愤地离开公司,并且再也不会回来了。每位员工的工资下界都是统一规定的。每当一个人离开公司,我就要从电脑中把他的工资档案删去,同样,每当公司招聘了一位新员工,我就得为他新建一个工资档案。老板经常到我这边来询问工资情况,他并不问具体某位员工的工资情况,而是问现在工资第k多的员工拿多少工资。每当这时,我就不得不对数万个员工进行一次漫长的排序,然后告诉他答案。好了,现在你已经对我的工作了解不少了。正如你猜的那样,我想请你编一个工资统计程序。怎么样,不是很困难吧?
Input
Output
输出文件的行数为F命令的条数加一。对于每条F命令,你的程序要输出一行,仅包含一个整数,为当前工资第k多的员工所拿的工资数,如果k大于目前员工的数目,则输出-1。输出文件的最后一行包含一个整数,为离开公司的员工的总数。
Sample Input
I 60
I 70
S 50
F 2
I 30
S 15
A 5
F 1
F 2
Sample Output
20
-1
2
HINT
I命令的条数不超过100000 A命令和S命令的总条数不超过100 F命令的条数不超过100000 每次工资调整的调整量不超过1000 新员工的工资不超过100000
注意好相同元素的处理就好了,代码写得比较挫
#include <iostream>
#include <cstdio>
using namespace std;
#define LS(x) node[(x)].ch[0]
#define RS(x) node[(x)].ch[1]
const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
int delta, minn, quit;
struct Splay {
struct Node {
int fa, ch[2];
int val, size;
void init(int _val) {
val = _val;
size = 1;
}
} node[N];
int root, tot;
void up(int x){
node[x].size = node[LS(x)].size + node[RS(x)].size + 1;
}
void rotate(int x, bool kind) {
int fx = node[x].fa;
int ffx = node[fx].fa;
node[fx].ch[!kind] = node[x].ch[kind];
node[node[x].ch[kind]].fa = fx;
node[x].ch[kind] = fx;
node[fx].fa = x;
node[ffx].ch[RS(ffx) == fx] = x;
node[x].fa = ffx;
up(fx);
}
void splay(int x, int goal) {
while(node[x].fa != goal) {
int fx = node[x].fa;
int ffx = node[fx].fa;
bool rotate_x = (LS(fx) == x);
bool rotate_fx = (LS(ffx) == fx);
if(ffx == goal) rotate(x, rotate_x);
else {
if(rotate_x == rotate_fx) rotate(fx, rotate_fx);
else rotate(x, rotate_x);
rotate(x, rotate_fx);
}
}
up(x);
if(goal == 0) root = x;
}
void find_pos(int x, int u, int &p) {
if(u == 0) return ;
if(x <= node[u].val) {
if(node[u].val < node[p].val)
p = u;
else if(node[u].val == node[p].val)
p = max(p, u);
find_pos(x, LS(u), p);
} else {
find_pos(x, RS(u), p);
}
}
void insert(int x) {
if(x < minn) return ;
x -= delta;
int u = 2;
find_pos(x, root, u);
splay(u, root);
int tmp = LS(u);
node[tot].init(x);
LS(u) = tot;
node[tot].fa = u;
LS(tot) = tmp;
if(tmp) node[tmp].fa = tot;
up(tot);
up(u);
up(root);
++tot;
}
void del() {
int gz = minn - delta;
int u = 2;
find_pos(gz, root, u);
splay(u, root);
quit += node[LS(u)].size;
LS(u) = 0;
up(u);
up(root);
}
int find_k(int x, int u) {
if(x <= node[LS(u)].size)
find_k(x, LS(u));
else{
x -= node[LS(u)].size + 1;
if(x == 0) return u;
else find_k(x, RS(u));
}
}
bool ok(int x) {
if(x > node[root].size - 2) return false;
return true;
}
void init() {
node[0].init(-INF); node[0].size = 0;
node[1].init(-INF);
node[2].init(INF);
root = 1;
node[root].fa = 0;
RS(root) = 2;
node[2].fa = root;
up(root);
node[0].fa = 0;
LS(0) = root;
tot = 3;
}
void debug() {
cout<<"root: "<<root<<endl;
for(int i = 0; i < tot; ++i) {
cout<<i<<": "<<node[i].val<<' '<<node[i].size<<' '<<node[i].fa<<' '<<node[i].ch[0]<<' '<<node[i].ch[1]<<endl;
}
}
} spl;
void init() {
spl.init();
delta = 0;
quit = 0;
}
int main() {
int n;
scanf("%d%d", &n, &minn);
init();
for(int i = 0; i < n; ++i) {
char op[5];
int x;
scanf("%s%d", op, &x);
if(op[0] == 'I') {
spl.insert(x);
} else if(op[0] == 'A') {
delta += x;
spl.del();
} else if(op[0] == 'S') {
delta -= x;
spl.del();
} else {
if(spl.ok(x)) {
x = spl.node[spl.root].size - x;
int p = spl.find_k(x, spl.root);
printf("%d\n", spl.node[p].val + delta);
} else {
puts("-1");
}
}
}
printf("%d\n", quit);
return 0;
}