結果

問題 No.649 ここでちょっとQK!
ユーザー face4face4
提出日時 2020-01-29 15:34:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 193 ms / 3,000 ms
コード長 3,434 bytes
コンパイル時間 836 ms
コンパイル使用メモリ 83,892 KB
実行使用メモリ 12,980 KB
最終ジャッジ日時 2023-10-14 05:44:10
合計ジャッジ時間 7,276 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 34 ms
4,352 KB
testcase_04 AC 95 ms
12,980 KB
testcase_05 AC 96 ms
12,888 KB
testcase_06 AC 97 ms
12,972 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 78 ms
7,276 KB
testcase_13 AC 74 ms
7,304 KB
testcase_14 AC 72 ms
7,260 KB
testcase_15 AC 77 ms
7,332 KB
testcase_16 AC 74 ms
7,948 KB
testcase_17 AC 88 ms
7,892 KB
testcase_18 AC 99 ms
8,340 KB
testcase_19 AC 112 ms
8,620 KB
testcase_20 AC 121 ms
9,188 KB
testcase_21 AC 133 ms
9,492 KB
testcase_22 AC 138 ms
9,792 KB
testcase_23 AC 148 ms
10,188 KB
testcase_24 AC 155 ms
10,764 KB
testcase_25 AC 175 ms
11,096 KB
testcase_26 AC 193 ms
11,308 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 2 ms
4,352 KB
testcase_29 AC 2 ms
4,352 KB
testcase_30 AC 66 ms
7,296 KB
testcase_31 AC 68 ms
7,500 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 1 ms
4,348 KB
testcase_34 AC 1 ms
4,352 KB
testcase_35 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<random>
#include<vector>
using namespace std;
typedef long long ll;

// 0-indexed
template<class T>
struct Treap{
    struct Node{
        T val;
        int pri;
        int cnt;
        Node* ch[2];
        Node(T v, int p) : val(v), pri(p){
            cnt = 1;
            ch[0] = ch[1] = NULL;
        }
    };

    Node* root;
    vector<T> v;    // 列を返すとき用
    T def;  // !!!

    Treap(T d) : def(d){
        root = NULL;
    }

    int count(Node* t)  { return t==NULL ? 0 : t->cnt;}

    Node* update(Node* t){
        t->cnt = count(t->ch[0]) + count(t->ch[1]) + 1;
        return t;
    }

    Node* rotate(Node* t, int b){
        Node* s = t->ch[b];
        t->ch[b] = s->ch[1-b];
        s->ch[1-b] = t;
        update(t);
        update(s);
        return s;
    }

    Node* insert(Node* t, int k, T val, int pri){
        if(t == NULL)   return new Node(val, pri);
        int leftCnt = count(t->ch[0]);
        int b = k > leftCnt;
        t->ch[b] = insert(t->ch[b], k-(b ? leftCnt+1 : 0), val, pri);
        update(t);
        if(t->ch[b]->pri > t->pri)  t = rotate(t, b);
        return t;
    }

    Node* erase(Node* t, int k, bool me=false){
        if(!me && count(t) <= k)   return t;    // out-of-range
        me |= count(t->ch[0])==k;
        if(me){
            if(t->ch[0]==NULL && t->ch[1]==NULL){
                delete t;
                return NULL;
            }
            int b = (t->ch[0]==NULL ? -1 : t->ch[0]->pri) < (t->ch[1]==NULL ? -1 : t->ch[1]->pri);
            t = rotate(t, b);
            t->ch[1-b] = erase(t->ch[1-b], k, me);
        }else{
            if(count(t->ch[0]) > k){
                t->ch[0] = erase(t->ch[0], k, me);
            }else{  // definitely count(t->ch[0]) < k
                t->ch[1] = erase(t->ch[1], k-count(t->ch[0])-1, me);
            }
        }
        return update(t);
    }

    T get(Node* t, int k){
        int leftCnt = count(t->ch[0]);
        if(leftCnt > k)     return get(t->ch[0], k);
        if(leftCnt == k)    return t->val;
        return get(t->ch[1], k-1-leftCnt);
    }

    Node* insert(int k, T val){
        return root = insert(root, k, val, rand());
    }

// !!! 雑拡張
    Node* insert_value(Node* t, T val, int pri){
        if(t == NULL)   return new Node(val, pri);
        int b = t->val < val;
        t->ch[b] = insert_value(t->ch[b], val, pri);
        update(t);
        if(t->pri < t->ch[b]->pri)  t = rotate(t, b);
        return t;
    }
    Node* insert_value(T val){
        return root = insert_value(root, val, rand());
    }
// !!!

    Node* erase(int k){
        return root = erase(root, k);
    }
    T get(int k){
        return k < count(root) ? get(root, k) : def;    // !!!
    }
    
    void flatten(Node* t){
        if(t==NULL) return;
        if(t->ch[0] != NULL)    flatten(t->ch[0]);
        v.push_back(t->val);
        if(t->ch[1] != NULL)    flatten(t->ch[1]);
    }

    vector<T> flatten(){
        v.clear();
        flatten(root);
        return v;
    }
};

int main(){
    Treap<ll> t(-1);
    int q, k;
    scanf("%d %d", &q, &k);
    k--;
    while(q--){
        int op;
        ll x;
        scanf("%d", &op);
        if(op == 1){
            scanf("%lld", &x);
            t.insert_value(x);
        }else if(op == 2){
            printf("%lld\n", t.get(k));
            if(t.get(k) != -1)  t.erase(k);
        }
    }
    return 0;
}
0