Description
Oier Company is a large-scale specialized software company, has tens of thousands of employees. As a cashier, one of my tasks is to count the salaries of each employee. It would have been a good job, but it was frustrating that our bosses were fickle and often adjusted their salaries. If he is in a good mood, he may add the same amount to each employee's salary. Conversely, if the mood is not good, it may be their wages deducted a same amount. I really don't know what else he's doing except for the pay raise.
The frequent adjustment of wages is very annoying to employees, especially when the collective deduction of wages, once an employee found that his salary is lower than the contract under the lower wages, he will immediately angrily leave the company, and never come back. The lower salary of each employee is uniformly stipulated. Every time a person leaves the company, I will delete his payroll file from the computer, again, whenever the company hires a new employee, I have to create a new payroll file for him.
The boss often come to my side to ask about the salary, he does not ask a specific employee's salary, but asked how many employees now pay K how much wages. At this point, I had to do a lengthy sort of tens of thousands of employees and tell him the answer.
Well, now you've learned a lot about my job. As you guessed, I would like to ask you to compile a payroll statistics program. It's not very difficult, is it?
format
Input format
The first row has two nonnegative integers n and Min. n indicates how many commands are below, min indicates the lower salary.
The next n lines, each line represents a command. The command can be one of the following four types:
Name Format function
I command I_k to create a new payroll file with the initial salary of K. If an employee's initial salary is below the lower wage, he will leave the company immediately.
A ordered A_k to add K to each employee's salary.
S orders S_k to deduct every employee's salary from K
F Order F_k to inquire about the wages of K-much
_ (underline) denotes a space, the i command, a command, the K in the S command is a nonnegative integer, and K in the F command is a positive integer.
At the beginning, you can assume that there is not a single employee in the company.
Output format
The number of rows in the output file is the number of bars in the F command plus one.
For each f command, your program outputs a row, contains only one integer, the number of salaries for employees who are currently paid a K-number, and if K is greater than the current number of employees, output-1.
The last line of the output file contains an integer that is the total number of employees who have left the company.
examples
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
Tips
I command the number of bars not exceeding 100000
The total number of the a command and the S command is not more than 100
The number of bars in the F command is no more than 100000
No more than 1000 adjustment for each wage adjustment
The salary of the new employee is not more than 100000
Solving
The subject involves the insertion of a single node, delete, and all the changes in the operation of the node, it is not difficult to think of the balance tree, take this problem to practice a long time not to write the Splay bar.
For a and S commands, we do not use tags, but directly with a variable w (can be positive and negative) accumulation;
Also, when the effect of the S command is cumulative, at this time wages plus w still less than min staff will leave, should find the first salary is not less than min-w employees, put him splay to the root, then the left subtree is all the employees left, at this time maintenance size field can maintain the number of all employees. The boundary conditions should be highly valued here .
For I operation, the salary becomes k-w at the time of insertion to maintain the relative size of all employees ' wages;
For f operation, use the Size field to query the K-wide directly;
Code
#include <cstdio> #include <algorithm> #include <cstring> #define MAXN 100005 #define NIL 0 #define OO 1
000000000 using namespace std;
int n, MINP, Root, tot, W;
int D[MAXN], son[maxn][2], FA[MAXN], S[MAXN]; inline void update (int rot) {S[rot] = S[son[rot][0]] + s[son[rot][1]] + 1;} void rotate (int x, int w) {int y =
FA[X];
son[y][w ^ 1] = son[x][w];
if (Son[x][w]!= nil) fa[son[x][w] = y;
FA[X] = Fa[y];
if (Fa[y]!= nil) {if (y = = son[fa[y]][0]) son[fa[y]][0] = x;
else son[fa[y]][1] = x; } Son[x][w] = y;
Fa[y] = x; Update (y);
Update (x);
} void Splay (int x, int poi) {if (x = = nil) return;
while (Fa[x]!= poi) {if (fa[fa[x]] = = POI) {if (x = = son[fa[x]][0]) rotate (x, 1);
else rotate (x, 0);
else {if (fa[x] = = Son[fa[fa[x]]][0]) {if (x = = Son[fa[x]][0]) {RotaTe (Fa[x], 1);
Rotate (x, 1);
else {rotate (x, 0); rotate (x, 1);
} else {if (x = = Son[fa[x]][0]) { Rotate (x, 1);
Rotate (x, 0);
else {rotate (fa[x], 0); rotate (x, 0);
{}}} if (poi = = nil) root = x;
void Insert (int rot, int x) {++s[rot];
if (D[rot] <= x) {if (son[rot][1] = = nil) {D[++tot] = x;
S[tot] = 1;
Fa[tot] = rot;
SON[ROT][1] = tot;
Son[tot][0] = son[tot][1] = nil;
Splay (rot, nil);
else {insert (son[rot][1], x);
} else {if (son[rot][0] = nil) {D[++tot] = x;
S[tot] = 1; Fa[tot] = roT
Son[rot][0] = tot;
Son[tot][0] = son[tot][1] = nil;
Splay (rot, nil);
else {insert (son[rot][0], x);
an int succ (int rot, int k) {if (rot = = nil) return nil;
if (D[rot] >= k) {int t = SUCC (son[rot][0), k);
if (t = = nil) return rot;
else return t;
else {return succ (son[rot][1], k);
an int select (int rot, int k) {if (rot = = Nil | | K < 0) return-1;
if (S[son[rot][1]] + 1 = k) return D[rot] + W;
if (S[son[rot][1]] >= k) return Select (Son[rot][1], k);
Return Select (Son[rot][0], k-s[son[rot][1]]-1);
int main () {char opt;
int k, sum = 0;
scanf ("%d%d", &n, &MINP);
D[root = ++tot] = (oo << 1);
S[tot] = 1;
Fa[tot] = son[tot][0] = son[tot][1] = nil;
while (n--) {scanf ("\n%c%d", &opt, &k); Switch (opt) {case ' I ': if (k >= mINP) {Insert (root, k-w);
++sum;
} break;
Case ' A ': W + k;
Break
Case ' S ': w-= k;
Splay (SUCC (Root, minp-w), nil);
S[root]-= s[son[root][0]];
Son[root][0] = nil;
Break
Case ' F ': printf ("%d\n", select (Root, K + 1));
Break
Default:break;
} printf ("%d\n", Sum-s[root] + 1);
return 0; }