Submission #1519180


Source Code Expand

// clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define forever for (;;)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
template<typename A> using V = std::vector<A>;
template<typename A> using F = std::function<A>;
template<typename A, typename B> using P = std::pair<A, B>;
using pii = P<int, int>;
using vi = V<int>;
using vd = V<double>;
using vs = V<std::string>;
using vpii = V<pii>;
using vvi = V<vi>;
using vvpii = V<vpii>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int n) { return n & 1; }
constexpr bool even(const int n) { return ~n & 1; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> std::istream &operator>>(std::istream &is, std::pair<A, B> &p) { is >> p.first; is >> p.second; return is; }
using namespace std;
// clang-format on

template<typename Monoid> class SegTree {
  using T = typename Monoid::value_type;

  Monoid m;
  std::vector<T> tree; // 1-indexed
  int size = 1;

  void propTo(int i) { tree[i] = m(tree[i * 2], tree[i * 2 + 1]); }

public:
  SegTree(const int n = 0) {
    while (size < n) size *= 2;
    tree.assign(size * 2, m.id());
  }

  template<typename InputIterator> SegTree(InputIterator first, InputIterator last) {
    int n = std::distance(first, last);
    while (size < n) size *= 2;
    tree.resize(size * 2, m.id());
    std::copy(first, last, tree.begin() + size);
    for (int i = size - 1; i >= 1; i--) propTo(i);
  }

  T fold(int l, int r) { // [l, r)
    T accl = m.id(), accr = m.id();
    for (l += size, r += size; l < r; l /= 2, r /= 2) {
      if (l & 1) accl = m(accl, tree[l++]);
      if (r & 1) accr = m(tree[--r], accr);
    }
    return m(accl, accr);
  }

  void update(int i, const T &x) {
    tree[i += size] = x;
    while (i /= 2) propTo(i);
  }

  const T &operator[](int i) const { return tree[i + size]; }
};

template<typename T> struct sumMonoid {
  using value_type = T;
  constexpr value_type id() { return 0; }
  constexpr value_type operator()(const value_type &a, const value_type &b) { return a + b; }
};

/* Find the maximum x∈[low, up) such that f(x) is true
    ---*     (true)
        ---- (false)
*/
template<typename F> int binarySearchL(int low, int up, const F &f) {
  while (up - low > 1) {
    int mid = low + (up - low) / 2;
    (f(mid) ? low : up) = mid;
  }
  return low;
}

main {
  int q;
  cin >> q;
  SegTree<sumMonoid<int>> st(200001);
  while (q--) {
    int t, x;
    cin >> t >> x;
    switch (t) {
      case 1: st.update(x, st[x] + 1); break;
      case 2:
        int a = binarySearchL(1, 200001, [&](int k) { return st.fold(0, k) < x; });
        st.update(a, st[a] - 1);
        cout << a << endl;
        break;
    }
  }
}

Submission Info

Submission Time
Task C - データ構造
User AyaMorisawa
Language C++14 (GCC 5.4.1)
Score 100
Code Size 3409 Byte
Status AC
Exec Time 386 ms
Memory 4992 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 2
AC × 18
Set Name Test Cases
Sample sample_01.txt, sample_02.txt
All sample_01.txt, sample_02.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask1_14.txt, subtask1_15.txt, subtask1_16.txt
Case Name Status Exec Time Memory
sample_01.txt AC 3 ms 4352 KB
sample_02.txt AC 3 ms 4352 KB
subtask1_01.txt AC 3 ms 4352 KB
subtask1_02.txt AC 3 ms 4352 KB
subtask1_03.txt AC 3 ms 4352 KB
subtask1_04.txt AC 28 ms 4352 KB
subtask1_05.txt AC 59 ms 4480 KB
subtask1_06.txt AC 3 ms 4352 KB
subtask1_07.txt AC 148 ms 4480 KB
subtask1_08.txt AC 379 ms 4992 KB
subtask1_09.txt AC 378 ms 4992 KB
subtask1_10.txt AC 244 ms 4608 KB
subtask1_11.txt AC 246 ms 4608 KB
subtask1_12.txt AC 100 ms 4352 KB
subtask1_13.txt AC 386 ms 4992 KB
subtask1_14.txt AC 384 ms 4992 KB
subtask1_15.txt AC 315 ms 4992 KB
subtask1_16.txt AC 228 ms 4608 KB