Rokua P1486 Depressed Teller _treap

Source: Internet
Author: User
Tags min
Title 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.

If an employee's initial wage is lower than the minimum wage, then the final answer is not counted into the input output format:

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.

Output format:

The number of rows in the output file 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 file contains an integer that is the total number of employees who leave the company.

input and Output sample input Example # #:

9
I,
s,
2 I, S,
5
F 1
F 2
Sample # # of output:
Ten
-1
2
Description

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

After 2 days of adjustment, finally ...

Attached code:

#include <iostream> #include <algorithm> #include <cstdio> using namespace std;
int n,m,ans=0;
	struct node{node* son[2];
	int V,w,s,flag;
		Node () {son[0]=son[1]=null;
		W=rand ();
		v=0;
	s=flag=1;
}
};
node* RT;
	inline int read () {int Date=0,w=1;char c=0; while (c< ' 0 ' | |
	C> ' 9 ') {if (c== '-') W=-1;c=getchar ();}
	while (c>= ' 0 ' &&c<= ' 9 ') {date=date*10+c-' 0 '; C=getchar ();}
return date*w;
	} void Maintain (node* &u) {u->s=u->flag;
	if (u->son[0]!=null) u->s+=u->son[0]->s;
if (u->son[1]!=null) u->s+=u->son[1]->s;
	} void Turn (node* &u,int f) {node* t=u->son[f^1];
	u->son[f^1]=t->son[f];
	t->son[f]=u;
	Maintain (U);
	Maintain (t);
u=t;
		} void Insert (node* &u,int x) {if (u==null) {u=new node;
		u->v=x;
		Maintain (U);
	Return
		}/* else if (u->v==x) {u->flag++;
		Maintain (U);
	Return
	}*/int y=x>=u->v?1:0;
	Insert (U-&GT;SON[Y],X);
	if (u->son[y]->w>u->w) turn (u,y^1); else Maintain (u);
	} void Remove (node* &u) {if (u==null) return;
	if (u->v>=m) remove (u->son[0]);
		else{if (u->son[0]!=null) ans+=u->son[0]->s;
		ans++;
		u=u->son[1];
	Remove (u);
} if (U!=null) maintain (U);
	} int sum (node* u,int k) {int rsons=0;
	if (u->son[1]!=null) rsons=u->son[1]->s;
	if (k>=rsons+1&&k<=u->flag+rsons) return u->v;
	if (k<=rsons) return sum (U-&GT;SON[1],K);
else return sum (u->son[0],k-u->flag-rsons);
	} void Add (node* &u,int x) {if (u==null) return;
	u->v+=x;
	if (u->son[0]!=null) Add (u->son[0],x);
	if (u->son[1]!=null) Add (u->son[1],x);
Return
	} int main () {char ch[2];
	int x;
	Srand (987);
	N=read (); M=read ();
		for (int cases=1;cases<=n;cases++) {scanf ("%s", ch); X=read ();
		if (ch[0]== ' I ') {if (x>=m) insert (RT,X);
		} if (ch[0]== ' A ') {Add (rt,x);
			} if (ch[0]== ' S ') {Add (rt,-x);
		Remove (RT);
			} if (ch[0]== ' F ') {if (rt==null) printf (" -1\n"); else if (x>rt->s| | x<=0) printf ("-1\ n ");
		else printf ("%d\n", Sum (rt,x));
	}} printf ("%d\n", ans);
return 0; }
Related Keywords:

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.