結果

問題 No.2809 Sort Query
ユーザー SlephySlephy
提出日時 2024-07-12 23:14:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,134 ms / 2,000 ms
コード長 9,983 bytes
コンパイル時間 2,495 ms
コンパイル使用メモリ 224,540 KB
実行使用メモリ 51,876 KB
最終ジャッジ日時 2024-07-12 23:15:56
合計ジャッジ時間 58,550 ms
ジャッジサーバーID
(参考情報)
judge4 / judge6
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 716 ms
31,300 KB
testcase_02 AC 664 ms
30,928 KB
testcase_03 AC 681 ms
30,252 KB
testcase_04 AC 696 ms
30,248 KB
testcase_05 AC 724 ms
30,044 KB
testcase_06 AC 390 ms
21,804 KB
testcase_07 AC 413 ms
21,668 KB
testcase_08 AC 402 ms
21,672 KB
testcase_09 AC 422 ms
21,672 KB
testcase_10 AC 399 ms
21,676 KB
testcase_11 AC 420 ms
35,364 KB
testcase_12 AC 398 ms
35,236 KB
testcase_13 AC 399 ms
35,204 KB
testcase_14 AC 431 ms
35,284 KB
testcase_15 AC 406 ms
35,192 KB
testcase_16 AC 406 ms
35,304 KB
testcase_17 AC 404 ms
35,296 KB
testcase_18 AC 405 ms
35,404 KB
testcase_19 AC 408 ms
35,368 KB
testcase_20 AC 429 ms
35,244 KB
testcase_21 AC 934 ms
51,748 KB
testcase_22 AC 959 ms
51,620 KB
testcase_23 AC 975 ms
51,876 KB
testcase_24 AC 963 ms
51,748 KB
testcase_25 AC 955 ms
51,748 KB
testcase_26 AC 1,089 ms
36,220 KB
testcase_27 AC 1,086 ms
36,096 KB
testcase_28 AC 1,097 ms
36,244 KB
testcase_29 AC 1,102 ms
36,100 KB
testcase_30 AC 1,096 ms
36,224 KB
testcase_31 AC 487 ms
27,916 KB
testcase_32 AC 493 ms
27,896 KB
testcase_33 AC 496 ms
28,284 KB
testcase_34 AC 492 ms
28,004 KB
testcase_35 AC 519 ms
27,960 KB
testcase_36 AC 391 ms
35,204 KB
testcase_37 AC 354 ms
35,132 KB
testcase_38 AC 358 ms
35,168 KB
testcase_39 AC 386 ms
35,336 KB
testcase_40 AC 343 ms
35,364 KB
testcase_41 AC 1,119 ms
27,676 KB
testcase_42 AC 1,100 ms
27,792 KB
testcase_43 AC 1,047 ms
27,776 KB
testcase_44 AC 1,086 ms
27,776 KB
testcase_45 AC 1,134 ms
27,792 KB
testcase_46 AC 1,016 ms
27,708 KB
testcase_47 AC 1,061 ms
27,776 KB
testcase_48 AC 967 ms
27,784 KB
testcase_49 AC 1,040 ms
27,840 KB
testcase_50 AC 1,041 ms
27,780 KB
testcase_51 AC 772 ms
27,704 KB
testcase_52 AC 648 ms
27,708 KB
testcase_53 AC 623 ms
27,748 KB
testcase_54 AC 580 ms
27,800 KB
testcase_55 AC 620 ms
27,708 KB
testcase_56 AC 608 ms
20,900 KB
testcase_57 AC 498 ms
18,728 KB
testcase_58 AC 425 ms
17,832 KB
testcase_59 AC 588 ms
19,364 KB
testcase_60 AC 371 ms
23,128 KB
testcase_61 AC 515 ms
27,800 KB
testcase_62 AC 447 ms
18,424 KB
testcase_63 AC 593 ms
25,348 KB
testcase_64 AC 648 ms
29,692 KB
testcase_65 AC 334 ms
17,176 KB
testcase_66 AC 2 ms
5,376 KB
testcase_67 AC 2 ms
5,376 KB
testcase_68 AC 2 ms
5,376 KB
testcase_69 AC 1 ms
5,376 KB
testcase_70 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = (int)1e9 + 1001010;
const ll llINF = (long long)4e18 + 22000020;
const string endn = "\n";
template <class T> inline auto vector2(size_t i, size_t j, const T &init = T()) {return vector(i, vector<T>(j, init));}
const string ELM_SEP = " ", VEC_SEP = endn;
template<class T> istream& operator >>(istream &i, vector<T> &A) {for(auto &I : A) {i >> I;} return i;}
template<class T> ostream& operator <<(ostream &o, const vector<T> &A) {int i=A.size(); for(const auto &I : A){o << I << (--i ? ELM_SEP : "");} return o;}
template<class T> ostream& operator <<(ostream &o, const vector<vector<T>> &A) {int i=A.size(); for(const auto &I : A){o << I << (--i ? VEC_SEP : "");} return o;}
template<class T> vector<T>& operator ++(vector<T> &A, int n) {for(auto &I : A) {I++;} return A;}
template<class T> vector<T>& operator --(vector<T> &A, int n) {for(auto &I : A) {I--;} return A;}
template<class T, class U> bool chmax(T &a, const U &b) {return ((a < b) ? (a = b, true) : false);}
template<class T, class U> bool chmin(T &a, const U &b) {return ((a > b) ? (a = b, true) : false);}
ll floor(ll a, ll b){if (b < 0) a = -a, b = -b; if(a >= 0) return a / b; else return (a + 1) / b - 1;}
ll ceil(ll a, ll b){if (b < 0) a = -a, b = -b; if(a > 0) return (a - 1) / b + 1; else return a / b;}
ll bit(unsigned long long val, unsigned long long digit){return (val >> digit) & 1;}
#ifdef DEBUG
#include <cpp-dump/dump.hpp>
#define dump(...) cpp_dump(__VA_ARGS__)
namespace cp = cpp_dump;
struct InitCppDump{
    InitCppDump(){
        if(!isatty(fileno(stderr))) CPP_DUMP_SET_OPTION(es_style, cpp_dump::es_style_t::no_es);
        CPP_DUMP_SET_OPTION(log_label_func, cp::log_label::line());
        CPP_DUMP_SET_OPTION(max_iteration_count, 30);
    }
} init_cpp_dump;
#else
#define dump(...)
#endif
// ================================== ここまでテンプレ ==================================

