The Depressed Teller
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. It's not very difficult, is it?
Input
Output
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.
Sample Input
9 10I 60I 70S 50F 2I 30S 15A 5F 1F 2
Sample Output
1020-12
Hint
The number of bars of the I command does not exceed the number of bars of the 100000 a command and the S command, no more than 100000 of the bar of the command, no more than 1000 of the adjustment amount per salary adjustment does not exceed 100000 of the new employee's salary
The first time to write a balance tree, the online copy of a board, but only understand the part, the inside of the successor code is I in other code pasted in, to be tested.
#include <iostream>#include<cstring>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<time.h>#include<string>#include<map>#include<stack>#include<vector>#include<Set>#include<queue>#defineMAXN 102Const intmaxn=2e5+ -;intLim;structSplaytree {intSZ[MAXN]; intch[maxn][2]; intPRE[MAXN]; intRt,top; InlinevoidUpintx) {sz[x]= Cnt[x] + sz[ch[x][0]] + sz[ch[x][1] ]; } InlinevoidRotate (intXintf) {inty=Pre[x]; ch[y][!F] =Ch[x][f]; pre[Ch[x][f]]=y; PRE[X]=Pre[y]; if(Pre[x]) ch[pre[y] [ch[pre[y]][1] = = Y] =x; CH[X][F]=y; Pre[y]=x; Up (y); } InlinevoidSplay (intXintGoal) {//rotate X to the bottom of the goal while(Pre[x]! =goal) { if(Pre[pre[x]] = = goal) Rotate (x, ch[pre[x]][0] ==x); Else { inty=pre[x],z=Pre[y]; intF = (ch[z][0]==y); if(Ch[y][f] = = x) Rotate (x,!f), Rotate (x,f); ElseRotate (y,f), Rotate (x,f); }} up (x); if(goal==0) rt=x; } InlinevoidRTO (intKintGoal) {//rotate the K-digit to the lower part of the goal intx=RT; while(sz[ch[x][0]! = k1) { if(K < sz[ch[x][0] ]+1) x=ch[x][0]; Else{k-= (sz[ch[x][0] ]+1); X= ch[x][1]; }} splay (X,goal); } InlinevoidVist (intx) {if(x) {printf ("knot point%2d: Left son%2d right son%2d%2d sz=%d\n", x,ch[x][0],ch[x][1],val[x],sz[x]); Vist (ch[x][0]); Vist (ch[x][1]); }} inlinevoidNewnode (int&x,intc) {x=++top; ch[x][0] = ch[x][1] = pre[x] =0; SZ[X]=1; CNT[X]=1; VAL[X]=C; } Inlinevoidinit () {sum=ch[0][0]=ch[0][1]=pre[0]=sz[0]=0; RT=top=0; cnt[0]=0; } InlinevoidInsert (int&x,intKeyintf) {if(!x) {Newnode (X,key); PRE[X]=F; Splay (x,0); return ; } if(key==Val[x]) {Cnt[x]++; SZ[X]++; Splay (x,0); return ; } Else if(key<Val[x]) {Insert (ch[x][0],key,x); } Else{Insert (ch[x][1],key,x); } up (x); } //find a precursor .InlineintGet_pre (intr) {if(ch[r][0] ==0)return-1;//does not existr = ch[r][0]; while(ch[r][1]) R = ch[r][1]; returnR; } //Find a successorInlineintGet_next (intr) {if(ch[r][1] ==0)return-1; R= ch[r][1]; while(ch[r][0]) R = ch[r][0]; returnR; } voidDelint&x,intf) {if(!x)return ; if(val[x]>=Lim) {del (ch[x][0],x); } Else{sum+=sz[ch[x][0]]+Cnt[x]; X=ch[x][1]; PRE[X]=F; if(f==0) rt=x; Del (X,F); } if(x) up (x); } InlinevoidUpdate () {del (RT,0); } InlineintFind_kth (intXintk) {if(k<sz[ch[x][0]]+1) { returnFind_kth (ch[x][0],k); } Else if(k > sz[ch[x][0] ] +Cnt[x])returnFind_kth (ch[x][1],k-sz[ch[x][0]]-cnt[x]); Else{splay (x,0); returnVal[x]; } } intCNT[MAXN]; intVAL[MAXN]; intsum;} SPT;intMain () {intN; intm; Charop[5]; scanf ("%d%d",&n,&m); intw=0; Spt.init (); while(n--) { intK; scanf ("%s%d",op,&k); if(op[0]=='I') { if(k<m) {Continue; } SPT. Insert (Spt.rt,k-W,0); } Else if(op[0]=='A') {W+=K; } Else if(op[0]=='S') {W-=K; Lim=m-W; Spt.update (); } Else { intsz=Spt.sz[spt.rt]; if(K>SZ) printf ("-1\n"); Else{printf ("%d\n", Spt.find_kth (spt.rt,sz-k+1)+W); }}} printf ("%d\n", spt.sum); return 0;}
HNOI2004 depressed Teller (splay)