結果

問題 No.649 ここでちょっとQK!
ユーザー rokahikou1rokahikou1
提出日時 2021-02-26 16:47:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 272 ms / 3,000 ms
コード長 5,295 bytes
コンパイル時間 1,849 ms
コンパイル使用メモリ 175,220 KB
実行使用メモリ 11,320 KB
最終ジャッジ日時 2024-04-10 07:22:52
合計ジャッジ時間 5,812 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,812 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 272 ms
7,984 KB
testcase_04 AC 77 ms
11,320 KB
testcase_05 AC 80 ms
11,064 KB
testcase_06 AC 73 ms
11,064 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 3 ms
6,940 KB
testcase_12 AC 65 ms
7,948 KB
testcase_13 AC 67 ms
7,836 KB
testcase_14 AC 66 ms
7,964 KB
testcase_15 AC 64 ms
7,968 KB
testcase_16 AC 64 ms
7,952 KB
testcase_17 AC 69 ms
8,124 KB
testcase_18 AC 75 ms
8,536 KB
testcase_19 AC 80 ms
8,816 KB
testcase_20 AC 89 ms
9,100 KB
testcase_21 AC 96 ms
9,256 KB
testcase_22 AC 103 ms
9,672 KB
testcase_23 AC 110 ms
9,832 KB
testcase_24 AC 116 ms
10,240 KB
testcase_25 AC 123 ms
10,528 KB
testcase_26 AC 128 ms
10,676 KB
testcase_27 AC 3 ms
6,940 KB
testcase_28 AC 3 ms
6,944 KB
testcase_29 AC 4 ms
6,940 KB
testcase_30 AC 52 ms
7,712 KB
testcase_31 AC 50 ms
7,964 KB
testcase_32 AC 3 ms
6,944 KB
testcase_33 AC 3 ms
6,940 KB
testcase_34 AC 3 ms
6,940 KB
testcase_35 AC 3 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:166:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  166 |     for(auto [a, v] : query) {
      |              ^

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for(int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++)
#define ALL(v) (v).begin(), (v).end()
#define LLA(v) (v).rbegin(), (v).rend()
#define SZ(v) (v).size()
#define INT(...)                                                               \
    int __VA_ARGS__;                                                           \
    read(__VA_ARGS__)
#define LL(...)                                                                \
    ll __VA_ARGS__;                                                            \
    read(__VA_ARGS__)
#define DOUBLE(...)                                                            \
    double __VA_ARGS__;                                                        \
    read(__VA_ARGS__)
#define CHAR(...)                                                              \
    char __VA_ARGS__;                                                          \
    read(__VA_ARGS__)
#define STRING(...)                                                            \
    string __VA_ARGS__;                                                        \
    read(__VA_ARGS__)
#define VEC(type, name, size)                                                  \
    vector<type> name(size);                                                   \
    read(name)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;
template <typename T> struct edge {
    int to;
    T cost;
    edge(int t, T c) : to(t), cost(c) {}
};
template <typename T> using WGraph = vector<vector<edge<T>>>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;
template <class T> inline vector<T> make_vec(size_t a, T val) {
    return vector<T>(a, val);
}
template <class... Ts> inline auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec(ts...))>(a, make_vec(ts...));
}

void read() {}
template <class T> inline void read(T &a) { cin >> a; }
template <class T, class S> inline void read(pair<T, S> &p) {
    read(p.first), read(p.second);
}
template <class T> inline void read(vector<T> &v) {
    for(auto &a : v)
        read(a);
}
template <class Head, class... Tail>
inline void read(Head &head, Tail &...tail) {
    read(head), read(tail...);
}
template <class T> inline void chmax(T &a, T b) { (a < b ? a = b : a); }
template <class T> inline void chmin(T &a, T b) { (a > b ? a = b : a); }

/**
 * @brief 0-indexed (Internally 1-indexed) Fenwick Tree
 *
 * @tparam T Universal set.
 */
template <typename T> struct FenwickTree {
    int n;
    vector<T> dat;

    FenwickTree(int n) : n(++n), dat(n, 0) {}

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

    /**
     * @brief Sum values in [0,i]
     *
     * @param i Right end of interval. Note that closed-interval [0,i].
     * @return T Summation values in [0,i].
     */
    T sum(int i) {
        T res = 0;
        for(++i; i > 0; i -= i & -i) {
            res = res + dat[i];
        }
        return res;
    }

    /**
     * @brief Sum values in [l,r]
     *
     * @param l Left end of interval.
     * @param r Right end of interval.
     * @return T Summation values in [l,r]
     */
    T sum(int l, int r) { return sum(r) - sum(l - 1); }

    /**
     * @brief Sum all values.
     *
     * @return T Summation values.
     */
    T sumAll() { return sum(n - 1); }

    /**
     * @brief If regard as histogram of elements, get k-th element.
     *
     * @param k Get k-th element.
     * @return int Index to which k-th element belongs.
     */
    int get(T k) {
        ++k;
        int res = 0;
        int N = 1;
        while(N < n)
            N *= 2;
        for(int i = N / 2; i > 0; i /= 2) {
            if(res + i < n && dat[res + i] < k) {
                k = k - dat[res + i];
                res = res + i;
            }
        }
        return res + 1;
    }
};

/**
 * @brief 座標圧縮
 *
 * @tparam T データ型
 * @param X 圧縮する数列 副作用により圧縮後の値が入る
 * @return std::vector<T> ソートして重複を除去した元数列
 */
template <typename T> std::vector<T> compress(std::vector<T> &X) {
    std::vector<T> vals = X;
    std::sort(vals.begin(), vals.end());
    vals.erase(unique(vals.begin(), vals.end()), vals.end());
    for(int i = 0; i < X.size(); i++) {
        X[i] = lower_bound(vals.begin(), vals.end(), X[i]) - vals.begin();
    }
    return vals;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    INT(q, k);
    FenwickTree<ll> bit(200010);
    vector<pll> query(q);
    vector<ll> vals;
    rep(i, q) {
        INT(a);
        if(a == 1) {
            LL(v);
            query[i] = {a, v};
            vals.emplace_back(v);
        } else {
            query[i] = {a, 0};
        }
    }
    auto dict = compress(vals);
    int now = 0;
    for(auto [a, v] : query) {
        if(a == 1) {
            bit.add(vals[now++], 1);
        } else {
            if(bit.sumAll() < k) {
                cout << -1 << endl;
            } else {
                int val = bit.get(k - 1);
                cout << dict[val - 1] << endl;
                bit.add(val - 1, -1);
            }
        }
    }
}
0