結果

問題 No.3298 K-th Slime
ユーザー karinohito
提出日時 2025-10-05 14:04:09
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 3,100 bytes
コンパイル時間 2,864 ms
コンパイル使用メモリ 281,308 KB
実行使用メモリ 10,496 KB
最終ジャッジ日時 2025-10-05 14:04:14
合計ジャッジ時間 5,177 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll =long long;


template<class T>
struct K_small {
    ll K;
    T sum;
    multiset<T> small, big;
    K_small(ll _K) :K() {
        sum = e();
        K = _K;
    }

    // xを追加する. 小さい方に追加されたら1を返す.
    bool insert(T x) {
        if (ll(small.size()) < K) {
            small.insert(x);
            sum = op(sum, x);
            return 1;
        }
        else {
            if(K==0){
                big.insert(x);
                return 0;
            }

            auto sla = small.end();
            sla--;
            T sb = *sla;
            if (sb > x) {
                small.insert(x);
                small.erase(sla);
                big.insert(sb);
                sum = op(sum, x);
                sum = op(sum, inv(sb));
                return 1;
            }
            else {
                big.insert(x);
                return 0;
            }
        }
    }


    bool erase(T x) {
        auto se=small.find(x);
        auto gb=big.find(x);
        assert(se != small.end() || gb != big.end());
        
        if (gb != big.end()) {
            big.erase(gb);
            return 0;
        }
        else {
            small.erase(se);
            sum = op(sum, inv(x));
            if (ll(big.size()) == 0)return 0;
            else {
                auto bs = big.begin();
                T ad = *bs;
                big.erase(bs);
                small.insert(ad);
                sum = op(sum, ad);
                return 1;
            }
        }
    }

    void changeK(ll NK) {
        K = NK;
        assert(ll(small.size()) + ll(big.size()) >= K);
        while (ll(small.size()) < K) {
            auto p = big.begin();
            T ns = *p;
            sum = op(sum, ns);
            small.insert(ns);
            big.erase(p);
        }
        while (ll(small.size()) > K) {
            auto p = small.end();
            p--;
            T ns = *p;
            sum = op(sum, inv(ns));
            big.insert(ns);
            small.erase(p);
        }
        return;
    }
    T get(){
        return *(prev(small.end()));
    }

    T pr() {
        return sum;
    }

    void debug(){
        cout<<"small:";
        for(auto s:small)cout<<" "<<s;
        cout<<endl;
        cout<<"big:";
        for(auto b:big)cout<<" "<<b;
        cout<<endl<<endl;
    }

    T e() {
        return 0;
    }
    T op(T a, T b) {
        return a + b;
    }
    T inv(T a) {
        return -a;
    }
};


int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    ll N,K,Q;
    cin>>N>>K>>Q;
    K_small<ll> D(K);
    for(int i=0;i<N;i++){
        int A;
        cin>>A;
        D.insert(A);
    }
    for(int q=0;q<Q;q++){
        int t;
        cin>>t;
        if(t==1){
            ll x;
            cin>>x;
            D.insert(x);
        }
        else if(t==2){
            ll y;
            cin>>y;
            ll d=D.get();
            D.erase(d);
            D.insert(d+y);
        }
        else{
            cout<<D.get()<<"\n";
        }
    }

}  

0