NOI2004 Depressed Teller (application of SBT)

Source: Internet
Author: User
Tags join printf time limit

1503: [NOI2004] Depressed cashier time limit:  5 sec   memory limit:  64 MB
Commit: 1530    Resolution:  538
[Submit] [] topic 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 The number of rows for the output 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
I (
i) 2
S 2
F 5
I
F 1
F


Sample output Ten

-1
2

prompt

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

Title: http://www.zybbs.org/JudgeOnline/problem.php?id=1503

Analysis: This problem with almost all SBT operation, it feels quite suitable for familiar with the data structure of SBT, as long as pay attention to the addition and subtraction of all members of the payroll variable statistics, the new employees first to lose the current variable value and then join, The new members will also have to determine whether to meet the conditions and then join (although not sure the data is not so embarrassing)

Code:

#include <cstdio> #include <iostream> using namespace std;
const int mm=1111111;
int L[MM],R[MM],S[MM],V[MM];
int I,j,k,n,limit,tt,root,add,sum,ans;
Char C;
    void right_rotate (int &t) {int k=l[t];
    L[T]=R[K];
    r[k]=t;
    S[K]=S[T];
    s[t]=s[l[t]]+s[r[t]]+1;
T=k;
    } void left_rotate (int &t) {int k=r[t];
    R[T]=L[K];
    l[k]=t;
    S[K]=S[T];
    s[t]=s[l[t]]+s[r[t]]+1;
T=k;
        } void maintain (int &t,bool flag) {if (flag) if (S[r[r[t]]]>s[l[t]]) left_rotate (t);
        else if (S[l[r[t]]]>s[l[t]]) right_rotate (r[t]), left_rotate (t);
    else return;
        else if (S[l[l[t]]]>s[r[t]]) right_rotate (t);
        else if (S[r[l[t]]]>s[r[t]]) left_rotate (l[t]), right_rotate (t);
    else return;
    Maintain (l[t],0);
    Maintain (r[t],1);
    Maintain (t,0);
Maintain (t,1);
        } void Insert (int &t,int v) {if (t) {++s[t];
        if (V<v[t]) Insert (L[T],V); else InserT (R[T],V);
    Maintain (t,v>=v[t]);
        } else {s[t=++tt]=1;
        V[t]=v;
    l[t]=r[t]=0;
    }} int Delete (int &t,int v) {--s[t]; if (v==v[t]| | v<v[t]&&! l[t]| | v>v[t]&&!
        R[t]) {int tmp=v[t]; if (! l[t]| |!
        R[T]) t=l[t]+r[t];
        else V[t]=delete (l[t],v[t]+1);
    return TMP;
    } else if (V<v[t]) return Delete (L[T],V);
else return Delete (R[T],V); } int Find (int t,int v) {while (t&&v!=v[t]) t=v<v[t]?
    Find (L[T],V): Find (R[T],V);
return t;
    } int Rank (int t,int v) {if (!t) return 1;
    if (V<=v[t]) return Rank (L[T],V);
else return S[l[t]]+1+rank (R[T],V);
    } int Select (int t,int k) {if (k==s[l[t]]+1) return v[t];
    if (K<=s[l[t]]) return Select (L[T],K);
else return Select (R[t],k-1-s[l[t]]);
    } int Pred (int t,int v) {if (!t) return v;
    if (V<=v[t]) return Pred (L[T],V);
        else {int tmp=pred (R[T],V); Return tmp==v?
 v[t]:tmp;   }} int Succ (int t,int v) {if (!t) return v;
    if (V>=v[t]) return SUCC (R[T],V);
        else {int tmp=succ (L[T],V); Return tmp==v?
    v[t]:tmp;
    }} int main () {scanf ("%d%d", &n,&limit);
    ans=sum=add=root=tt=s[0]=0; for (I=0;i<n;++i) {while (C=getchar ()) < ' A ' | |
        C> ' Z ');
        scanf ("%d", &k);
        if (c== ' I ' &&k>=limit) Insert (Root,k-add), ++sum;
        if (c== ' A ') add+=k;
            if (c== ' S ') {add-=k;
        while (sum&& (J=select (root,1)) +add<limit) Delete (root,j),--Sum,++ans;
            if (c== ' F ') if (k>sum) puts ("-1");
    else printf ("%d\n", select (root,sum-k+1) +add);
    } printf ("%d\n", ans);
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.