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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

template <class T> inline bool chmax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

template <class T> class BinaryIndexedTree {
private:
  int N;
  std::vector<T> dat;

public:
  BinaryIndexedTree(int n) : N(n), dat(n + 1, static_cast<T>(0)) {}
  // a is 1-indexed
  void add(int a, const T v) {
    if (a == 0)
      return;
    while (a <= N) {
      dat[a] += v;
      a += (a & -a);
    }
  }
  // return sum of [1, a]
  // a is 1-indexed
  T sum(int a) const {
    T res = static_cast<T>(0);
    while (a > 0) {
      res += dat[a];
      a -= (a & -a);
    }
    return res;
  }
  // return sum of [a. b)
  // a and b are 1-indexed
  T sum(int a, int b) const { return sum(b - 1) - sum(a - 1); }
};

void solve() {
  int N, Q;
  cin >> N >> Q;
  vector<int> c(N);
  for (int i = 0; i < N; ++i)
    cin >> c[i];
  vector<pair<int, int>> query(Q);
  for (int i = 0; i < Q; ++i)
    cin >> query[i].first >> query[i].second;

  vector<vector<int>> pla(N + 1);
  for (int i = 0; i < N; ++i)
    pla[c[i]].push_back(i + 1);
  for (auto &v : pla)
    v.push_back(0);

  vector<int> cur(N + 1);
  BinaryIndexedTree<int> bit(N);
  for (int i = 1; i < N + 1; ++i)
    bit.add(pla[i][cur[i]], 1);

  vector<int> id(Q);
  for (int i = 0; i < Q; ++i)
    id[i] = i;
  sort(id.begin(), id.end(),
       [&query](int i, int j) { return query[i] < query[j]; });

  vector<int> res(Q);
  int curid = 0;
  for (int i = 0; i < N; ++i) {
    while (curid < Q && query[id[curid]].first == i + 1) {
      res[id[curid]] = bit.sum(query[id[curid]].second);
      ++curid;
    }
    bit.add(pla[c[i]][cur[c[i]]], -1);
    bit.add(pla[c[i]][++cur[c[i]]], 1);
  }

  for (int x : res)
    cout << x << '\\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

template <class T> inline bool chmax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

template <class T> class SegmentTree {
  using Func = std::function<T(T, T)>;

private:
  const Func F;
  const T DEF;
  int N;
  std::vector<T> dat;

public:
  SegmentTree(int n, const Func f, const T def) : F(f), DEF(def) {
    N = 1;
    while (N < n)
      N *= 2;
    dat.assign(2 * N, DEF);
  }
  // a is 0-indexed
  void set(int a, const T v) { dat[a + N] = v; }
  void build() {
    for (int i = N - 1; i > 0; --i)
      dat[i] = F(dat[2 * i], dat[2 * i + 1]);
  }
  // a is 0-indexed
  void update(int a, const T v) {
    int cur = a + N;
    dat[cur] = v;
    while ((cur /= 2) > 0)
      dat[cur] = F(dat[2 * cur], dat[2 * cur + 1]);
  }
  // return value of [a, b)
  // a and b are 0-indexed
  T get(int a, int b) const { return get(a, b, 1, 0, N); }
  T get(int a, int b, int k, int l, int r) const {
    if (b <= l || r <= a)
      return DEF;
    if (a <= l && r <= b)
      return dat[k];
    T vleft = get(a, b, 2 * k, l, (l + r) / 2);
    T vright = get(a, b, 2 * k + 1, (l + r) / 2, r);
    return F(vleft, vright);
  }
  // a is 0-indexed
  T at(int a) const { return dat.at(a + N); }
};

void solve() {
  int N, Q;
  cin >> N >> Q;
  vector<int> c(N);
  for (int i = 0; i < N; ++i)
    cin >> c[i];

  SegmentTree<bitset<500000>> st(
      N, [](auto a, auto b) { return a | b; }, bitset<500000>(0));
  for (int i = 0; i < N; ++i)
    st.set(i, bitset<500000>(1) << (c[i] - 1));
  st.build();

  vector<int> res(Q);
  for (int i = 0; i < Q; ++i) {
    int l, r;
    cin >> l >> r;
    res[i] = st.get(l - 1, r).count();
  }
  for (int x : res)
    cout << x << '\\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}