結果

問題 No.2809 Sort Query
ユーザー InTheBloomInTheBloom
提出日時 2024-07-13 01:54:48
言語 C++23(gcc13)
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,106 ms / 2,000 ms
コード長 6,047 bytes
コンパイル時間 2,354 ms
コンパイル使用メモリ 124,796 KB
実行使用メモリ 48,720 KB
最終ジャッジ日時 2024-07-13 01:55:52
合計ジャッジ時間 62,602 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 823 ms
39,084 KB
testcase_02 AC 802 ms
39,348 KB
testcase_03 AC 763 ms
39,096 KB
testcase_04 AC 794 ms
39,092 KB
testcase_05 AC 752 ms
39,220 KB
testcase_06 AC 527 ms
34,360 KB
testcase_07 AC 484 ms
34,484 KB
testcase_08 AC 499 ms
34,484 KB
testcase_09 AC 537 ms
34,388 KB
testcase_10 AC 487 ms
34,616 KB
testcase_11 AC 691 ms
38,244 KB
testcase_12 AC 641 ms
40,848 KB
testcase_13 AC 685 ms
38,244 KB
testcase_14 AC 682 ms
38,248 KB
testcase_15 AC 727 ms
38,116 KB
testcase_16 AC 712 ms
38,244 KB
testcase_17 AC 661 ms
38,248 KB
testcase_18 AC 719 ms
38,320 KB
testcase_19 AC 674 ms
38,116 KB
testcase_20 AC 702 ms
38,184 KB
testcase_21 AC 1,106 ms
47,460 KB
testcase_22 AC 1,050 ms
48,720 KB
testcase_23 AC 1,067 ms
47,592 KB
testcase_24 AC 1,034 ms
47,460 KB
testcase_25 AC 1,100 ms
47,456 KB
testcase_26 AC 874 ms
41,148 KB
testcase_27 AC 889 ms
41,156 KB
testcase_28 AC 882 ms
41,020 KB
testcase_29 AC 844 ms
41,028 KB
testcase_30 AC 914 ms
41,000 KB
testcase_31 AC 573 ms
33,984 KB
testcase_32 AC 558 ms
35,448 KB
testcase_33 AC 552 ms
34,256 KB
testcase_34 AC 520 ms
33,988 KB
testcase_35 AC 546 ms
33,980 KB
testcase_36 AC 615 ms
38,120 KB
testcase_37 AC 623 ms
38,120 KB
testcase_38 AC 604 ms
38,400 KB
testcase_39 AC 650 ms
38,116 KB
testcase_40 AC 663 ms
38,116 KB
testcase_41 AC 863 ms
35,104 KB
testcase_42 AC 846 ms
37,128 KB
testcase_43 AC 919 ms
35,224 KB
testcase_44 AC 859 ms
36,740 KB
testcase_45 AC 954 ms
35,348 KB
testcase_46 AC 659 ms
36,488 KB
testcase_47 AC 664 ms
35,340 KB
testcase_48 AC 689 ms
35,092 KB
testcase_49 AC 620 ms
36,488 KB
testcase_50 AC 641 ms
36,876 KB
testcase_51 AC 540 ms
25,872 KB
testcase_52 AC 483 ms
25,748 KB
testcase_53 AC 467 ms
25,628 KB
testcase_54 AC 463 ms
25,812 KB
testcase_55 AC 451 ms
25,620 KB
testcase_56 AC 737 ms
25,584 KB
testcase_57 AC 537 ms
20,836 KB
testcase_58 AC 498 ms
21,884 KB
testcase_59 AC 518 ms
20,516 KB
testcase_60 AC 714 ms
26,828 KB
testcase_61 AC 769 ms
36,892 KB
testcase_62 AC 486 ms
21,856 KB
testcase_63 AC 692 ms
26,568 KB
testcase_64 AC 902 ms
37,928 KB
testcase_65 AC 546 ms
23,432 KB
testcase_66 AC 2 ms
5,376 KB
testcase_67 AC 3 ms
5,376 KB
testcase_68 AC 3 ms
5,376 KB
testcase_69 AC 2 ms
5,376 KB
testcase_70 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;
using ll = long long;

constexpr int iINF = 1'000'000'000;
constexpr ll llINF = 1'000'000'000'000'000'000;

// https://github.com/dyktr06/kyopro_library/blob/main/lib/data_structure/priority_set.hpp
// thank you very much dyktr_06

template <typename T>
struct PrioritySet{
    struct compress{
        vector<T> sorted, compressed;

        compress(){}

        void init(const vector<T> &vec){
            int n = vec.size();
            compressed.resize(n);
            for(T x : vec){
                sorted.emplace_back(x);
            }
            sort(sorted.begin(), sorted.end());
            sorted.erase(unique(sorted.begin(), sorted.end()), sorted.end());
            for(int i = 0; i < n; ++i){
                compressed[i] = lower_bound(sorted.begin(), sorted.end(), vec[i]) - sorted.begin();
            }
        }

