Back Gear | The depressed teller of the splay tree application

Source: Internet
Author: User

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. It's not very difficult, is it? Format input 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 Action I command i_k 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 each employee's salary with KS order S_k to deduct each employee's salary from the KF order F_k query the K-plus salary _ (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. The problem is to learn to stretch the tree. Of course, I do not have the ability to learn directly, thanks to the blog Garden of the Yi-Red childe template. The nature of the problem is a two-fork search tree, the extension tree is used to optimize (stretching tree is not a separate structure, I do not know why many students do not understand).
#include"iostream"#include"Cstdio"using namespacestd;intkey[400000],sum[400000]={0},cnt[400000]={0},pre[400000],ch[400000][2];//key is the value of this node//sum indicates how many subtrees there are at this point, and CNT indicates that this point appears several times, and the pre represents the parent node of the point, and CH represents the childintN,mi,de=0, root=0, tot=0;intans=0;voidNewNodeint&num,intFaintk) {num=++tot; key[num]=K; Sum[num]=cnt[num]=1; pre[num]=FA; ch[num][0]=ch[num][1]=0;//the num value of the corresponding subtree is stored in CH return;} voidRotateintXintKind//kind indicates what X is the subtree of y, 0 is the left subtree, and 1 is the right subtree .{ intY=PRE[X];//y is the Father node of xCh[y][kind]=ch[x][!kind];//x is left, then X's position is replaced by a subtree in the opposite position of xpre[ch[x][!kind]]=y; if(Pre[y]) ch[pre[y]][key[pre[y] [pre[x]=Pre[y]; Pre[y]=x; ch[x][!kind]=y; sum[y]=sum[ch[y][0]]+sum[ch[y][1]]+cnt[y]; Sum[x]=sum[ch[x][0]]+sum[ch[x][1]]+Cnt[x];return;}intSearchintk) { intx=Root; while(Ch[x][key[x] {if(key[x]==k)returnx; X=Ch[x][key[x]}returnx;}voidSplay (intX//shake the node x to the root{  while(pre[x]!=0)//when it's not at the root, {  inty=Pre[x]; if(pre[y]==0) Rotate (x,ch[y][1]==x); //if y is the root node, rotate it once. Determines if the right subtree of y is x and returns the subtree type that happens to be x  Else   {   intZ=pre[y];//Z is the grandfather of X   intKind= (y==ch[z][1]);//kind represents the relationship of Y and Z   intKind2= (x==ch[y][1]);//Kind2 represents the relationship between X and Y   if(KIND==KIND2)  {rotate (y,kind); rotate (x,kind2); }//one-font rotation     Else{rotate (X,kind2); rotate (x,kind); }//Zigzag Rotation}} Root=x;return;} voidInsert (intk) { if(root==0) {NewNode (root,0, k);return; } Splay (search (k));//Find the node that is similar to K and "shake" it to the root, good to insert ~ if(key[root]==k) {sum[root]++; cnt[root]++;return; } intsp=0;//species, the record is the root node of the Zuozi or right sub-tree if(Key[root]intREC=CH[ROOT][SP];//Record records the original sub-treeNewNode (CH[ROOT][SP],ROOT,K);//Add new Points intX=CH[ROOT][SP];//X is the new pointCh[x][sp]=rec;//the subtree of the new point is Y, and the subtree should be of the same typepre[rec]=x; sum[x]=sum[rec]+cnt[x]; Sum[root]+=cnt[x]; splay (x);}voidDelete () {Insert (Mi-de-1);//a person who inserts a base pay minus the number of people who have been added before is used to judge. //in this topic, by inserting this point to the less than his salary (that is, wages less than the basic salary) of the people deletedans+=sum[root]-sum[ch[root][1]]-1;//ans is the number of people leaving the companyroot=ch[root][1];//right subtree is still in the company.pre[root]=0; }intFind (intask) { if(Ask>sum[root])return-1; ask=sum[root]-ask+1; intx=Root; while(ask>0) {  if(ask>sum[ch[x][0]]+cnt[x]) {ask=ask-sum[ch[x][0]]-CNT[X]; x=ch[x][1];Continue;} if(ask<=sum[ch[x][0]]+CNT[X] && ask>sum[ch[x][0]]) {splay (x);returnkey[x]+de;} X=ch[x][0]; } }intMain () {CIN>> n >> mi; De=0; CharAintb; scanf ("%c",&a);  for(intI=1; i<=n; i++)    {        CharAintb; scanf ("%c%d",&a,&b); if(a=='I'&& B>=mi) Insert (b-de); if(a=='A') de+=b; if(a=='S') {de-=b;delete ();} if(a=='F') {Cout<<find (b) <<Endl;} scanf ("%c",&a); } cout<< ans <<Endl; return 0;}

Back Gear | The depressed teller of the splay tree application

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.