結果

問題 No.649 ここでちょっとQK!
ユーザー oxyshoweroxyshower
提出日時 2019-06-01 13:17:04
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 444 ms / 3,000 ms
コード長 1,813 bytes
コンパイル時間 3,162 ms
コンパイル使用メモリ 175,072 KB
実行使用メモリ 23,456 KB
最終ジャッジ日時 2023-10-17 22:44:34
合計ジャッジ時間 11,375 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 281 ms
6,364 KB
testcase_04 AC 302 ms
23,456 KB
testcase_05 AC 303 ms
23,456 KB
testcase_06 AC 186 ms
9,456 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 203 ms
12,588 KB
testcase_13 AC 201 ms
12,588 KB
testcase_14 AC 206 ms
12,588 KB
testcase_15 AC 202 ms
12,592 KB
testcase_16 AC 204 ms
12,592 KB
testcase_17 AC 228 ms
13,340 KB
testcase_18 AC 257 ms
14,364 KB
testcase_19 AC 273 ms
15,376 KB
testcase_20 AC 292 ms
16,136 KB
testcase_21 AC 318 ms
17,148 KB
testcase_22 AC 347 ms
18,172 KB
testcase_23 AC 371 ms
18,928 KB
testcase_24 AC 403 ms
19,944 KB
testcase_25 AC 426 ms
20,968 KB
testcase_26 AC 444 ms
21,716 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 3 ms
4,348 KB
testcase_30 AC 147 ms
10,212 KB
testcase_31 AC 146 ms
10,212 KB
testcase_32 AC 1 ms
4,348 KB
testcase_33 AC 1 ms
4,348 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#ifdef LOCAL_DEBUG
  #include "LOCAL_DEBUG.hpp"
#endif

template <class T>
struct BIT{
  int n;
  vector<T> bit; //[1,N]

  BIT(int _n){
    n = _n + 1;
    bit.resize(n,0);
  }

  T sum(int i){
    T sum = 0;
    while(i > 0){
      sum += bit[i];
      i -= i & -i;
    }
    return sum;
  }

  void add(int i,T x){
    while(i < n){
      bit[i] += x;
      i += i & -i;
    }
  }

  // 合計がw以上の最小の位置 O(log^2 n)
  int lower_bound(T x) {
    int left = -1, right = n;
    while(right - left > 1) {
      int mid = (left + right)/2;
      if(sum(mid) >= x) right = mid;
      else left = mid;
    }
    return right;
  }
  /*
  // a[0]+...+a[ret] >= x
  int lower_bound(T x){
    int ret = -1;
    int k = 1;
    while(2*k < n) k <<= 1;
    while(k > 0){
      if(ret+k < n && bit[ret+k] < x){
        x -= bit[ret+k];
        ret += k;
      }
      k >>= 1;
    }
    return ret+1;
  }
  */
};

template<class T>
vector<T> compress(vector<T> v){
  sort(v.begin(),v.end());
  v.erase(unique(v.begin(),v.end()),v.end());
  return v;
}

signed main(){

  int q,k; cin >> q >> k;
  vector<int> query(q),v(q),vs;
  for(int i = 0; i < q; i++){
    cin >> query[i];
    if(query[i] == 1) cin >> v[i],vs.push_back(v[i]);
  }
  vs = compress(vs);
  map<int,int> mp;
  for(int i = 0; i < vs.size(); i++){
    mp[vs[i]] = i+1;
  }

  BIT<int> bitval(vs.size());
  BIT<int> bitnum(vs.size());

  for(int i = 0; i < q; i++){
    if(query[i] == 1){
      bitval.add(mp[v[i]],v[i]);
      bitnum.add(mp[v[i]],1);
    }
    else {
      if(bitnum.sum(vs.size()) < k) cout << -1 << endl;
      else {
        int idx = bitnum.lower_bound(k);
        cout << vs[idx-1] << endl;
        bitnum.add(idx,-1);
      }
    }
  }

  return 0;
}
0