        int get(const T &x) const{
            return lower_bound(sorted.begin(), sorted.end(), x) - sorted.begin();
        }

        T inv(const T &x) const{
            return sorted[x];
        }

        size_t size() const{
            return sorted.size();
        }

        vector<T> getCompressed() const{
            return compressed;
        }
    };

    struct BinaryIndexedTree{
        int N;
        vector<T> BIT;
        BinaryIndexedTree() {}

        void init(int size){
            N = size;
            BIT.assign(N + 1, 0);
        }

        T get(int i){
            return sum(i + 1) - sum(i);
        }

        void add(int i, T x){
            i++;
            while(i <= N){
                BIT[i] += x;
                i += i & -i;
            }
        }

        T sum(int i) const {
            T ans = 0;
            while(i > 0){
                ans += BIT[i];
                i -= i & -i;
            }
            return ans;
        }

        T sum(int L, int R) const {
            return sum(R) - sum(L);
        }

        int lower_bound(T x) const {
            if(x <= 0){
                return 0;
            } else{
                int v = 0, r = 1;
                while(r < N) r = r << 1;
                for(int len = r; len > 0; len = len >> 1){
                    if(v + len < N && BIT[v + len] < x){
                        x -= BIT[v + len];
                        v += len;
                    }
                }
                return v;
            }
        }

        int upper_bound(T x) const {
            if(x < 0){
                return 0;
            } else{
                int v = 0, r = 1;
                while(r <= N) r = r << 1;
                for(int len = r; len > 0; len = len >> 1){
                    if(v + len <= N && BIT[v + len] <= x){
                        x -= BIT[v + len];
                        v += len;
                    }
                }
                return v;
            }
        }

        T operator [](int i) const {
            return sum(i, i + 1);
        }
    };

    vector<T> a;
    compress comp;
    BinaryIndexedTree cnt, val;

    PrioritySet(){ }

    void add(T x){
        a.push_back(x);
    }

    void build(){
        comp.init(a);
        cnt.init(comp.size());
        val.init(comp.size());
    }

    T size(){
        return cnt.sum((int) comp.size());
    }

    void insert(T x, T count = 1){
        cnt.add(comp.get(x), count);
        val.add(comp.get(x), count * x);
    }

    void erase(T x, T count = 1){
        T idx = comp.get(x);
        if(cnt.get(idx) < count){
            count = cnt.get(idx);
        }
        cnt.add(idx, -count);
        val.add(idx, -count * x);
    }

    // 1-indexed
    T kth_small_element(T k){
        T idx = cnt.lower_bound(k);
        return comp.inv(idx);
    }

    T kth_large_element(T k){
        T rev_k = size() - k + 1;
        return kth_small_element(rev_k);
    }

    // 1-indexed
    T kth_small_sum(T k){
        if(size() < k){
            return val.sum((int) comp.size());
        }
        T idx = cnt.lower_bound(k);
        T sum = val.sum(idx);
        sum += comp.inv(idx) * (k - cnt.sum(idx));
        return sum;
    }

    T kth_large_sum(T k){
        if(size() < k){
            return val.sum((int) comp.size());
        }
        T rev_k = size() - k;
        return val.sum((int) comp.size()) - kth_small_sum(rev_k);
    }
};

int main () {
    int N, Q; cin >> N >> Q;
    vector<ll> A(N);
    for (int i = 0; i < N; i++) cin >> A[i];

    PrioritySet<ll> ps;
    for (int i = 0; i < N; i++) ps.add(A[i]);

    vector<int> t(Q), k(Q);
    vector<ll> x(Q);
    for (int i = 0; i < Q; i++) {
        cin >> t[i];
        if (t[i] == 1) {
            cin >> k[i] >> x[i];
            k[i]--;
            ps.add(x[i]);
        }
        if (t[i] == 3) {
            cin >> k[i];
            k[i]--;
        }
    }

    ps.build();
    for (int i = 0; i < N; i++) ps.insert(A[i]);

    queue<int> que;
    vector<bool> changed(N, true);
    for (int i = 0; i < N; i++) que.push(i);

    bool sorted = false;
    vector<ll> B = A;
    vector<ll> remove, add;

    for (int i = 0; i < Q; i++) {
        if (t[i] == 1) {
            if (!changed[k[i]]) que.push(k[i]);
            changed[k[i]] = true;
            B[k[i]] = x[i];
        }

        if (t[i] == 2) {
            while (!que.empty()) {
                int id = que.front();
                que.pop();
                ll r = A[id];
                if (sorted) r = ps.kth_small_element(id + 1);

                remove.push_back(r);
                add.push_back(B[id]);
                changed[id] = false;
            }

            for (auto v : remove) ps.erase(v);
            for (auto v : add) ps.insert(v);
            remove.resize(0);
            add.resize(0);

            sorted = true;
        }
        if (t[i] == 3) {
            if (changed[k[i]]) {
                cout << B[k[i]] << "\n";
            }
            else {
                cout << ps.kth_small_element(k[i] + 1) << "\n";
            }
        }
    }
}
0