結果

問題 No.649 ここでちょっとQK!
ユーザー みずくらげみずくらげ
提出日時 2019-07-23 12:05:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 477 ms / 3,000 ms
コード長 3,852 bytes
コンパイル時間 1,191 ms
コンパイル使用メモリ 109,920 KB
実行使用メモリ 12,784 KB
最終ジャッジ日時 2023-09-07 11:07:33
合計ジャッジ時間 9,627 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 289 ms
4,380 KB
testcase_04 AC 207 ms
12,784 KB
testcase_05 AC 208 ms
12,708 KB
testcase_06 AC 213 ms
12,736 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 209 ms
7,568 KB
testcase_13 AC 206 ms
7,744 KB
testcase_14 AC 202 ms
7,544 KB
testcase_15 AC 219 ms
7,616 KB
testcase_16 AC 209 ms
7,620 KB
testcase_17 AC 239 ms
8,032 KB
testcase_18 AC 264 ms
8,464 KB
testcase_19 AC 296 ms
8,908 KB
testcase_20 AC 319 ms
9,268 KB
testcase_21 AC 344 ms
9,760 KB
testcase_22 AC 374 ms
10,092 KB
testcase_23 AC 392 ms
10,516 KB
testcase_24 AC 421 ms
11,032 KB
testcase_25 AC 449 ms
11,440 KB
testcase_26 AC 477 ms
11,788 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 3 ms
4,380 KB
testcase_30 AC 176 ms
7,556 KB
testcase_31 AC 176 ms
7,632 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <cassert>
#include <random>

using namespace std;

typedef long long VAL;


struct Tree {
    unsigned int randInt() {
        static unsigned int tx = 123456789, ty=362436069, tz=521288629, tw=88675123;
        unsigned int tt = (tx^(tx<<11));
        tx = ty; ty = tz; tz = tw;
        return ( tw=(tw^(tw>>19))^(tt^(tt>>8)) );
    }
    

    struct node_t{
        VAL val;
        node_t* lch;
        node_t* rch;
        int cnt;
        VAL sum;

        node_t() : val(0), cnt(1), sum(0) {lch = rch = NULL;}
        node_t(VAL v) : val(v), cnt(1), sum(v) {lch = rch = NULL;}
    };


    node_t* root;
    Tree() : root(NULL) { }
    Tree(node_t* t) : root(t) { }

    int cnt(node_t* t) {return !t ?0 :t->cnt;}
    int cnt() {return this->cnt(this->root);}

    VAL sum(node_t* t) {return !t ?0 :t->sum;}
    VAL sum() {return this->sum(this->root);}

    node_t* update(node_t* t) {
        t->cnt = cnt(t->lch) + cnt(t->rch) + 1;
        t->sum = sum(t->lch) + sum(t->rch) + t->val;
        return t;
    }

    int lowerBound(node_t* t, VAL val) {
        if (!t) return 0;
        if (val <= t->val) return lowerBound(t->lch, val);
        else return cnt(t->lch) + lowerBound(t->rch, val) + 1;
    }
    int lowerBound(VAL val) {return this->lowerBound(this->root, val);}

    int upperBound(node_t* t, VAL val) {
        return lowerBound(t, val+1);
    }
    int upperBound(VAL val) {return this->lowerBound(this->root, val+1);}

    VAL get(node_t* t, int k) {
        if (!t) return -1;
        if (k == cnt(t->lch)) return t->val;
        if (k < cnt(t->lch)) return get(t->lch, k);
        else return get(t->rch, k - cnt(t->lch) - 1);
    }
    VAL get(int k) {return get(this->root, k);}


    node_t* merge(node_t* l, node_t* r) {
        if (!l || !r) return !l ?r :l;

        if (randInt() % (l->cnt + r->cnt) < l->cnt) {
            l->rch = merge(l->rch, r);
            return update(l);
        } else {
            r->lch = merge(l, r->lch);
            return update(r);
        }
    }
    void merge(Tree add) {this->root = this->merge(this->root, add.root);}

    pair<node_t*, node_t*> split(node_t* t, int k) { // [0, k), [k, n)
        if (!t) return make_pair(t, t);

        if (k <= cnt(t->lch)) {
            pair<node_t*, node_t*> s = split(t->lch, k);
            t->lch = s.second;
            return make_pair(s.first, update(t));
        } else {
            pair<node_t*, node_t*> s = split(t->rch, k - cnt(t->lch) - 1);
            t->rch = s.first;
            return make_pair(update(t), s.second);
        }
    }
    Tree split(int k) {
        pair<node_t*, node_t*> s = split(this->root, k);
        this->root = s.first;
        return Tree(s.second);
    }

    void insert(VAL val) {
        pair<node_t*, node_t*> s = this->split(this->root, this->lowerBound(val));
        this->root = this->merge(this->merge(s.first, new node_t(val)), s.second);
    }

    void erase(VAL val) {
        if (!(this->upperBound(val) - this->lowerBound(val))) return;
        pair<node_t*, node_t*> s = this->split(this->root, this->lowerBound(val));
        this->root = this->merge(s.first, this->split(s.second, 1).second);
    }
};



int main() {
    int q, k;
    cin >> q >> k;
    Tree tree;
    for (int i = 0; i < q; i++) {
        int t;
        cin >> t;
        if (t == 1) {
            long long val;
            cin >> val;
            tree.insert(val);
        } else {
            if (tree.cnt() < k) {
                cout << -1 << endl;
                continue;
            }
            long long val = tree.get(k-1);
            cout << val << endl;
            tree.erase(val);
        }
    }
}
0