Bzoj topic 1503: [NOI2004] Depressed Teller (sbt+ delay operation)

Source: Internet
Author: User

1503: [NOI2004] Depressed teller time limit: 5 Sec Memory Limit: MB
Submit: 8058 Solved: 2828
[Submit] [Status] [Discuss] 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?

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 Input9 10
I 60
I 70
S 50
F 2
I 30
S 15
A 5
F 1
F 2

Sample Output10
20
-1
2

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

Source

Splay

is still very simple, is a bit more skill, is to delay the operation, because each time to add or subtract, there is no need to add and subtract all update a tree, save a shape variable last added on the line, is reduced when the wages may be lower than the minimum standard will leave the company, so to update, If the key value of this point is less than Min, then his left subtree is less than min, so root is directly equal to the right subtree, from the right subtree to find, otherwise large, right sub-tree are large, find his left subtree, finally updated size.

I am also drunk, the final output leaves the number of companies, the title is not to say read into the time under less than Min directly leave the company,, that is not also called to leave the company, that part of the people actually do not count.

AC Code

/************************************************************** problem:1503 user:kxh1995 language:c++ Resu lt:accepted time:640 Ms memory:16444 kb****************************************************************/#include &L  T;stdio.h> #include <string.h>struct s {int key,left,right,size;  }TREE[1001000];  int top,root;        void Left_rot (int &x) {int y=tree[x].right;        Tree[x].right=tree[y].left;        Tree[y].left=x;        Tree[y].size=tree[x].size;        tree[x].size=tree[tree[x].left].size+tree[tree[x].right].size+1;    X=y;        } void Right_rot (int &x) {int y=tree[x].left;        Tree[x].left=tree[y].right;        Tree[y].right=x;        Tree[y].size=tree[x].size;        tree[x].size=tree[tree[x].left].size+tree[tree[x].right].size+1;    X=y; } void maintain (int &x,bool flag)//maintain SBT status {if (Flag==false) {if (tree[tree[tree[x].left ].left].size>tree[tree[x].right].size) Right_rot (x);                    else if (tree[tree[tree[x].left].right].size>tree[tree[x].right].size) {                    Left_rot (Tree[x].left);                   Right_rot (x);        } else return; } else {if (tree[tree[tree[x].right].right].size>tree[tree[x].left].size) Left_rot            (x);                    else if (tree[tree[tree[x].right].left].size>tree[tree[x].left].size) {                    Right_rot (Tree[x].right);                Left_rot (x);        } else return;        } maintain (Tree[x].left,false);        Maintain (tree[x].right,true);        Maintain (x,true);    Maintain (X,FALSE);          } void Insert (int &x,int key) {if (x==0) {x=++top;          tree[x].left=0;          tree[x].right=0;          tree[x].size=1;     Tree[x].key=key; } else {tree[x].size++;          if (key<tree[x].key) insert (Tree[x].left,key);          else insert (Tree[x].right,key);      Maintain (X,key>=tree[x].key);      }} int remove (int &x,int key) {tree[x].size--;      if (key>tree[x].key) remove (Tree[x].right,key);          else if (key<tree[x].key) remove (Tree[x].left,key);                  else if (tree[x].left!=0&&tree[x].right==0) {int temp=x;                  X=tree[x].left;              return temp;  } else if (!tree[x].left&&tree[x].right!=0) {int                      Temp=x;                      X=tree[x].right;                  return temp;                          } else if (!tree[x].left&&!tree[x].right) {                      int temp=x;    x=0;                      return temp;                          } else {int temp=tree[x].right;                          while (Tree[temp].left) Temp=tree[temp].left;                          Tree[x].key=tree[temp].key;                      Remove (Tree[x].right,tree[temp].key);      }} int Getmin (int x) {while (tree[x].left) X=tree[x].left;  return tree[x].key;      } int Getmax (int x) {while (tree[x].right) x=tree[x].right;  return tree[x].key;    }void del (int &x,int num,int minval) {if (x==0) return;        if (tree[x].key+num<minval) {x=tree[x].right;    Del (x,num,minval);        } else {del (tree[x].left,num,minval);    tree[x].size=tree[tree[x].left].size+tree[tree[x].right].size+1;      }}int get_min_k (int &x,int k)//Select K small number {int r=tree[tree[x].left].size+1;       if (r==k)   return tree[x].key;          else if (r<k) return Get_min_k (TREE[X].RIGHT,K-R);  else return Get_min_k (tree[x].left,k);    }int get_max_k (int &x,int k) {int r=tree[tree[x].right].size+1;    if (r==k) return tree[x].key;        else if (r<k) return Get_max_k (TREE[X].LEFT,K-R); else return Get_max_k (tree[x].right,k);}    int main () {int n,p;        while (scanf ("%d%d", &n,&p)!=eof) {top=root=0;        int num=0,sum=0;        Char s[2];        int A;        int i;            for (i=0;i<n;i++) {scanf ("%s%d", s,&a);                    if (s[0]== ' I ') {if (a<p) {///sum++;                Continue            } insert (Root,a-num);                } else if (s[0]== ' A ') num+=a;                  else if (s[0]== ' s ') {      Num-=a;                    Del (ROOT,NUM,P);                        } else {//printf ("%d%d\n", tree[root].size,root);                        if (a>tree[root].size) printf (" -1\n");                    else printf ("%d\n", Get_max_k (root,a) +num);    }} printf ("%d\n", top-tree[root].size); }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Bzoj topic 1503: [NOI2004] Depressed Teller (sbt+ delay operation)

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.