結果

問題 No.649 ここでちょっとQK!
ユーザー kyunakyuna
提出日時 2019-08-24 13:06:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 412 ms / 3,000 ms
コード長 2,197 bytes
コンパイル時間 1,304 ms
コンパイル使用メモリ 91,832 KB
実行使用メモリ 22,904 KB
最終ジャッジ日時 2024-04-22 13:15:23
合計ジャッジ時間 9,687 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 292 ms
5,632 KB
testcase_04 AC 280 ms
22,780 KB
testcase_05 AC 281 ms
22,904 KB
testcase_06 AC 186 ms
8,820 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 187 ms
12,132 KB
testcase_13 AC 187 ms
12,128 KB
testcase_14 AC 181 ms
12,132 KB
testcase_15 AC 184 ms
12,120 KB
testcase_16 AC 185 ms
12,132 KB
testcase_17 AC 206 ms
13,140 KB
testcase_18 AC 225 ms
14,032 KB
testcase_19 AC 244 ms
14,784 KB
testcase_20 AC 274 ms
15,804 KB
testcase_21 AC 294 ms
16,684 KB
testcase_22 AC 313 ms
17,448 KB
testcase_23 AC 330 ms
18,336 KB
testcase_24 AC 361 ms
19,340 KB
testcase_25 AC 380 ms
20,100 KB
testcase_26 AC 412 ms
21,112 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 3 ms
5,376 KB
testcase_30 AC 126 ms
9,956 KB
testcase_31 AC 119 ms
9,960 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
using namespace std;

template<typename Abel> struct BIT {
    int sz;
    vector<Abel> dat;
    BIT(int n, Abel UNITY_SUM = 0) : sz(n + 1), dat(sz, UNITY_SUM) { }  // [1, n]
    inline void add(int i, Abel x) {
        while (i < dat.size()) { dat[i] += x; i += i & -i; }
    }
    inline Abel sum(int i) {    // [1, i]
        Abel res = dat[0];
        while (i > 0) { res += dat[i]; i -= i & -i; }
        return res;
    }
    inline Abel sum(int a, int b) { return sum(b - 1) - sum(a - 1); }   // [a, b)
    int lower_bound(long long w) {
        if (w <= 0) return 0;
        int idx = 0, r = 1; while (r < sz) r <<= 1;
        for (int k = r >> 1; k > 0; k >>= 1) {
            if (idx + k < sz && dat[idx + k] < w) w -= dat[idx += k];
        }
        return idx + 1;
    }
    Abel operator[](int i) { return sum(i, i + 1); }
    friend ostream& operator<<(ostream& os, BIT<Abel> &bit) {
        for (int i = 1; i < bit.sz; i++) os << bit[i] << " ";
        return os;
    }
};

template<typename T> struct Compress1D {
    map<T, int> zip; vector<T> unzip; int sz;
    Compress1D(vector<T> a) {
        sort(begin(a), end(a)); a.erase(unique(begin(a), end(a)), end(a));
        sz = a.size(); unzip.resize(sz);
        for (int i = 0; i < sz; i++) zip[a[i]] = i, unzip[i] = a[i];
    }
    int operator[](T x) { return zip[x]; }
    T inv(int idx) { return unzip[idx]; }
    int size() { return sz; }
};

int main() {
    int q, k; cin >> q >> k;
    vector<long long> vals, v(q);
    vector<int> t(q);
    for (int i = 0; i < q; i++) {
        cin >> t[i];
        if (t[i] == 1) cin >> v[i], vals.emplace_back(v[i]);
    }
    Compress1D<long long> idx(vals);
    BIT<int> bit(idx.size());
    for (int i = 0; i < q; i++) {
        if (t[i] == 1) {
            bit.add(idx[v[i]] + 1, 1);
        } else {
            int size = bit.sum(idx.size());
            if (k > size) {
                cout << -1 << endl;
            } else {
                int val = bit.lower_bound(k);
                cout << idx.inv(val - 1) << endl;
                bit.add(val, -1);
            }
        }
    }
    return 0;
}
0