結果

問題 No.649 ここでちょっとQK!
ユーザー るこーそーるこーそー
提出日時 2024-11-13 21:32:26
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 5,747 bytes
コンパイル時間 3,528 ms
コンパイル使用メモリ 251,132 KB
実行使用メモリ 16,768 KB
最終ジャッジ日時 2024-11-13 21:32:51
合計ジャッジ時間 23,961 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 298 ms
6,820 KB
testcase_04 AC 799 ms
16,128 KB
testcase_05 AC 783 ms
16,768 KB
testcase_06 AC 800 ms
16,000 KB
testcase_07 AC 2 ms
6,816 KB
testcase_08 AC 2 ms
6,820 KB
testcase_09 AC 2 ms
6,816 KB
testcase_10 AC 2 ms
6,820 KB
testcase_11 AC 2 ms
6,816 KB
testcase_12 AC 524 ms
8,576 KB
testcase_13 AC 551 ms
8,320 KB
testcase_14 AC 511 ms
8,448 KB
testcase_15 AC 580 ms
8,576 KB
testcase_16 AC 554 ms
8,960 KB
testcase_17 AC 656 ms
9,344 KB
testcase_18 AC 716 ms
9,856 KB
testcase_19 AC 785 ms
10,368 KB
testcase_20 AC 891 ms
11,008 KB
testcase_21 AC 951 ms
11,392 KB
testcase_22 AC 1,050 ms
12,032 KB
testcase_23 AC 1,134 ms
12,288 KB
testcase_24 AC 1,246 ms
12,800 KB
testcase_25 AC 1,324 ms
13,440 KB
testcase_26 AC 1,443 ms
13,824 KB
testcase_27 AC 4 ms
6,816 KB
testcase_28 RE -
testcase_29 AC 4 ms
6,816 KB
testcase_30 AC 511 ms
8,448 KB
testcase_31 AC 510 ms
8,832 KB
testcase_32 AC 2 ms
6,820 KB
testcase_33 AC 2 ms
6,816 KB
testcase_34 AC 3 ms
6,816 KB
testcase_35 AC 2 ms
6,816 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In instantiation of 'S SplayTree<S, op, e, F, mapping, composition, id>::get(int) [with S = long long int; S (* op)(S, S) = op; S (* e)() = e; F = long long int; S (* mapping)(F, S) = mapping; F (* composition)(F, F) = composition; F (* id)() = id]':
main.cpp:202:23:   required from here
main.cpp:145:26: warning: converting to non-pointer type 'long long int' from NULL [-Wconversion-null]
  145 |         if (!root)return NULL;
      |                          ^~~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<typename S, S (*op)(S, S), S (*e)(), typename F, S (*mapping)(F, S), F (*composition)(F, F), F (*id)()>
class SplayTree{
private:
    struct node{
        S v, prod;
        F lazy;
        int cnt, rev;
        node *l, *r;
        node(S v): v(v), prod(v), lazy(id()), cnt(1), rev(0){
            l = r = nullptr;
        }
    };

    node *root=nullptr;

    int count(node *x){return !x ? 0:x->cnt;}
    S sum(node *x){return !x ? e():x->prod;}

    void push(node *x){
        if (!x)return;
        if (x->lazy!=id()){
            x->v=mapping(x->lazy,x->v);
            x->prod=mapping(x->lazy,x->prod);
            if (x->l)x->l->lazy=composition(x->lazy,x->l->lazy);
            if (x->r)x->r->lazy=composition(x->lazy,x->r->lazy);
            x->lazy=id();
        }
        if (x->rev){
            swap(x->l,x->r);
            if (x->l)x->l->rev^=1;
            if (x->r)x->r->rev^=1;
            x->rev=0;
        }
    }

    void all_push(node *x){
        push(x);
        if (x)push(x->l),push(x->r);
    }

    node *update(node *x){
        if (!x)return x;
        x->cnt=count(x->l)+count(x->r)+1;
        x->prod=op(op(sum(x->l),x->v),sum(x->r));
        return x;
    }
    
    node *rotate(node *x,bool right){
        node *y= right ? x->l : x->r;
        all_push(x);all_push(y);
        if (!y)return x;
        if (right){
            x->l=y->r;
            y->r=x;
        }else{
            x->r=y->l;
            y->l=x;
        }
        update(x);update(y);
        return y;
    }

