結果

問題 No.649 ここでちょっとQK!
ユーザー yuto_okayama
提出日時 2019-01-20 11:55:25
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 422 ms / 3,000 ms
コード長 1,602 bytes
コンパイル時間 1,436 ms
コンパイル使用メモリ 173,112 KB
実行使用メモリ 21,128 KB
最終ジャッジ日時 2024-07-21 20:19:53
合計ジャッジ時間 9,602 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define repi(i,a,b) for(int i=int(a);i<(int)(b);i++)
#define reps(i,n) for(int i=0;i<=(int)(n);i++)
#define all(x) (x).begin(),(x).end()
#define foreach(u,v) for(auto (u) : (v))
#define pb push_back
#define mp make_pair
#define mt make_tuple

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;

const int inf = 1e9;
const ll linf = 1e18;
const ll mod = 1e9 + 7;
const double eps = 1e-9;

const int MAXN = 200010;
int bit[MAXN+1];

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

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

int search(int x)
{
  int l = 0, r = MAXN;
  while(r-l > 1){
    int m = (l+r)/2;
    if(sum(m) < x){
      l = m; 
    }else{
      r = m;
    }
  }
  return r;
}

int main()
{
  int q, k;
  cin >> q >> k;

  vl t(q), v;
  rep(i, q){
    cin >> t[i];
    if(t[i] == 1){
      ll vv;
      cin >> vv;
      v.pb(vv);
    }
  }

  vl V;
  foreach(vv, v){
    V.pb(vv);
  }

  sort(all(V));
  V.erase(unique(all(V)), V.end());

  map<ll, int> nv;
  foreach(vv, v){
    int nvv = lower_bound(all(V), vv) - V.begin();
    nv[vv] = nvv;
  }

  auto itv = v.begin();
  rep(i, q){
    if(t[i] == 1){
      add(nv[*itv]+1, 1);
      itv++; 
    }else{
      ll ans = search(k)-1;
      if(ans == MAXN-1) cout << -1 << endl;
      else cout << V[ans] << endl;
      add(ans+1, -1);
    }
  }

  return 0;
}
0