結果

問題 No.649 ここでちょっとQK!
ユーザー hashiryohashiryo
提出日時 2020-05-02 15:26:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 792 ms / 3,000 ms
コード長 3,380 bytes
コンパイル時間 1,571 ms
コンパイル使用メモリ 167,584 KB
実行使用メモリ 247,688 KB
最終ジャッジ日時 2023-08-28 18:45:27
合計ジャッジ時間 12,574 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 266 ms
4,380 KB
testcase_04 AC 179 ms
16,052 KB
testcase_05 AC 179 ms
15,844 KB
testcase_06 AC 142 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 375 ms
128,700 KB
testcase_13 AC 379 ms
128,420 KB
testcase_14 AC 360 ms
118,768 KB
testcase_15 AC 370 ms
128,552 KB
testcase_16 AC 353 ms
128,532 KB
testcase_17 AC 402 ms
140,592 KB
testcase_18 AC 444 ms
152,580 KB
testcase_19 AC 478 ms
164,224 KB
testcase_20 AC 514 ms
176,404 KB
testcase_21 AC 564 ms
188,360 KB
testcase_22 AC 594 ms
200,252 KB
testcase_23 AC 635 ms
212,200 KB
testcase_24 AC 678 ms
224,156 KB
testcase_25 AC 720 ms
235,980 KB
testcase_26 AC 792 ms
247,688 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 118 ms
8,132 KB
testcase_31 AC 112 ms
8,132 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << ": " << x << endl
#define debugArray(x, n)                           \
  for (long long hoge = 0; (hoge) < (n); ++(hoge)) \
  cerr << #x << "[" << hoge << "]: " << x[hoge] << endl

template <typename M>
struct SegmentTree_Dynamic {
  using T = typename M::T;
  using ll = long long;
  using U = unsigned long long;
  struct Node {
    T dat;
    // U xor_lazy;
    Node *ch[2];
    Node() : dat(M::ti()), ch{nullptr, nullptr} {}
    /*
void *operator new(size_t) {
  static Node pool[1 << 23];
  return pool + node_count++;
}
*/
  };
  using np = Node *;

 private:
  const int height;
  const ll n;
  static int node_count;
  np root;

 private:
  /*
   inline void push(np t, ll b) {
     if ((t->xor_lazy >> (U)b) & (U)1) swap(t->ch[0], t->ch[1]);
     if (t->ch[0] != nullptr) t->ch[0]->xor_lazy ^= t->xor_lazy;
     if (t->ch[1] != nullptr) t->ch[1]->xor_lazy ^= t->xor_lazy;
     t->xor_lazy = 0;
   }
   */
  T value(np t) { return t ? t->dat : M::ti(); }
  np set_Node(np t, U pos, T val, ll b) {
    if (t == nullptr) t = new Node();
    if (b < 0) {
      t->dat = val;
      return t;
    }
    // push(t, b);
    bool f = (pos >> (U)b) & (U)1;
    t->ch[f] = set_Node(t->ch[f], pos, val, b - 1);
    t->dat = M::f(value(t->ch[0]), value(t->ch[1]));
    return t;
  }
  T query_Node(ll l, ll r, np t, ll lb, ll ub, ll b) {
    if (t == nullptr || ub <= l || r <= lb) return M::ti();
    // push(t, b);
    if (l <= lb && ub <= r) return t->dat;
    ll c = (lb + ub) / 2;
    T vl = query_Node(l, r, t->ch[0], lb, c, b - 1);
    T vr = query_Node(l, r, t->ch[1], c, ub, b - 1);
    return M::f(vl, vr);
  }
  template <typename C>
  ll find(np t, C &check, U bias) {
    if (!t) return -1;
    ll ret = 0;
    T acc = M::ti();
    for (ll b = height - 1; b >= 0; b--) {
      // push(t, b);
      bool f = (bias >> (U)b) & (U)1;
      if (!check(M::f(acc, value(t->ch[f]))))
        acc = M::f(acc, value(t->ch[f])), f = !f;
      t = t->ch[f];
      if (!t) return -1;
      ret |= (U)f << (U)b;
    }
    return ret;
  }

 public:
  SegmentTree_Dynamic() {}
  SegmentTree_Dynamic(ll n_)
      : height(ceil(log2(n_))), n(1LL << height), root(nullptr) {}
  void clear() {
    Node::node_count = 0;
    root = nullptr;
  }
  void xor_all(U key) {
    if (root != nullptr) root->lazy ^= key;
  }
  void set_val(ll k, T x) { root = set_Node(root, k, x, height - 1); }
  //[a,b)
  T query(ll a, ll b) { return query_Node(a, b, root, 0, n, height - 1); }
  T operator[](ll k) { return query(k, k + 1); }
  // min { i : check(query(0,i+1)) = true }
  template <typename C>
  ll find_first(C &check, U bias = 0) {
    return find(root, check, bias);
  }
};
template <typename M>
int SegmentTree_Dynamic<M>::node_count = 0;

struct RsumQ {
  using T = int;
  static T ti() { return 0; }
  static T f(const T &l, const T &r) { return l + r; }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int Q, K;
  cin >> Q >> K;
  K--;
  SegmentTree_Dynamic<RsumQ> seg((long long)1e18 + 10);
  while (Q--) {
    long long v;
    cin >> v;
    if (v == 1) {
      cin >> v;
      seg.set_val(v, seg[v] + 1);
    } else {
      auto check = [&](int x) { return x > K; };
      v = seg.find_first(check);
      cout << v << endl;
      if (v >= 0) seg.set_val(v, seg[v] - 1);
    }
  }
  return 0;
}
0