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

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

template <typename T> class BinaryIndexedTree {
  std::vector<T> arr;
  const size_t size;
  const std::function<T(const T, const T)> operation;
  const T initial_value;

  void initialize_array() {
    this->arr = vector<T>(this->size, this->initial_value);
  }

public:
  BinaryIndexedTree(const size_t N, function<T(const T, const T)> func, T init)
      : size(N + 1), operation(func), initial_value(init) {
    initialize_array();
  }

  void update(const size_t idx, const T value) {
    long long idxll = (long long)idx;
    while (idxll <= (long long)this->size) {
      this->arr[idxll] = this->operation(this->arr[idxll], value);
      idxll += (idxll & -idxll);
    }
  }

  T query(const size_t idx) {
    T ret = this->initial_value;
    long long idxll = (long long)idx;
    while (idxll > 0) {
      ret = this->operation(ret, this->arr[idxll]);
      idxll -= (idxll & -idxll);
    }
    return ret;
  }
};

int main() {
  size_t N, Q;
  cin >> N >> Q;

  vector<size_t> prev(N + 1, 0);
  vector<vector<size_t>> point(N + 1);
  size_t c;
  for (size_t i = 1; i <= N; i++) {
    cin >> c;
    point[prev[c]].push_back(i);
    prev[c] = i;
  }

  vector<vector<pair<size_t, size_t>>> query(N + 1);
  size_t l, r;
  for (size_t i = 0; i < Q; i++) {
    cin >> l >> r;
    query[l].push_back(make_pair(r, i));
  }

  BinaryIndexedTree<size_t> bit(
      5e5 + 5, [](size_t a, size_t b) { return a + b; }, 0);

  vector<size_t> ans(Q);

  for (size_t l = N; l <= N; l--) {
    for (size_t r : point[l]) {
      bit.update(r, 1);
    }
    for (pair<size_t, size_t> q : query[l]) {
      size_t r = q.first;
      size_t i = q.second;
      ans[i] = r - l + 1 - bit.query(r);
    }
  }
  for (size_t a : ans) {
    cout << a << endl;
  }
}


Error: 0

Fixed submission:

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

template <typename T> class BinaryIndexedTree {
  std::vector<T> arr;
  const size_t size;
  const std::function<T(const T, const T)> operation;
  const T initial_value;

  void initialize_array() {
    this->arr = vector<T>(this->size, this->initial_value);
  }

public:
  BinaryIndexedTree(const size_t N, function<T(const T, const T)> func, T init)
      : size(N + 1), operation(func), initial_value(init) {
    initialize_array();
  }

  void update(const size_t idx, const T value) {
    long long idxll = (long long)idx;
    while (idxll <= (long long)this->size) {
      this->arr[idxll] = this->operation(this->arr[idxll], value);
      idxll += (idxll & -idxll);
    }
  }

  T query(const size_t idx) {
    T ret = this->initial_value;
    long long idxll = (long long)idx;
    while (idxll > 0) {
      ret = this->operation(ret, this->arr[idxll]);
      idxll -= (idxll & -idxll);
    }
    return ret;
  }
};

int main() {
  size_t N, Q;
  cin >> N >> Q;

  vector<size_t> prev(N + 1, 0);
  vector<vector<size_t>> p(N + 1);
  size_t c;
  for (size_t i = 1; i <= N; i++) {
    cin >> c;
    p[prev[c]].push_back(i);
    prev[c] = i;
  }

  vector<vector<pair<size_t, size_t>>> query(N + 1);
  size_t l, r;
  for (size_t i = 0; i < Q; i++) {
    cin >> l >> r;
    query[l].push_back(make_pair(r, i));
  }

  BinaryIndexedTree<size_t> bit(
      2e5 + 5, [](size_t a, size_t b) { return a + b; }, 0);

  vector<size_t> ans(Q);

  for (size_t l = N; l <= N; l--) {
    for (size_t r : p[l]) {
      bit.update(r, 1);
    }
    for (pair<size_t, size_t> q : query[l]) {
      size_t r = q.first;
      size_t i = q.second;
      ans[i] = r - l + 1 - bit.query(r);
    }
  }
  for (size_t a : ans) {
    cout << a << endl;
  }
}