結果

問題 No.649 ここでちょっとQK!
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2018-02-10 01:10:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 274 ms / 3,000 ms
コード長 4,948 bytes
コンパイル時間 2,088 ms
コンパイル使用メモリ 179,672 KB
実行使用メモリ 13,184 KB
最終ジャッジ日時 2024-04-17 15:14:37
合計ジャッジ時間 7,512 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 18 ms
5,376 KB
testcase_04 AC 93 ms
13,184 KB
testcase_05 AC 96 ms
13,056 KB
testcase_06 AC 92 ms
12,928 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 126 ms
8,064 KB
testcase_13 AC 121 ms
8,064 KB
testcase_14 AC 119 ms
7,808 KB
testcase_15 AC 129 ms
7,936 KB
testcase_16 AC 115 ms
7,936 KB
testcase_17 AC 132 ms
8,448 KB
testcase_18 AC 151 ms
8,832 KB
testcase_19 AC 168 ms
9,216 KB
testcase_20 AC 183 ms
9,728 KB
testcase_21 AC 203 ms
10,112 KB
testcase_22 AC 220 ms
10,624 KB
testcase_23 AC 239 ms
11,008 KB
testcase_24 AC 251 ms
11,392 KB
testcase_25 AC 264 ms
11,776 KB
testcase_26 AC 274 ms
12,160 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 114 ms
7,808 KB
testcase_31 AC 105 ms
7,936 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
template<typename V> struct SuperMultiSet {
    int getrand(int x) { static uint32_t y = time(0); y ^= (y << 13); y ^= (y >> 17); y ^= (y << 5); return y % x; }
    struct Node {
        V val;
        Node *lch, *rch;
        int sz;
        Node(V a) :val(a), lch(0), rch(0), sz(1) { init(); }

        void init() { }
        void push() { }
        void update() { }
    };


    SuperMultiSet() :root(NULL) {}

    Node *root;
    inline int size(Node *t) { return t ? t->sz : 0; }
    int size() { return size(root); }
    inline void push(Node *t) {
        if (t == NULL)return;
        t->push();
    }
    inline Node *update(Node *t) {
        if (!t)return NULL;
        t->sz = size(t->lch) + size(t->rch) + 1;
        t->update();
        return t;
    }

    Node *merge(Node *a, Node *b) {
        push(a); push(b);
        if (!a)return b;
        if (!b)return a;

        if (getrand(size(a) + size(b))<size(a)) {
            a->rch = merge(a->rch, b);
            return update(a);
        }
        else {
            b->lch = merge(a, b->lch);
            return update(b);
        }
    }

    template<class... T> Node *merge(Node *a, T... y) { return merge(a, merge(y...)); }

    pair<Node *, Node *>split(Node *t, int k) {//[0,k) [k,N)
        push(t);
        if (!t)return make_pair(t, t);
        if (k <= size(t->lch)) {
            pair<Node *, Node *>s = split(t->lch, k);
            t->lch = s.second;
            return make_pair(s.first, update(t));
        }
        else {
            pair<Node *, Node *>s = split(t->rch, k - size(t->lch) - 1);
            t->rch = s.first;
            return make_pair(update(t), s.second);
        }
    }

    int lower_bound(V x) { return lower_bound(root, x); }
    int lower_bound(Node *t, V x) {
        push(t);
        if (!t)return 0;
        if (t->val >= x)return lower_bound(t->lch, x);
        return lower_bound(t->rch, x) + size(t->lch) + 1;
    }

    // [l,r]番目でa,b,cに分ける(終わったらちゃんとマージすること)
    // rootは0になってる
    tuple<Node*, Node*, Node*> split3(int l, int r) {
        auto p = split(root, l);

        auto a = p.first;

        auto q = split(p.second, r - l + 1);

        auto b = q.first;
        auto c = q.second;
        root = 0;

        return make_tuple(a, b, c);
    }

    void dump() {
        dump(root);
        cout << endl;
    }
    void dump(Node *t) {
        if (t == NULL)return;
        push(t);
        dump(t->lch);
        cout << t->val << " ";
        dump(t->rch);
    }

    void insert(V x) {
        int k = lower_bound(x);

        Node *a, *b;
        tie(a, b) = split(root, k);

        root = merge(a, new Node(x), b);
    }

    void erase(V x) {
        int k = lower_bound(x);

        Node *a, *b;
        tie(a, b) = split(root, k);

        Node *ba, *bb;
        tie(ba, bb) = split(b, 1);

        root = merge(a, bb);
    }

    V getAt(int i) {
        auto p = split(root, i);
        auto a = p.first;
        auto q = split(p.second, 1);
        Node* b = q.first;
        auto c = q.second;

        root = merge(merge(a, b), c);

        return b->val;
    }
};
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/





int Q, K;
SuperMultiSet<ll> rbst;
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> Q >> K;
    rep(q, 0, Q) {
        int t; cin >> t;
        if (t == 1) {
            ll v; cin >> v;
            rbst.insert(v);
        }
        else {
            int n = rbst.size();
            if (n < K) printf("-1\n");
            else {
                ll ans = rbst.getAt(K - 1);
                rbst.erase(ans);
                printf("%lld\n", ans);
            }
        }
    }
}
0