Balanced binary tree SBT (bzoj1503[noi2004] depressed teller)

Source: Internet
Author: User
Tags printf time limit

1503: [NOI2004] Depressed teller Time Limit:5 Sec Memory limit:64 MB
submit:6779 solved:2377
[Submit] [Status] 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

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

Sample Output 10
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


Previously written with splay, not much to say, the basic is the same, every time delete a subtrees tree

#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <vector > #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #
Include<algorithm> using namespace std;
const int maxn=200010;
int n,m;
    struct SBT {int left,right,size,key;
        void init (int val) {left=right=0;
        size=1;
    Key=val;
}}TREE[MAXN];
int tot,root;
    void left_rotate (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_rotate (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,int flag) {if (!flag) {if (tree[tree[tree[x].left].left].sIze>tree[tree[x].right].size) right_rotate (x); else if (tree[tree[tree[x].left].right].size>tree[tree[x].right].size) left_rotate (tree[x].left), Right_rotat
        E (x);
    else return;
        } else {if (tree[tree[tree[x].right].right].size>tree[tree[x].left].size) left_rotate (x); else if (tree[tree[tree[x].right].left].size>tree[tree[x].left].size) right_rotate (tree[x].right), lef
        T_rotate (x);
    else return;
    } maintain (tree[x].left,0);
    Maintain (tree[x].right,1);
    Maintain (x,0);
Maintain (x,1);
        }//Insert a value of key for the node void insert (int &x,int key) {if (!x) {x=++tot;
    Tree[x].init (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 del (int &x,int key) {if (!x) return 0;
    int res=0;
    tree[x].size--; if (key==tree[x].key| |
       (key<tree[x].key&&tree[x].left==0) | |
    (key>tree[x].key&&tree[x].right==0))
            {if (tree[x].left&&tree[x].right) {int P=del (tree[x].left,key+1);
            Tree[x].key=tree[p].key;
        Res=p;
            } else {int p=x;
            X=tree[x].left+tree[x].right;
        Res=p;
    }} else Res=del (Key<tree[x].key?tree[x].left:tree[x].right,key);
    Maintain (X,key<tree[x].key);
return res;
    } void Delete (int &t,int delay) {if (!t) return;
        if (tree[t].key+delay<m) {t=tree[t].right;
    Delete (T,delay);
        } else {Delete (tree[t].left,delay);
    tree[t].size=tree[tree[t].left].size+tree[tree[t].right].size+1;
    }} int find (int &t,int k) {if (k<=tree[tree[t].right].size) return find (TREE[T].RIGHT,K); else if (k>tree[tree[t].right].size+1) return find (tree[T].left,k-tree[tree[t].right].size-1];
return tree[t].key;
    } int main () {char op[10];
    int x;
        while (scanf ("%d%d", &n,&m)!=eof) {int delay=0,ans=0;
        root=tot=0;
            while (n--) {scanf ("%s%d", op,&x);
                if (op[0]== ' I ') {if (x<m) continue;
            Insert (Root,x-delay);
            } else if (op[0]== ' A ') delay+=x;
                else if (op[0]== ' S ') {delay-=x;
            Delete (Root,delay);
        } else printf ("%d\n", Tree[root].size>=x?find (root,x) +delay:-1);
    } printf ("%d\n", tot-tree[root].size);
} return 0;

 }




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.