結果

問題 No.649 ここでちょっとQK!
ユーザー TiramisterTiramister
提出日時 2018-10-18 19:21:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 356 ms / 3,000 ms
コード長 4,274 bytes
コンパイル時間 1,509 ms
コンパイル使用メモリ 118,436 KB
実行使用メモリ 19,672 KB
最終ジャッジ日時 2024-04-24 14:35:12
合計ジャッジ時間 8,659 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,376 KB
testcase_04 AC 271 ms
19,672 KB
testcase_05 AC 268 ms
19,668 KB
testcase_06 AC 181 ms
6,612 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 172 ms
10,644 KB
testcase_13 AC 168 ms
10,640 KB
testcase_14 AC 161 ms
10,644 KB
testcase_15 AC 160 ms
10,768 KB
testcase_16 AC 168 ms
10,768 KB
testcase_17 AC 182 ms
11,408 KB
testcase_18 AC 195 ms
12,172 KB
testcase_19 AC 219 ms
12,944 KB
testcase_20 AC 239 ms
13,844 KB
testcase_21 AC 252 ms
14,536 KB
testcase_22 AC 268 ms
15,432 KB
testcase_23 AC 296 ms
16,076 KB
testcase_24 AC 314 ms
16,712 KB
testcase_25 AC 327 ms
17,352 KB
testcase_26 AC 356 ms
18,124 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 114 ms
8,588 KB
testcase_31 AC 110 ms
8,596 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 #

/* ---------- STL Libraries ---------- */

// IO library
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm library
#include <algorithm>
#include <cmath>
#include <numeric>
#include <random>

// container library
#include <array>
#include <bitset>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

/* ---------- Namespace ---------- */

using namespace std;

/* ---------- Type Abbreviation ---------- */

template <typename T>
using PQ = priority_queue<T>;
template <typename T>
using GPQ = priority_queue<T, vector<T>, greater<T>>;

using ll = long long;

#define fst first
#define snd second
#define mp make_pair
#define mt make_tuple

/* ---------- conversion ---------- */

#define INT(c) static_cast<int>(c)
#define CHAR(n) static_cast<char>(n)
#define LL(n) static_cast<ll>(n)
#define DOUBLE(n) static_cast<double>(n)

/* ---------- container ---------- */

#define ALL(v) (v).begin(), (v).end()
#define SIZE(v) (LL((v).size()))

#define FIND(v, k) (v).find(k) != (v).end()
#define VFIND(v, k) find(ALL(v), k) != (v).end()

#define gsort(b, e) sort(b, e, greater<decltype(*b)>())

/* ----------- debug ---------- */

template <class T>
ostream& operator<<(ostream& os, vector<T> v) {
    os << "[";
    for (auto vv : v)
        os << vv << ",";
    return os << "]";
}

template <class T>
ostream& operator<<(ostream& os, set<T> v) {
    os << "[";
    for (auto vv : v)
        os << vv << ",";
    return os << "]";
}

template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> p) {
    return os << "(" << p.fst << "," << p.snd << ")";
}

/* ---------- Constants ---------- */

// const ll MOD = 1e9 + 7;
// const int INF = 1 << 25;
// const ll INF = 1LL << 50;
// const double PI = acos(-1);
// const double EPS = 1e-10;
// mt19937 mert(LL(time(0)));

/* ---------- Short Functions ---------- */

template <typename T>
T sq(T a) {
    return a * a;
}

template <typename T>
T gcd(T a, T b) {
    if (a > b) return gcd(b, a);
    return a == 0 ? b : gcd(b % a, a);
}

template <typename T, typename U>
T mypow(T b, U n) {
    if (n == 0) return 1;
    if (n == 1) return b /* % MOD */;
    if (n % 2 == 0) {
        return mypow(b * b /* % MOD */, n / 2);
    } else {
        return mypow(b, n - 1) * b /* % MOD */;
    }
}

ll pcnt(ll b) {
    return __builtin_popcountll(b);
}

/* v-v-v-v-v-v-v-v-v Main Part v-v-v-v-v-v-v-v-v */

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