結果

問題 No.649 ここでちょっとQK!
ユーザー PachicobuePachicobue
提出日時 2018-02-11 19:10:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 271 ms / 3,000 ms
コード長 4,292 bytes
コンパイル時間 2,628 ms
コンパイル使用メモリ 218,568 KB
実行使用メモリ 24,224 KB
最終ジャッジ日時 2024-04-28 18:09:31
合計ジャッジ時間 9,147 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 271 ms
6,940 KB
testcase_04 AC 156 ms
24,216 KB
testcase_05 AC 153 ms
24,224 KB
testcase_06 AC 58 ms
9,632 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 108 ms
12,928 KB
testcase_13 AC 115 ms
12,928 KB
testcase_14 AC 107 ms
12,924 KB
testcase_15 AC 108 ms
12,928 KB
testcase_16 AC 113 ms
12,924 KB
testcase_17 AC 132 ms
13,720 KB
testcase_18 AC 138 ms
14,652 KB
testcase_19 AC 158 ms
15,320 KB
testcase_20 AC 169 ms
16,496 KB
testcase_21 AC 181 ms
18,060 KB
testcase_22 AC 193 ms
19,116 KB
testcase_23 AC 198 ms
19,908 KB
testcase_24 AC 231 ms
20,832 KB
testcase_25 AC 247 ms
21,628 KB
testcase_26 AC 251 ms
22,352 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 79 ms
10,492 KB
testcase_31 AC 78 ms
10,496 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 1 ms
6,944 KB
testcase_34 AC 2 ms
6,944 KB
testcase_35 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template <typename Base>
class BinaryIndexedTree
{
public:
    using T = typename Base::T;
    using AbelGroup = Base;

    BinaryIndexedTree(const int n) : data_num(n), size(1 << (__lg(2 * data_num - 1))), value(size + 1, AbelGroup::identity()) { assert(n > 0); }
    BinaryIndexedTree(const vector<T>& val) : data_num(val.size()), size(1 << (__lg(2 * data_num - 1))), value(size + 1, AbelGroup::identity())
    {
        for (int i = 1; i <= data_num; i++) {
            value[i] = val[i - 1];
        }
        for (int x = 1; x < size; x++) {
            value[x + (x & -x)] = op(value[x + (x & -x)], value[x]);
        }
    }

    T accumulate(const int a) const
    {
        assert(0 <= a and a < data_num);
        int ind = a + 1;
        T sum = AbelGroup::identity();
        while (ind > 0) {
            sum = op(sum, value[ind]);
            ind &= ind - 1;
        }
        return sum;
    }
    T accumulate(const int l, const int r) const  // [l,r)
    {
        assert(0 <= l and l < r and r <= data_num);
        return op(accumulate(r - 1), op.inv(l == 0 ? AbelGroup::identity() : accumulate(l - 1)));
    }

    void add(const int a, const T& val)
    {
        assert(0 <= a and a < data_num);
        int ind = a + 1;
        while (ind <= size) {
            value[ind] = op(value[ind], val);
            ind += ind & (-ind);
        }
    }

    void set(const int a, const T& val)
    {
        const int v = get(a);
        add(a, op(val, op.inv(v)));
    }

    T get(const int a) const
    {
        assert(0 <= a and a < data_num);
        return accumulate(a, a + 1);
    }

    int lowerBound(T w) const
    {
        if (w <= AbelGroup::identity()) {
            return 0;
        }
        int x = 0;
        for (int k = ((size == data_num) ? size : size / 2); k > 0; k /= 2) {
            if (x + k <= size and value[x + k] < w) {
                w = op(w, op.inv(value[x + k]));
                x += k;
            }
        }
        return x;
    }

    int upperBound(T w) const
    {
        if (w <= AbelGroup::identity()) {
            return 0;
        }
        int x = 0;
        for (int k = ((size == data_num) ? size : size / 2); k > 0; k /= 2) {
            if (x + k <= size and value[x + k] <= w) {
                w = op(w, op.inv(value[x + k]));
                x += k;
            }
        }
        return min(x, data_num);
    }

private:
    const int data_num;
    const int size;
    const AbelGroup op{};
    vector<T> value;
};

template <typename T>
ostream& operator<<(ostream& os, const BinaryIndexedTree<T>& bit)
{
    os << "[";
    for (int i = 0; i < bit.data_num; i++) {
        os << bit.get(i) << ",";
    }
    os << "]" << endl;

    return os;
}
struct Sum {
    using T = ll;
    T operator()(const T& a, const T& b) const
    {
        return a + b;
    }
    T inv(const T& a) const
    {
        return -a;
    }
    static constexpr T identity()
    {
        return 0;
    }
};


int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int Q, K;
    cin >> Q >> K;
    using P = pair<int, ll>;
    vector<P> query(Q);
    vector<ll> v;
    for (int q = 0; q < Q; q++) {
        cin >> query[q].first;
        if (query[q].first == 1) {
            cin >> query[q].second;
            v.push_back(query[q].second);
        }
    }
    auto zip = v;
    sort(zip.begin(), zip.end());
    zip.erase(unique(zip.begin(), zip.end()), zip.end());
    map<ll, int> mp;
    const int size = zip.size();
    if (size != 0) {
        for (int i = 0; i < zip.size(); i++) {
            mp[zip[i]] = i;
        }
        BinaryIndexedTree<Sum> bit(size);
        for (int q = 0; q < Q; q++) {
            if (query[q].first == 1) {
                const int t = mp[query[q].second];
                bit.add(t, 1);
            } else {
                if (bit.accumulate(size - 1) < K) {
                    cout << -1 << "\n";
                } else {
                    const int l = bit.lowerBound(K);
                    cout << zip[l] << "\n";
                    bit.add(l, -1);
                }
            }
        }
    } else {
        for (int q = 0; q < Q; q++) {
            cout << -1 << endl;
        }
    }
    return 0;
}
0