    node *splay(node *x,int k){
        if (!x)return nullptr;
        all_push(x);
        if (k<count(x->l)){
            if (x->l==nullptr)return x;
            if (k<count(x->l->l)){
                x->l->l=splay(x->l->l,k);
                x=rotate(x,true);
            }else if (count(x->l->l)<k){
                x->l->r=splay(x->l->r,k-count(x->l->l)-1);
                if (x->l->r)x->l=rotate(x->l,false);
            }
            return x->l ? rotate(x,true) : update(x);
        }else if (count(x->l)<k){
            if (x->r==nullptr)return x;
            k=k-count(x->l)-1;
            if (k<count(x->r->l)){
                x->r->l=splay(x->r->l,k);
                if (x->r->l)x->r=rotate(x->r,true);
            }else if (count(x->r->l)<k){
                x->r->r=splay(x->r->r,k-count(x->r->l)-1);
                x=rotate(x,false);
            }
            return x->r ? rotate(x,false) : update(x);
        }
        return update(x);
    }

    node *merge(node *l,node *r) {
        if (!l || !r) return !l ? r : l;
        r=splay(r,0);
        r->l=l;
        return update(r);
    }

    pair<node*, node*> split(node *x, int k) {
        assert(0<=k && k<=size());
        if (!x) return {nullptr, nullptr};
        if (k==size())return {x,nullptr};
        x=splay(x, k);
        node* l=x->l;
        x->l=nullptr;
        return {l,x};
    }

    tuple<node*, node*, node*> split3(node *x,int l,int r){//[0,l),[l,r),[r,size)
        auto [left_mid,right]=split(x,r);
        auto [left,mid]=split(left_mid,l);
        return {left,mid,right};
    }

public:
    int size(){return (!root ? 0 : root->cnt);}
    
    void insert(int k,S v) {
        assert(0<=k && k<=size());
        auto [l,r]=split(root,k);
        root=merge(merge(l,new node(v)),r);
    }
    void erase(int k) {
        assert(0<=k && k<size());
        root=splay(root,k);
        auto [left_mid,right]=split(root,k+1);
        auto [left,mid]=split(left_mid,k);
        delete mid;
        root=merge(left,right);
    }

    SplayTree merge(SplayTree other){
        return SplayTree(merge(root, other.root));
    }

    pair<SplayTree,SplayTree> split(int k){
        auto [left,right]=split(root,k);
        return {SplayTree(left),SplayTree(right)};
    }

    S get(int k){
        assert(0<=k && k<size());
        if (!root)return NULL;
        root=splay(root,k);
        all_push(root);
        update(root);
        return root->v;
    }

    void set(int k,S v){
        assert(0<=k && k<size());
        root=splay(root,k);
        root->v=v;
    }

    S prod(int l,int r) {
        assert(0<=l && r<=size() && l<=r);
        root=splay(root,l);
        auto [left,mid,right]=split3(root,l,r);
        all_push(mid);
        S res=sum(update(mid));
        root=merge(merge(left,mid),right);
        return res;
    }

    void apply(int l,int r,F f){
        assert(0<=l && r<=size() && l<=r);
        root=splay(root,l);
        auto [left,mid,right]=split3(root,l,r);
        if (mid)mid->lazy=composition(f,mid->lazy);
        root=merge(merge(left,mid),right);
    }

    void reverse(int l,int r){
        assert(0<=l && r<=size() && l<=r);
        root=splay(root,l);
        auto [left,mid,right]=split3(root,l,r);
        if (mid)mid->rev=1;
        root=merge(merge(left,mid),right);
    }
};

using S=long long;
S op(S a,S b){return min(a,b);}
S e(){return 0;}
using F=long long;
S mapping(F f,S x){return f+x;}
F composition(F f,F g){return f+g;}
F id(){return 0;}

void solve() {
    SplayTree<S,op,e,F,mapping,composition,id> st;
    int q,k;
    cin >> q >> k;

    auto lb=[&](S x){
        int ng=-1,ok=st.size();
        while (ok-ng>1){
            int mid=(ok+ng)/2;
            if (st.get(mid)<x)ng=mid;
            else ok=mid;
        }
        return ok;
    };

    while (q--){
        long long t,x;
        cin >> t;
        if (t==1){
            cin >> x;
            st.insert(lb(x),x);
        }else {
            if (k<=st.size()){
                cout << st.get(k-1) << endl;
                st.erase(k-1);
            }else{
                cout << -1 << endl;
            }
        }
    }
}

int main() {
    solve();
}
0