結果

問題 No.649 ここでちょっとQK!
ユーザー finefine
提出日時 2018-02-09 22:41:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 280 ms / 3,000 ms
コード長 1,786 bytes
コンパイル時間 1,861 ms
コンパイル使用メモリ 177,976 KB
実行使用メモリ 19,584 KB
最終ジャッジ日時 2024-04-17 05:12:37
合計ジャッジ時間 7,004 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 274 ms
5,376 KB
testcase_04 AC 159 ms
19,584 KB
testcase_05 AC 159 ms
19,456 KB
testcase_06 AC 66 ms
6,916 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 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 132 ms
10,740 KB
testcase_13 AC 126 ms
10,740 KB
testcase_14 AC 131 ms
10,740 KB
testcase_15 AC 124 ms
10,612 KB
testcase_16 AC 120 ms
10,736 KB
testcase_17 AC 142 ms
11,456 KB
testcase_18 AC 155 ms
12,052 KB
testcase_19 AC 170 ms
12,896 KB
testcase_20 AC 184 ms
13,744 KB
testcase_21 AC 197 ms
14,456 KB
testcase_22 AC 215 ms
15,176 KB
testcase_23 AC 237 ms
15,768 KB
testcase_24 AC 250 ms
16,612 KB
testcase_25 AC 269 ms
17,460 KB
testcase_26 AC 280 ms
18,176 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 94 ms
8,696 KB
testcase_31 AC 92 ms
8,688 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 <bits/stdc++.h>

using namespace std;

using ll = long long;

//1-indexであることに注意
template <typename T>
struct BIT {
    int n;
    vector<T> data;

    BIT(int n) : n(n), data(n + 1, 0) {}

    T sum(int i) {
        T s = 0;
        while (i > 0) {
            s += data[i];
            i -= i & -i;
        }
        return s;
    }

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

//引数unzipには圧縮前のvectorを入れる
int compress(vector<ll>& unzip, map<ll, int>& zip) {
    sort(unzip.begin(), unzip.end());
    unzip.erase(unique(unzip.begin(), unzip.end()), unzip.end());
    for (int i = 0; i < unzip.size(); i++) {
        zip[unzip[i]] = i;
    }
    return unzip.size();
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int Q, K;
    cin >> Q >> K;
    vector<ll> q(Q);
    vector<ll> v;
    for (int i = 0; i < Q; i++) {
        cin >> q[i];
        if (q[i] == 1) {
            ll x;
            cin >> x;
            v.push_back(x);
            q[i] = x;
        } else {
            q[i] = -1;
        }
    }

    map<ll, int> zip;
    compress(v, zip);
    BIT<int> b(v.size());
    for (int i = 0; i < Q; i++) {
        if (q[i] == -1) {
            if (b.sum(v.size()) < K) {
                cout << -1 << endl;
            } else {
                int ng = 0, ok = v.size();
                while (ok - ng > 1) {
                    int mid = (ok + ng) / 2;
                    if (b.sum(mid) >= K) ok = mid;
                    else ng = mid;
                }
                cout << v[ok - 1] << endl;
                b.add(ok, -1);
            }
        } else {
            b.add(zip[q[i]] + 1, 1);
        }
    }
    return 0;
}
0