結果

問題 No.649 ここでちょっとQK!
ユーザー ShibuyapShibuyap
提出日時 2020-08-03 06:12:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 495 ms / 3,000 ms
コード長 1,726 bytes
コンパイル時間 2,327 ms
コンパイル使用メモリ 210,600 KB
実行使用メモリ 30,000 KB
最終ジャッジ日時 2023-09-29 13:19:55
合計ジャッジ時間 12,586 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 289 ms
5,628 KB
testcase_04 AC 318 ms
29,836 KB
testcase_05 AC 317 ms
30,000 KB
testcase_06 AC 170 ms
5,652 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 210 ms
15,644 KB
testcase_13 AC 211 ms
15,360 KB
testcase_14 AC 210 ms
15,396 KB
testcase_15 AC 208 ms
15,364 KB
testcase_16 AC 201 ms
15,380 KB
testcase_17 AC 233 ms
16,560 KB
testcase_18 AC 271 ms
17,840 KB
testcase_19 AC 285 ms
18,964 KB
testcase_20 AC 317 ms
20,184 KB
testcase_21 AC 348 ms
21,668 KB
testcase_22 AC 385 ms
22,604 KB
testcase_23 AC 399 ms
23,836 KB
testcase_24 AC 439 ms
25,044 KB
testcase_25 AC 450 ms
26,232 KB
testcase_26 AC 495 ms
27,568 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 146 ms
11,672 KB
testcase_31 AC 143 ms
11,768 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}

const int MAX_N = 500005;
int bit[MAX_N+1];
int nn = MAX_N; // 1-index

void adds(int a, int w){
    for(int x = a; x <= nn; x += x & -x) bit[x] += w;
}
int sums(int a){ // bit[1,a]の和を返す.
    int ret = 0;
    for(int x = a; x > 0; x -= x & -x) ret += bit[x];  
    return ret;
}

int main() {
    int Q,K;
    cin >> Q >> K;
    int a[Q];
    ll b[Q] = {};
    set<ll> se;
    rep(i,Q){
        cin >> a[i];
        if(a[i] == 1){
            cin >> b[i];
            se.insert(b[i]);
        } 
    }

    int m = se.size();
    map<ll,int> mp;
    int cnt = 0;
    ll val[m+1] = {};
    for(auto x : se){
        cnt++;
        mp[x] = cnt;
        val[cnt] = x;
    }

    int now = 0;
    rep(i,Q){
        if(a[i] == 1){
            adds(mp[b[i]], 1);
            now++;
        }else{
            if(now < K){
                cout << -1 << endl;
                continue;
            }
            if(sums(1) >= K){
                cout << val[1] << endl;
                adds(1,-1);
                now--;
                continue;
            }
            int l = 1;
            int r = 200005;
            while(l+1<r){
                int mid = (l+r)/2;
                if(sums(mid)>=K){
                    r = mid;
                }else{
                    l = mid;
                }
            }
            cout << val[r] << endl;
            adds(r,-1);
            now--;
        }
    }

    return 0;
}
 
 

0