結果

問題 No.649 ここでちょっとQK!
ユーザー playrollerplayroller
提出日時 2019-06-21 16:58:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 262 ms / 3,000 ms
コード長 2,105 bytes
コンパイル時間 1,749 ms
コンパイル使用メモリ 176,716 KB
実行使用メモリ 8,512 KB
最終ジャッジ日時 2023-08-26 17:58:30
合計ジャッジ時間 6,854 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 262 ms
6,140 KB
testcase_04 AC 70 ms
8,512 KB
testcase_05 AC 70 ms
8,428 KB
testcase_06 AC 59 ms
8,276 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 62 ms
5,652 KB
testcase_13 AC 64 ms
5,628 KB
testcase_14 AC 80 ms
5,624 KB
testcase_15 AC 62 ms
5,560 KB
testcase_16 AC 61 ms
5,680 KB
testcase_17 AC 67 ms
6,028 KB
testcase_18 AC 74 ms
6,156 KB
testcase_19 AC 80 ms
6,404 KB
testcase_20 AC 87 ms
6,732 KB
testcase_21 AC 97 ms
7,544 KB
testcase_22 AC 101 ms
7,728 KB
testcase_23 AC 108 ms
7,864 KB
testcase_24 AC 114 ms
7,984 KB
testcase_25 AC 121 ms
8,136 KB
testcase_26 AC 127 ms
8,432 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,504 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 52 ms
5,684 KB
testcase_31 AC 51 ms
5,592 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define rep(i,j,n) for(int i=j;i<n;++i)
#define all(i) i.begin(),i.end()
#define rall(i) i.rbegin(), i.rend()
#define INF 1e9
#define LINF 1e18
const int mod = 1e9 + 7;

typedef long long i64;
typedef pair<int, int> pi;

template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vector<T>>;

i64 gcd(i64 n, i64 m) {return (m == 0? n : gcd(m, n % m));}
i64 lcd(i64 n, i64 m) {return (n / gcd(n, m) * m);}
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

template <class T> class BinaryIndexedTree {
  private:
    int n;
    std::vector<T> bit;

  public:
    BinaryIndexedTree(int node) : n(node), bit(node + 1, 0) {}

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

    inline T sum(int a, int b) {
      return sum(b) - sum(a - 1);
    }

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

    int get(long long k) {
      ++k;
      int res = 0, N = 1;
      while(N < bit.size()) N *= 2;
      for(int i = N / 2; i > 0; i /= 2) {
        if(res + i < bit.size() && bit[res + i] < k) {
          k -= bit[res + i];
          res += i;
        }
      }
      return res + 1;
    }
};


int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int q, k;
  cin >> q >> k;
  vt<pair<int, i64>> query(q);
  vt<i64> vec;
  rep(i, 0, q) {
    int c;
    i64 v;
    cin >> c;
    if(c == 1) {
      cin >> v;
      vec.push_back(v);
    }
    else v = 0;
    query[i] = {c, v};
  }

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

  BinaryIndexedTree<int> bit(vec.size());
  rep(i, 0, q) {
    auto p = query[i];
    if(p.first == 1) {
      int idx = lower_bound(all(vec), p.second) - vec.begin();
      bit.add(idx, 1);
    }
    else {
      if(bit.sum(vec.size() - 1) < k) {
        cout << -1 << endl;
      }
      else {
        int res = bit.get(k - 1);
        cout << vec[res - 1] << endl;
        bit.add(res - 1, -1);
      }
    }
  }
}
0