結果

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

ソースコード

diff #

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

using ll = long long;

bool chmin(auto &a, auto b) { return a > b ? a = b, true : false; }
bool chmax(auto &a, auto b) { return a < b ? a = b, true : false; }

template<typename T1, typename T2 = long long, typename Compare = less<T1>>
struct top_k_set {
    top_k_set(int _k) : k(_k), lower_sum(0) {}
    void insert(T1 x) {
        if (k > 0 && ((int)lower.size() < k || Compare()(x, *prev(lower.end())))) {
            lower_sum += x;
            lower.insert(x);
        } else {
            upper.insert(x);
        }
        normalize();
    }
    void erase(T1 x) {
        auto it = lower.find(x);
        if (it != lower.end()) {
            lower_sum -= *it;
            lower.erase(it);
        } else {
            it = upper.find(x);
            assert(it != upper.end());
            upper.erase(it);
        }
        normalize();
    }
    T1 kth_val() const {
        assert((int)lower.size() == k);
        return *prev(lower.end());
    }
    T2 sum() const {
        return lower_sum;
    }
private:
    int k;
    multiset<T1, Compare> lower, upper;
    T2 lower_sum;
    void normalize() {
        while ((int)lower.size() > k) {
            auto it = prev(lower.end());
            upper.insert(*it);
            lower_sum -= *it;
            lower.erase(it);
        }
        while ((int)lower.size() < k && upper.size()) {
            auto it = upper.begin();
            lower_sum += *it;
            lower.insert(*it);
            upper.erase(it);
        }
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, K, Q;
    cin >> N >> K >> Q;
    top_k_set<ll> S(K);
    for (int i = 0; i < N; i++) {
        int A;
        cin >> A;
        S.insert(A);
    }
    while (Q--) {
        int t;
        cin >> t;
        if (t == 1) {
            int x;
            cin >> x;
            S.insert(x);
        } else if (t == 2) {
            int y;
            cin >> y;
            auto x = S.kth_val();
            S.erase(x);
            S.insert(x + y);
        } else {
            cout << S.kth_val() << '\n';
        }
    }
}
0