結果

問題 No.649 ここでちょっとQK!
ユーザー siman
提出日時 2022-03-18 01:58:47
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
AC  
実行時間 523 ms / 3,000 ms
コード長 1,848 bytes
コンパイル時間 3,477 ms
コンパイル使用メモリ 149,876 KB
実行使用メモリ 44,588 KB
最終ジャッジ日時 2024-10-02 01:15:12
合計ジャッジ時間 13,418 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

class BinaryIndexTree {
  public:
    vector <ll> bit;
    int N;

    BinaryIndexTree(int n) {
      N = n;

      for (int i = 0; i <= N; ++i) {
        bit.push_back(0);
      }
    }

    ll sum(int i) {
      ll ret = 0;

      while (i > 0) {
        ret += bit[i];
        i -= i & -i;
      }

      return ret;
    }

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

int main() {
  int Q, K;
  cin >> Q >> K;

  int T[Q];
  ll V[Q];

  vector<ll> nums;
  set<ll> S;

  for (int i = 0; i < Q; ++i) {
    int type;
    ll v;
    cin >> type;
    T[i] = type;

    if (type == 1) {
      cin >> v;
      V[i] = v;
      if (not S.count(v)) {
        S.insert(v);
        nums.push_back(v);
      }
    }
  }

  sort(nums.begin(), nums.end());
  map<ll, int> pos;
  map<int, ll> val;
  for (int i = 0; i < nums.size(); ++i) {
    ll v = nums[i];
    pos[v] = i + 1;
    val[i + 1] = v;
  }

  BinaryIndexTree bit(Q + 10);
  int L = nums.size();

  for (int i = 0; i < Q; ++i) {
    int type = T[i];

    if (type == 1) {
      ll v = V[i];
      int idx = pos[v];
      bit.add(idx, 1);
    } else {
      int sum = bit.sum(L);
      if (sum < K) {
        cout << -1 << endl;
      } else {
        int ng = 0;
        int ok = L;

        while (abs(ok - ng) >= 2) {
          int idx = (ok + ng) / 2;
          ll k = bit.sum(idx);

          if (K <= k) {
            ok = idx;
          } else {
            ng = idx;
          }
        }

        bit.add(ok, -1);
        cout << val[ok] << endl;
      }
    }
  }

  return 0;
}
0