結果

問題 No.2809 Sort Query
ユーザー umimelumimel
提出日時 2024-07-12 22:54:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 817 ms / 2,000 ms
コード長 3,730 bytes
コンパイル時間 2,523 ms
コンパイル使用メモリ 190,820 KB
実行使用メモリ 38,320 KB
最終ジャッジ日時 2024-07-12 22:55:29
合計ジャッジ時間 49,055 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 672 ms
37,144 KB
testcase_02 AC 555 ms
37,820 KB
testcase_03 AC 520 ms
36,668 KB
testcase_04 AC 527 ms
37,820 KB
testcase_05 AC 522 ms
36,664 KB
testcase_06 AC 437 ms
35,844 KB
testcase_07 AC 415 ms
35,808 KB
testcase_08 AC 425 ms
36,332 KB
testcase_09 AC 410 ms
35,584 KB
testcase_10 AC 410 ms
35,948 KB
testcase_11 AC 506 ms
35,468 KB
testcase_12 AC 467 ms
35,088 KB
testcase_13 AC 469 ms
35,792 KB
testcase_14 AC 506 ms
35,816 KB
testcase_15 AC 485 ms
35,588 KB
testcase_16 AC 521 ms
35,068 KB
testcase_17 AC 473 ms
35,840 KB
testcase_18 AC 526 ms
36,400 KB
testcase_19 AC 482 ms
35,524 KB
testcase_20 AC 499 ms
36,708 KB
testcase_21 AC 810 ms
38,144 KB
testcase_22 AC 817 ms
38,164 KB
testcase_23 AC 765 ms
37,748 KB
testcase_24 AC 816 ms
38,252 KB
testcase_25 AC 810 ms
38,212 KB
testcase_26 AC 699 ms
37,160 KB
testcase_27 AC 705 ms
38,320 KB
testcase_28 AC 733 ms
36,916 KB
testcase_29 AC 708 ms
37,772 KB
testcase_30 AC 713 ms
37,224 KB
testcase_31 AC 364 ms
36,108 KB
testcase_32 AC 367 ms
35,280 KB
testcase_33 AC 362 ms
36,692 KB
testcase_34 AC 404 ms
36,184 KB
testcase_35 AC 358 ms
35,912 KB
testcase_36 AC 417 ms
35,412 KB
testcase_37 AC 445 ms
35,516 KB
testcase_38 AC 421 ms
36,288 KB
testcase_39 AC 413 ms
36,104 KB
testcase_40 AC 421 ms
35,640 KB
testcase_41 AC 580 ms
36,832 KB
testcase_42 AC 603 ms
36,272 KB
testcase_43 AC 583 ms
37,308 KB
testcase_44 AC 586 ms
36,956 KB
testcase_45 AC 622 ms
37,140 KB
testcase_46 AC 513 ms
37,104 KB
testcase_47 AC 546 ms
37,708 KB
testcase_48 AC 534 ms
36,844 KB
testcase_49 AC 529 ms
36,952 KB
testcase_50 AC 558 ms
36,892 KB
testcase_51 AC 537 ms
36,736 KB
testcase_52 AC 454 ms
36,452 KB
testcase_53 AC 434 ms
36,304 KB
testcase_54 AC 462 ms
36,704 KB
testcase_55 AC 404 ms
37,732 KB
testcase_56 AC 374 ms
24,480 KB
testcase_57 AC 300 ms
20,052 KB
testcase_58 AC 293 ms
21,040 KB
testcase_59 AC 306 ms
19,008 KB
testcase_60 AC 368 ms
27,664 KB
testcase_61 AC 473 ms
33,004 KB
testcase_62 AC 289 ms
20,208 KB
testcase_63 AC 382 ms
25,852 KB
testcase_64 AC 541 ms
35,136 KB
testcase_65 AC 301 ms
23,584 KB
testcase_66 AC 2 ms
6,944 KB
testcase_67 AC 2 ms
6,940 KB
testcase_68 AC 2 ms
6,940 KB
testcase_69 AC 2 ms
6,944 KB
testcase_70 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'void solve()':
main.cpp:108:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  108 |     for(auto [type, k, x] : query){
      |              ^

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
const ll MOD1000000007 = 1000000007;
const ll MOD998244353 = 998244353;
const ll MOD[3] = {999727999, 1070777777, 1000000007};
const ll LINF = 1LL << 60LL;
const int IINF = (1 << 30) - 1;


template<typename T>
struct fenwick_tree{
    int n;
    vector<T> dat;

    // constractor
    fenwick_tree(){}
    
    fenwick_tree(int n) : n(n){
        dat.resize(n+1, 0);
    }

    /*
        - return sum in range [0, i]
        - query time : O(log n)
    */
    T sum(int i){
        T s = 0;
        while(i > 0){
            s += dat[i];
            i -= i & -i;
        }
        return s;
    }

    /*
        - return sum in range [l, r)
        - query time : O(log n)
    */
    T sum(int l, int r){
        return sum(r) - sum(l);
    }

    /*
        - add x to dat[i+1]
        - processing time : O(log n)
    */
   void add(int i, T x){
        i++;
        while(i <= n){
            dat[i] += x;
            i += i & -i;
        }
    }
};


void solve(){
    int n, q; cin >> n >> q;
    vector<ll> a(n);
    for(int i=0; i<n; i++) cin >> a[i];
    vector<tuple<int, int, ll>> query(q+n);
    for(int i=0; i<n; i++){
        query[i] = {1, i, a[i]};
        a[i] = 0;
    }
    q += n;
    for(int i=n; i<q; i++){
        int type; cin >> type;
        if(type == 1){
            int k;
            ll x;
            cin >> k >> x;
            query[i] = {1, k-1, x};
        }
        if(type == 2){
            query[i] = {2, -1, -1};
        }
        if(type == 3){
            int k; cin >> k;
            query[i] = {3, k-1, -1};
        }
    }

    int siz = 0;
    vector<ll> comp;
    for(int i=0; i<q; i++) comp.pb(get<2>(query[i]));
    sort(all(comp));
    comp.erase(unique(all(comp)), comp.end());
    for(int i=0; i<q; i++){
        int tmp = lower_bound(all(comp), get<2>(query[i])) - comp.begin();
        query[i] = {get<0>(query[i]), get<1>(query[i]), tmp};
    }
    siz = (int)comp.size();

    fenwick_tree<int> ft1(siz), ft2(n);
    ft1.add(0, n);
    set<int> st;
    for(auto [type, k, x] : query){
        if(type == 1){
            if(a[k] == 0){
                int cnt = k+1 - ft2.sum(0, k+1);
                int lo = 0, mid, hi = siz-1; 
                while(hi - lo > 1){
                    mid = (lo + hi)/2;

                    if(cnt <= ft1.sum(0, mid+1)) hi = mid;
                    else lo = mid;
                }

                ft1.add(hi, -1);
                st.insert(k);
                ft2.add(k, 1);
            }
            a[k] = x;
        }
        if(type == 2){
            for(auto idx : st){
                ft1.add(a[idx], 1);
                ft2.add(idx, -1);
                a[idx] = 0;
            }
            set<int> new_st;
            st = new_st;
        }
        if(type == 3){
            int ans = 0;
            if(a[k] != 0){
                ans = a[k];
            }else{
                int cnt = k+1 - ft2.sum(0, k+1);
                int lo = 0, mid, hi = siz-1; 
                while(hi - lo > 1){
                    mid = (lo + hi)/2;

                    if(cnt <= ft1.sum(0, mid+1)) hi = mid;
                    else lo = mid;
                }

                ans = hi;
            }

            cout << comp[ans] << '\n';
        }
    }
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    int T=1;
    //cin >> T;
    while(T--) solve();
}
0