結果

問題 No.649 ここでちょっとQK!
ユーザー simansiman
提出日時 2022-03-18 01:58:47
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 616 ms / 3,000 ms
コード長 1,848 bytes
コンパイル時間 4,413 ms
コンパイル使用メモリ 149,992 KB
実行使用メモリ 44,512 KB
最終ジャッジ日時 2024-04-10 00:17:27
合計ジャッジ時間 11,075 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 279 ms
6,940 KB
testcase_04 AC 452 ms
44,512 KB
testcase_05 AC 422 ms
44,372 KB
testcase_06 AC 165 ms
7,668 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 284 ms
21,940 KB
testcase_13 AC 279 ms
21,940 KB
testcase_14 AC 277 ms
21,936 KB
testcase_15 AC 241 ms
22,064 KB
testcase_16 AC 228 ms
21,936 KB
testcase_17 AC 264 ms
23,852 KB
testcase_18 AC 293 ms
25,468 KB
testcase_19 AC 367 ms
27,196 KB
testcase_20 AC 417 ms
29,612 KB
testcase_21 AC 381 ms
32,256 KB
testcase_22 AC 454 ms
33,920 KB
testcase_23 AC 550 ms
35,592 KB
testcase_24 AC 568 ms
37,488 KB
testcase_25 AC 601 ms
39,172 KB
testcase_26 AC 616 ms
40,836 KB
testcase_27 AC 3 ms
6,940 KB
testcase_28 AC 3 ms
6,944 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 179 ms
16,156 KB
testcase_31 AC 181 ms
16,272 KB
testcase_32 AC 2 ms
6,944 KB
testcase_33 AC 2 ms
6,940 KB
testcase_34 AC 1 ms
6,944 KB
testcase_35 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

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