結果

問題 No.649 ここでちょっとQK!
ユーザー TiramisterTiramister
提出日時 2018-10-18 19:27:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 365 ms / 3,000 ms
コード長 1,912 bytes
コンパイル時間 967 ms
コンパイル使用メモリ 88,884 KB
実行使用メモリ 19,928 KB
最終ジャッジ日時 2024-04-24 14:42:35
合計ジャッジ時間 7,617 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 285 ms
5,376 KB
testcase_04 AC 276 ms
19,928 KB
testcase_05 AC 273 ms
19,672 KB
testcase_06 AC 183 ms
6,616 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 174 ms
10,644 KB
testcase_13 AC 175 ms
10,640 KB
testcase_14 AC 175 ms
10,644 KB
testcase_15 AC 176 ms
10,768 KB
testcase_16 AC 173 ms
10,764 KB
testcase_17 AC 196 ms
11,408 KB
testcase_18 AC 215 ms
12,304 KB
testcase_19 AC 235 ms
12,944 KB
testcase_20 AC 247 ms
13,712 KB
testcase_21 AC 264 ms
14,412 KB
testcase_22 AC 281 ms
15,044 KB
testcase_23 AC 304 ms
15,944 KB
testcase_24 AC 324 ms
16,716 KB
testcase_25 AC 335 ms
17,356 KB
testcase_26 AC 365 ms
18,380 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 3 ms
5,376 KB
testcase_29 AC 3 ms
5,376 KB
testcase_30 AC 116 ms
8,592 KB
testcase_31 AC 113 ms
8,720 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 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const int MAX_V = 200100;

class BIT {
public:
    // 要素数N、値vで初期化
    explicit BIT(int N, int v) : V_NUM(N) {
        fill(data, data + V_NUM + 1, v);
    }

    // [1, i]の総和を求める
    int query(int i) {
        ll ret = 0;
        while (i > 0) {
            ret += data[i];
            i -= (i & -i);
        }
        return ret;
    }

    // data[i]にv可算
    void update(int i, int v) {
        while (i < V_NUM) {
            data[i] += v;
            i += (i & -i);
        }
    }

    int V_NUM;
    int data[MAX_V];
};

int main() {
    int Q, K;
    cin >> Q >> K;

    ll val[Q];
    vector<ll> v = {-1};
    // BITが1-indexedなので、その調整用

    for (int i = 0; i < Q; ++i) {
        int q;
        cin >> q;
        if (q == 1) {
            cin >> val[i];
            v.push_back(val[i]);
        } else {
            val[i] = -1;
        }
    }

    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());

    map<ll, int> dic;
    int idx = 0;
    for (ll c : v) {
        dic[c] = idx;
        ++idx;
    }
    // [1, idx)まで値が入っている

    BIT bit(idx, 0);
    for (int i = 0; i < Q; ++i) {
        if (val[i] >= 0) {
            bit.update(dic[val[i]], 1);
        } else {
            // i <= ok <=> bit[i] <= K
            int ok = 0, ng = idx;
            while (ng - ok > 1) {
                int mid = (ok + ng) / 2;
                if (bit.query(mid) < K) {
                    ok = mid;
                } else {
                    ng = mid;
                }
            }

            if (ng == idx) {
                cout << -1 << endl;
            } else {
                cout << v[ng] << endl;
                bit.update(ng, -1);
            }
        }
    }

    return 0;
}
0