template<typename T>
class Compressor{
    vector<T> vals;

public:
    // コンストラクタ
    // O(NlogN)
    Compressor(const vector<vector<T>> &data){
        vals.reserve(data.size() * data[0].size());
        for(int i = 0; i < data.size(); i++){
            std::copy(data[i].begin(), data[i].end(), back_inserter(vals));
        }
        sort(vals.begin(), vals.end());
        vals.erase(unique(vals.begin(), vals.end()), vals.end());
    }
    Compressor(const vector<T> &data) : vals(data){
        sort(vals.begin(), vals.end());
        vals.erase(unique(vals.begin(), vals.end()), vals.end());
    }

    // 圧縮前の値 value に対応する圧縮後の値 index を返す
    // O(logN)
    int get_index(T value){
        auto lb = lower_bound(vals.begin(), vals.end(), value);
        assert(lb != vals.end() && *lb == value);
        return (int)(lb - vals.begin());
    }

    // 圧縮後の値 index に対応する圧縮前の値 value を返す
    // O(1)
    T get_value(size_t index){
        return vals.at(index);
    }

    // 圧縮後の配列のサイズを返す
    // O(1)
    int size() const {
        return vals.size();
    }
};

// ACLの実装を一部改変したもの
// == 変更点 ==
// internal::ceil_pow2 を埋め込み
// add関数 を追加
// size関数 を追加
template <class S, S (*op)(S, S), S (*e)()> struct segtree {
  public:
    segtree() : segtree(0) {}
    explicit segtree(int n) : segtree(vector<S>(n, e())) {}
    explicit segtree(const vector<S>& v) : _n(int(v.size())) {
        log = 0;
        while((1U << log) < (unsigned int)(_n)) log++;
        _size = 1 << log;
        d = vector<S>(2 * _size, e());
        for (int i = 0; i < _n; i++) d[_size + i] = v[i];
        for (int i = _size - 1; i >= 1; i--) {
            update(i);
        }
    }

    size_t size() const {
        return _n;
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += _size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    void add(int p, S x){
        assert(0 <= p && p < _n);
        p += _size;
        d[p] += x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) const {
        assert(0 <= p && p < _n);
        return d[p + _size];
    }

    S prod(int l, int r) const {
        assert(0 <= l && l <= r && r <= _n);
        S sml = e(), smr = e();
        l += _size;
        r += _size;

        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    S all_prod() const { return d[1]; }

    template <bool (*f)(S)> int max_right(int l) const {
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) const {
        assert(0 <= l && l <= _n);
        // assert(f(e()));
        if (l == _n) return _n;
        l += _size;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < _size) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - _size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*f)(S)> int min_left(int r) const {
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) const {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += _size;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < _size) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - _size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

    friend ostream& operator<<(ostream &os, const segtree &st) {
        os << "[";
        for (int i = 0; i < st._n; i++) {
            if (i != 0) os << ", ";
            os << st.get(i);
        }
        os << "]";
        return os;
    }

  private:
    int _n, _size, log;
    vector<S> d;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};


template<class S> struct RangeSum {
    static S op(S l, S r){ return l + r; }
    static S e(){ return 0; }
    using Type = struct segtree<S, op, e>;
};
template<class S> struct RangeMin {
    static S op(S l, S r){ return min(l, r); }
    static S e(){ return numeric_limits<S>::max(); }
    using Type = struct segtree<S, op, e>;
};
template<class S> struct RangeMax {
    static S op(S l, S r){ return max(l, r); }
    static S e(){ return numeric_limits<S>::min(); }
    using Type = struct segtree<S, op, e>;
};
// ex) RangeMin<int>::Type seg(10);

template<class Judgement>
ll binary_search_ll(ll ok, ll ng, Judgement judge){
    auto need_continue = [&]() -> bool {
        if(ok > ng) return((__int128_t)ok - (__int128_t)ng > 1);
        else return ((__int128_t)ng - (__int128_t)ok > 1);
    };
    while(need_continue()){
        ll mid = ((__int128_t)ok + (__int128_t)ng) / 2;
        if(judge(mid)) ok = mid;
        else ng = mid;
    }
    return ok;
};

int main(int argc, char *argv[]){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    ll n, q; cin >> n >> q;
    vector<ll> a(n); cin >> a;
    vector<ll> all_a = a;
    vector<tuple<ll, ll, ll>> ops(q);
    for(int qid = 0; qid < q; qid++){
        auto &[v0, v1, v2] = ops[qid];
        cin >> v0;
        if(v0 == 1){
            cin >> v1 >> v2;
            v1--;
            all_a.emplace_back(v2);
        }
        if(v0 == 2){
        }
        if(v0 == 3){
            cin >> v1;
            v1--;
        }
    }

    Compressor<ll> comp(all_a);
    RangeSum<ll>::Type seg(comp.size());

    for(int i = 0; i < a.size(); i++){
        seg.add(comp.get_index(a[i]), 1);
    }

    map<ll, ll> changed; // {a_idx, comp_idx}
    vector<ll> will_delete; // comp_idx
    bool sorted = false;
    for(int qid = 0; qid < q; qid++){
        const auto &[op, k, x] = ops[qid];

        dump(op, k, x);

        auto judge = [&](ll mid) -> bool {
            // [0, mid] >= sum ?
            ll sum = seg.prod(0, mid+1);
            return (sum >= k+1);
        };


        auto get_comp_idx = [&](ll k) -> ll {
            // return comp_idx of a[k]
            if(sorted) return binary_search_ll((int)comp.size()-1, -1, judge);
            else if(changed.count(k)) return changed[k];
            else return comp.get_index(a[k]);
        };

        if(op == 1){
            // change
            if(!changed.count(k)){
                ll comp_idx = get_comp_idx(k);
                dump(comp_idx);
                will_delete.emplace_back(comp_idx);
            }
            changed[k] = comp.get_index(x);
        }
        if(op == 2){
            // sort
            for(auto [a_idx, comp_idx] : changed){
                seg.add(comp_idx, 1);
            }
            for(auto idx : will_delete){
                seg.add(idx, -1);
            }

            changed.clear();
            will_delete.clear();
            sorted = true;
        }
        if(op == 3){
            // output
            if(changed.count(k)){
                cout << comp.get_value(changed[k]) << endl;
            }
            else{
                dump(seg);
                // ll comp_idx = binary_search_ll((int)comp.size()-1, -1, judge);
                ll comp_idx = get_comp_idx(k);
                dump(comp_idx);
                cout << comp.get_value(comp_idx) << endl;
            }
        }
    }
    return 0;
}
0