[LuoguP1801] black box

Title Description

Black Box is a primitive database. It can store an array of integers, and a special variable i. The very beginning of Black Box is empty. And i is equal to 0. The Black Box to deal with a string of commands.

Only two commands:

ADD (x): x elements into the BlackBox;

GET: i plus 1, then the output Blackhox in small numbers of i.

Remember: the i-th smaller number is the number of Black Box in ascending sort order after a large i-th element. E.g:

We have to demonstrate what a command 11 command string. (As shown below)

Now asked to find the best treatment for a given command string. ADD and GET command up to 200,000 respectively. Now represented by two array of integers command string:

1.A (1), A (2), ... A (M): a series of elements to be placed in the Black Box. Each number is no more than the absolute value of the integer 2 billion, M $ 200000. For example, the above example is A = (3,1, a 4,2,8, -1000,2).

2.u (1), u (2), ... u (N): represents u (j) elements are placed inside the Black Box after the emergence of a GET command. For example, in the above example u = (l, 2,6,6). Input data is not wrong ruling.

Input Format

The first line, two integers, M, N.

Second row, M integers, indicates A (l)

……A(M)。

Third row, N integers, represents u (l)

... u (N).

Output Format

The output of the output string of Black Box derived command string, a digit line.

Sample input and output

Input # 1
7 4
3 1 -4 2 8 -1000 2
1 2 6 6
Output # 1
3
3
1
2

Description / Tips

For 30% of the data, M≤10000;

For 50% of the data, M≤100000:

To 100% of the data, M≤200000.

 

A large root heap maintenance before the i-th

A small root maintain the rest of the heap

It can also be directly balanced tree

 

#include <bits/stdc++.h>
using namespace std;
priority_queue<int> q1;
priority_queue<int, vector<int>, greater<int> > q2;
int m, n;
int a[200000 + 10], u[200000 + 10];
int main(){
    cin >> m >> n;
    for(int i = 1; i <= m; i++) cin >> a[i];
    for(int i = 1; i <= n; i++) cin >> u[i];
    for(int i = 1; i <= u[1]; i++){
        q2.push(a[i]);
    }
    cout << q2.top() << endl;
    for(int i = 2; i <= n; i++){
        q1.push(q2.top()); q2.pop();
        for(int j = u[i - 1] + 1; j <= u[i]; j++){
            q1.push(a[j]);
            q2.push(q1.top());
            q1.pop();
        }
        cout << q2.top() << endl;
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/ruoruoruo/p/12101407.html