zoukankan      html  css  js  c++  java
  • Atcoder 212D Querying Multiset

    Problem Statement

    Takahashi has many balls, on which nothing is written, and one bag. Initially, the bag is empty. Takahashi will do Q operations, each of which is of one of the following three types.

    • Type 1: Write an integer Xi on a blank ball and put it in the bag.
    • Type 2: For each ball in the bag, replace the integer written on it with that integer plus Xi.
    • Type 3: Pick up the ball with the smallest integer in the bag (if there are multiple such balls, pick up one of them). Record the integer written on this ball and throw it away.

    For each 1≤i≤Q, you are given the type Pi of the i-th operation and the value of Xi if the operation is of Type 1 or 2. Print the integers recorded in the operations of Type 3 in order.

    Constraints

    • 1≤Q≤2×105
    • 1≤Pi≤3
    • 1≤Xi≤109
    • All values in input are integers.
    • There is one or more ii such that Pi=3
    • If Pi=3, the bag contains at least one ball just before the i-th operation.

    Input

    Input is given from Standard Input in the following format:

    Q
    query1
    query2
    ::
    queryQ
    

    Each queryi in the 2-nd through (Q+1)-th lines is in the following format:

    1 Xi
    2 Xi
    3
    

    The first number in each line is 1≤Pi≤3, representing the type of the operation. If Pi=1 or Pi=2, it is followed by a space, and then by Xi.

    Output

    For each operation with Pi=3 among the Q operations, print the recorded integer in its own line.


    Sample Input 1 Copy

    5
    1 3
    1 5
    3
    2 2
    3
    

    Sample Output 1 Copy

    3
    7
    

    Takahashi will do the following operations.

    • Write 3 on a ball and put it in the bag.
    • Write 5 on a ball and put it in the bag.
    • The bag now contains a ball with 3 and another with 5. Pick up the ball with the smaller of them, or 3. Record 3 and throw it away.
    • The bag now contains just a ball with 5. Replace this integer with 5+2=7.
    • The bag now contains just a ball with 7. Pick up this ball, record 7, and throw it away.

    Therefore, we should print 3 and 7, in the order they are recorded.


    Sample Input 2 Copy

    6
    1 1000000000
    2 1000000000
    2 1000000000
    2 1000000000
    2 1000000000
    3
    

    Sample Output 2 Copy

    5000000000
    

    Note that the outputs may not fit into a 32-bit integer.

    题目翻译

    3种操作。操作1:将一个数字(X_i)写入。操作2:所有数字加上(X_i)。操作3:读取并删除最小的数字

    (Q<=2*10^5,X_i<=10^9)

    题目解析

    要点在于如何快速维护所有当前数字(+X_i)

    考虑对数字的增加进行全局维护,维护全局的相加和(nowsum),最后读取时加上(nowsum)表示操作2的影响

    当插入一个数字时,则减去当前的(nowsum),即抵消掉之前的操作2的影响

    每次将(x_i-nowsum)插入有限队列,读取时(+nowsum)

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #include<queue>
    using namespace std;
    typedef long long ll;
    priority_queue<ll,vector<ll>,greater<ll> >q;
    ll nowsum=0;
    int main(){
        int Q,opt,x;
        cin>>Q;
        while (Q--){
            scanf("%d",&opt);
            if (opt==1){
                scanf("%d",&x);
                q.push(x-nowsum);
            }else if (opt==2){
                scanf("%d",&x);
                nowsum+=x;
            }else{
                printf("%lld
    ",q.top()+nowsum);
                q.pop();
            }
        }
    }
    
  • 相关阅读:
    参数模型和非参数模型
    windows上安装第二个mysql
    Mysql 连接数,最大并发数设置
    mybatis 一次执行多条sql
    微信小程序发布新版本的小程序之后,从发现-小程序中启动,还是看的到旧版本。[转]
    Fragment调用startActivityForResult导致的回调Activity无法获取正确的requestId的问题
    不可重入锁和可重入锁
    Android gradle 各种版本下载
    android 各国语言对应的缩写
    解决乱码setCharacterEncoding("UTF-8")不生效的问题
  • 原文地址:https://www.cnblogs.com/Y-E-T-I/p/15135234.html
Copyright © 2011-2022 走看看