結果

問題 No.649 ここでちょっとQK!
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2018-02-10 01:06:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 351 ms / 3,000 ms
コード長 5,046 bytes
コンパイル時間 2,506 ms
コンパイル使用メモリ 186,424 KB
実行使用メモリ 13,056 KB
最終ジャッジ日時 2024-04-17 15:13:31
合計ジャッジ時間 8,186 ms
ジャッジサーバーID
(参考情報)
judge1 / 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 19 ms
5,376 KB
testcase_04 AC 106 ms
13,056 KB
testcase_05 AC 108 ms
13,056 KB
testcase_06 AC 108 ms
13,056 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 145 ms
8,064 KB
testcase_13 AC 141 ms
7,808 KB
testcase_14 AC 140 ms
7,936 KB
testcase_15 AC 149 ms
7,936 KB
testcase_16 AC 138 ms
8,064 KB
testcase_17 AC 163 ms
8,320 KB
testcase_18 AC 178 ms
8,704 KB
testcase_19 AC 199 ms
9,216 KB
testcase_20 AC 218 ms
9,600 KB
testcase_21 AC 247 ms
10,112 KB
testcase_22 AC 262 ms
10,368 KB
testcase_23 AC 293 ms
10,880 KB
testcase_24 AC 315 ms
11,520 KB
testcase_25 AC 348 ms
11,648 KB
testcase_26 AC 351 ms
12,288 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 3 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 135 ms
8,064 KB
testcase_31 AC 130 ms
7,808 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 #

#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; }
//---------------------------------------------------------------------------------------------------
struct dice {
    mt19937 mt; dice() { random_device rd; mt = mt19937(rd()); }
    int operator()(int x) { return this->operator()(0, x - 1); }
    int operator()(int x, int y) { uniform_int_distribution<int> dist(x, y); return dist(mt); }
} dc;
using V = ll;
struct RBST {
    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() { }
    };


    RBST() :root(NULL) {}

    Node *root;
    inline int getsz(Node *t) { return t ? t->sz : 0; }
    int getsz() { return getsz(root); }
    inline void push(Node *t) {
        if (t == NULL)return;
        t->push();
    }
    inline Node *update(Node *t) {
        if (!t)return NULL;
        t->sz = getsz(t->lch) + getsz(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 (dc(getsz(a) + getsz(b))<getsz(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 <= getsz(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 - getsz(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) + getsz(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;
RBST 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.getsz();
            if (n < K) printf("-1\n");
            else {
                ll ans = rbst.getAt(K - 1);
                rbst.erase(ans);
                printf("%lld\n", ans);
            }
        }
    }
}
0