"Tree-like array" NOI2004 depressed teller ...

Source: Internet
Author: User
Tags min time limit

"Tree-shaped array" depressed teller

Time limit:1000ms Memory limit:65536k
Total Submit:10 Accepted:4

Description

"Problem description"

Oier Company is a large specialized software company, with tens of thousands of employees. As a teller, one of my tasks is to count the wages of each employee. It was supposed to be a good job, but the depressing part is that our bosses are fickle and often adjust their employees ' 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 is possible to deduct their wages by a similar amount. I really don't know what else he's going to do in addition to the salary adjustment.
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 stipulated wages, he would immediately angrily leave the company, and will never come back. The lower bound of wages for each employee is uniformly defined. Whenever a person leaves the company, I have to delete his payroll file from the computer, as well, whenever the company hires a new employee, I have to create a new payroll file for him.
The boss often came to my side to inquire about the salary, he did not ask the specific employee's salary, but asked how much the salary of the employees of the K-m pay. At this point, I had to make a lengthy sort of tens of thousands of employees and tell him the answer.
Well, now you've learned a lot about my work. As you guessed, I would like to ask you to compile a payroll statistics program. Well, it's not very difficult.


Input

The first line has two non-negative integers n and min. n indicates how many commands are below and min represents the lower bound of wages.

Next n rows, each line represents a command. The command can be one of the following four:

Name format effect
I command I_k to create a new payroll file with an initial wage of K. If an employee's initial wage falls below the lower salary, he will leave the company immediately.
A command a_k The wages of each employee plus K
S command S_k to deduct the wages of each employee K
F Order F_k to inquire about the K-pay
_ (underscore) denotes a space, the I command, the A command, the K in the S command is a nonnegative integer, and the k in the F command is a positive integer.

At the beginning, it can be assumed that there is not a single employee in the company.

In addition, just into the company found that the wages of the must not left is not counted in the number of people leaving the company.

Data range
The number of bars in the I command does not exceed 100000
A command and S command have no more than 100 total number of bars
The number of bars in the F command does not exceed 100000
The adjustment amount of each salary adjustment does not exceed 1000
The salary of the new employee is not more than 100000

Output

The number of rows to output is the number of bars of the F command plus one.
For each f command, your program will output a line that contains only an integer, the number of wages for employees with a current salary of more than K, and if K is greater than the current number of employees, output-1.
The last line of the output contains an integer that is the total number of employees who leave the company.

Sample Input


Sample Output


Source

NOI2004

Use a tree-like array to solve problems.

Because

1. The maximum salary of new employees is not more than 100000

2.A command and S command the total number of bars does not exceed 100

3. Adjustment of each salary adjustment does not exceed 1000
So set a one-dimensional array of length 400000 A, subscript indicates the employee's salary, and a tree-like array.

For adjustment of salary, you can set a variable key, which means 0

If it is a command, Dec (key,k), it is equivalent to a number in the axis, the origin of the pan left shifted k, the other values are added to the corresponding K

In the case of the S Command, Inc (KEY,K), it is equivalent to translating the origin to the right in a number of axes, and the other values are correspondingly reduced by K

In this way, an employee may leave, enumerating for i:= the original k to changed K, and if found A[i]=1, delete the point. Here to delete the method can only be found a delete, can not first count the number of points in the interval, deleted together, with a tree array can not be achieved.

The minimum value of key is-100000, but if a is opened to [ -100000,200000] with a tree-like array is a problem, so open a 400000 (300000, I do not know why) array, the initial value of key is 200000, Each time an initial wage is read, it is given a margin of +key, the result of which is an array subscript.

The search for the K-more salary is equal to the total number of-k+1, because the tree-like array records how many individuals are paid less than the employee. The binary method is used to find the subscript of the array, and there may be multiple subscript to satisfy the value, then we should find the leftmost subscript. When the total-k+1 <=0, it outputs-1, because there is no K-man at all.

That's it, it's kind of troublesome.

Const
maxn=500010;
Var
C:char;
N,min,m,tot,key,i,now:longint;
A:array[0..maxn]of Longint;

Procedure Add (X,data:longint);
Begin
If X=0 then exit;
While X<=MAXN do
Begin
A[x]:=a[x]+data;
X:=x+x and-x;
End
End

function Fin (x:longint): Longint;
Begin
fin:=0;
If X=0 then exit;
While x>0 do
Begin
Inc (Fin,a[x]);
X:=x-x and-x;
End
End

Procedure change (x:longint);
Var
I,key0,s:longint;
Begin
s:=0;
Key0:=key;
Key:=key+x;
S:=fin (key-1);
Add (key,-s);
For I:=key0 to Key-1 do a[i]:=0;
Inc (TOT,S);
Dec (now,s);
End

Procedure Insert (X:longint);
Begin
X:=x-min+key;
Add (x,1);
End

Procedure find (X:longint);
Var
L,r,y:longint;
Begin
L:=key; r:=key-min+200000;
While L<=r do
Begin
Y:=fin ((l+r) Div 2);
If Y>=x then r:= (l+r) Div 2-1
else l:= (l+r) Div 2+1
End
Writeln (l-key+min);
End

Begin
tot:=0;
now:=0;
READLN (n,min);
key:=min+200000;
For I:=1 to N do
Begin
Read (c);
READLN (m);
If c= ' I ' then
Begin
If M>=min Then
Begin
Insert (m);
Inc (now);
End
End
Else
If c= ' A ' then
Begin
Key:=key-m;
End
Else
If c= ' S ' then
Begin
Change (m);
End
Else
If c= ' F ' then
Begin
If Now-m+1>0 then find (now-m+1)
Else Writeln (-1);
End
End
Writeln (TOT);
End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.