Problem p02599 (C++) — buggy submission, verdict: Runtime Error

// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 08/02/20 20:55:48
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
template <typename T> class binary_indexed_tree {
private:
  vector<T> tree_;

public:
  explicit binary_indexed_tree(size_t size) : tree_(size + 1) {}

  template <typename Iter>
  explicit binary_indexed_tree(Iter begin, Iter end)
      : binary_indexed_tree(end - begin) {
    for (auto it = begin; it != end; ++it)
      add(it - begin, *it);
  }

  void add(size_t index, T x) {
    for (size_t i = index + 1; i < tree_.size(); i += i & (-i))
      tree_[i] += x;
  }

  T sum(size_t index) const {
    T sum = 0;
    for (size_t i = index + 1; i > 0; i -= i & (-i))
      sum += tree_[i];
    return sum;
  }

  T range_sum(size_t l, size_t r) const {
    return l > 0 ? sum(r - 1) - sum(l - 1) : sum(r - 1);
  }
};

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

  int n, q;
  cin >> n >> q;
  vector<int> C(n);
  for (auto &c : C)
    cin >> c;

  vector<int> p(n + 1, -1);
  vector<pair<int, int>> cm;
  for (int i = 0; i < n; i++) {
    cm.emplace_back(p[C[i]], i);
    p[C[i]] = i;
  }
  sort(cm.begin(), cm.end());

  struct Query {
    int i;
    int l, r;
  };
  vector<Query> Q(q);
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    Q[i] = {i, l, r};
  }
  sort(Q.begin(), Q.end(), [](auto l, auto r) { return l.l < r.l; });

  vector<int> ans(q);
  binary_indexed_tree<int> bit(n);
  int l = 0;
  int k = 0;
  for (int i = 0; i < n; i++) {
    while (l < n and cm[l].first < i) {
      bit.add(cm[l].second, 1);
      l++;
    }
    while (k < q and Q[k].l == i) {
      ans[Q[k].i] = bit.range_sum(i, Q[k].r + 1);
      k++;
    }
  }
  for (auto a : ans)
    cout << a << endl;

  return 0;
}


Error: 0

Fixed submission:

// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 08/02/20 20:55:48
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

template <typename M> class segment_tree {
private:
  size_t size_;
  std::vector<M> tree_;

public:
  explicit segment_tree(size_t size) : size_(size), tree_(size * 2) {}

  template <typename Iter>
  explicit segment_tree(Iter begin, Iter end) : segment_tree(end - begin) {
    for (auto it = begin; it != end; ++it)
      tree_[size_ + it - begin] = *it;
    for (size_t i = size_ - 1; i > 0; i--)
      tree_[i] = tree_[i * 2] * tree_[i * 2 + 1];
  }

  M operator[](size_t index) const { return tree_[size_ + index]; }

  void set(size_t index, M val) {
    auto i = size_ + index;
    tree_[i] = val;
    i >>= 1;
    while (i > 0) {
      tree_[i] = tree_[i * 2] * tree_[i * 2 + 1];
      i >>= 1;
    }
  }

  M query(size_t l, size_t r) const {
    M lhs, rhs;
    for (auto _l = l + size_, _r = r + size_; _l < _r; _l >>= 1, _r >>= 1) {
      if (_l & 1)
        lhs = lhs * tree_[_l++];
      if (_r & 1)
        rhs = tree_[--_r] * rhs;
    }
    return lhs * rhs;
  }
};

struct Set {
  bitset<(ll)5e5 + 1> s;
  Set() = default;
  Set(bitset<(ll)5e5 + 1> &&s) : s(s) {}
  Set(int v) { s[v] = 1; }
  Set operator*(const Set &r) const { return {this->s | r.s}; }
  Set &operator*=(const Set &r) { return *this = *this * r; }
};

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

  int n, q;
  cin >> n >> q;
  vector<int> C(n);
  for (auto &c : C)
    cin >> c;
  segment_tree<Set> rsq(C.begin(), C.end());
  while (q--) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    const auto &s = rsq.query(l, r + 1).s;
    int ans = 0;
    for (int i = 0; i < n; i++)
      ans += s[i];
    cout << ans << endl;
  }

  return 0;
}