結果

問題 No.649 ここでちょっとQK!
ユーザー pppppp
提出日時 2018-02-15 01:03:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 5,330 bytes
コンパイル時間 2,818 ms
コンパイル使用メモリ 102,732 KB
実行使用メモリ 813,464 KB
最終ジャッジ日時 2023-08-25 00:15:07
合計ジャッジ時間 12,714 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 WA -
testcase_05 RE -
testcase_06 AC 1,317 ms
12,724 KB
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 MLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <fstream>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sstream>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <random>

using namespace std;

#define INT_MAX_VALUE 2147483647
#define LONG_LONG_MAX_VALUE 9223372036854775807
#define ll long long

template <class T>
T mymax(T a,T b){
    if(a>=b) return a;
    return b;
}
template <class T>
T mymin(T a,T b){
    if(a<=b) return a;
    return b;
}

ll gcd(ll a, ll b){
    if(a<b){
        swap(a,b);
    }
    while(b){
        ll r = a%b;
        a=b;
        b=r;
    }
    return a;
}

ll lcm(ll a, ll b){
    return (a*b)/gcd(a,b);
}

struct XX{
    ll a;
    int b;
    int j;
    int i;
};

class xxGreater {
public:
    bool operator()(const XX& riLeft, const XX& riRight) const {
        //第2条件
        if((riLeft.a) == (riRight.a)){
            return riLeft.i < riRight.i;//<:昇順(小さいものから順番)、>:降順(大きいものから順番)
            //プライオリティキューの場合は > で、top()すると値の小さいものがとれる
        }
        //第1条件
        return (riLeft.a) < (riRight.a);
    }
};
class xxrevGreater {
public:
    bool operator()(const XX& riLeft, const XX& riRight) const {
        //第2条件
        if((riLeft.i) == (riRight.i)){
            return riLeft.i < riRight.i;//<:昇順(小さいものから順番)、>:降順(大きいものから順番)
            //プライオリティキューの場合は > で、top()すると値の小さいものがとれる
        }
        //第1条件
        return (riLeft.i) < (riRight.i);
    }
};

//map<long long,long long> prime_f(long long n){
//    map<long long,long long>res;
//    for(int i=2;i*i<=n;i++){
//        while(n%i==0){
//            ++res[i];
//            n/=i;
//        }
//    }
//    if(n!=1)res[n]=1;
//    return res;
//}

//https://www.slideshare.net/iwiwi/2-12188757
//キー(val)の2分探索木
//優先度(pri)の2分ヒープ(各々のノードはそのノードの子よりも大きいか等しくなる)
struct node_t{
    ll val;//値
    node_t *ch[2];//左右ポインタ
    int pri;//優先度(ランダムな数値)
    int cnt;//部分木のサイズ
    ll sum;//部分木の値の和
    
    node_t(ll v,double p):val(v),pri(p),cnt(1),sum(1){
        ch[0]=ch[1]=NULL;
    }
};
int count(node_t *t){
    return !t ? 0:t->cnt;
}
ll sum(node_t *t){
    return !t ? 0:t->sum;
}
node_t *update(node_t *t){
    t->cnt=count(t->ch[0])+count(t->ch[1])+1;
    t->sum=sum(t->ch[0])+sum(t->ch[1])+t->val;
    return t;
}
node_t *rotate(node_t *t,int b){//b==0 or 1
    node_t *s=t->ch[1-b];//子(p)を保存
    t->ch[1-b]=s->ch[b]; //bを親(q)の左に付ける
    s->ch[b]=t;          //子(p)の右に親を付ける
    update(t);
    update(s);
    return s;            //子を親にする
}
//     q
//    / \
//   p   c
//  / \
// a   b
//これを下に回転
//     p
//    / \
//   a   q
//      / \
//     b   c

//tが根となっている木のk番目に値val,優先度priのノード挿入
node_t *insert(node_t *t,int k,ll val,int pri){
    if(!t){
        return new node_t(val,pri);
    }

    int b=(val > t->val);
    t->ch[b]=insert(t->ch[b],k,val,pri);
    update(t);
    if(t->pri > t->ch[b]->pri){
        t=rotate(t,1-b);
    }
    return t;
}
node_t *merge(node_t *l,node_t *r){
    if(!l || !r){
        return !l ? r:l;
    }
    if(l->pri > r->pri){
        l->ch[1]=merge(l->ch[1],r);
        return update(l);
    }else{
        r->ch[0]=merge(l,r->ch[0]);
        return update(r);
    }
}
pair<node_t*,node_t*> split(node_t *t,int k){//[0,k),[k,n)
    if(!t){
        node_t* ret=NULL;
        return make_pair(ret,ret);
    }
    if(k<=count(t->ch[0])){
        pair<node_t*,node_t*>s=split(t->ch[0],k);
        t->ch[0]=s.second;
        return make_pair(s.first,update(t));
    }else{
        pair<node_t*,node_t*>s=split(t->ch[1],k-count(t->ch[0])-1);
        t->ch[1]=s.first;
        return make_pair(update(t),s.second);
    }
}

int main(int argc, const char * argv[])
{
    //scanf("%s",S);
    //scanf("%d",&N);
    //scanf("%lld %lld",&target1,&target2);
    //sscanf(tmp.c_str(),"%dd%d%d",&time[i], &dice[i], &z[i]);
    //getline(cin, target);
    //cin >> x >> y;
    //テスト用
    //ifstream ifs( "1_06.txt" );
    //ifs >> a;
    //ここから
    
    //入力高速化
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int Q,K;
    cin >> Q >> K;
    node_t *t=NULL;
    for(int i=0;i<Q;i++){
        ll tmp;
        cin >> tmp;
        if(tmp==1){
            ll tmp2;
            cin >> tmp2;
            std::random_device rnd;
            int r=rnd();
            t=insert(t,0,tmp2,r);
        }else{
            if(t->cnt<K){
                cout << -1 << endl;
            }else{
                pair<node_t*,node_t*>p1=split(t,K);
                pair<node_t*,node_t*>p2=split(t,K-1);
                cout << p2.second->val << endl;
                t=merge(p2.first,p1.second);
            }
        }
    }

    
    
    //ここまで
    //cout << "ans" << endl;改行含む
    //printf("%.0f\n",ans);//小数点以下表示なし
    //printf("%.7f\n",p);
    
    return 0;
}

0