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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

template <class Abel> struct BIT {
  vector<Abel> dat[2];
  Abel UNITY_SUM = 0; // to be set

  /* [1, n] */
  BIT(int n) { init(n); }
  void init(int n) {
    for (int iter = 0; iter < 2; ++iter)
      dat[iter].assign(n + 1, UNITY_SUM);
  }

  /* a, b are 1-indexed, [a, b) */
  inline void sub_add(int p, int a, Abel x) {
    for (int i = a; i < (int)dat[p].size(); i += i & -i)
      dat[p][i] = dat[p][i] + x;
  }
  inline void add(int a, int b, Abel x) {
    sub_add(0, a, x * -(a - 1));
    sub_add(1, a, x);
    sub_add(0, b, x * (b - 1));
    sub_add(1, b, x * (-1));
  }

  /* a is 1-indexed, [a, b) */
  inline Abel sub_sum(int p, int a) {
    Abel res = UNITY_SUM;
    for (int i = a; i > 0; i -= i & -i)
      res = res + dat[p][i];
    return res;
  }
  inline Abel sum(int a, int b) {
    return sub_sum(0, b - 1) + sub_sum(1, b - 1) * (b - 1) - sub_sum(0, a - 1) -
           sub_sum(1, a - 1) * (a - 1);
  }

  /* debug */
  void print() {
    for (int i = 1; i < (int)dat[0].size(); ++i)
      cout << sum(i, i + 1) << ",";
    cout << endl;
  }
};

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> a(N);
  for (int i = 0; i < N; ++i)
    cin >> a[i];

  vector<int> lefts(Q), rights(Q), ids(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> lefts[i] >> rights[i];
    --lefts[i];
    ids[i] = i;
  }
  sort(ids.begin(), ids.end(),
       [&](int i, int j) { return rights[i] < rights[j]; });

  BIT<int> bit(N + 5);
  vector<int> prev(1100000, -1);
  vector<int> res(Q, 0);
  int r = 0;
  for (auto i : ids) {
    for (; r < rights[i]; ++r) {
      bit.add(prev[a[r]] + 2, r + 2, 1);
      prev[a[r]] = r;
    }
    int tmp = bit.sum(lefts[i] + 1, lefts[i] + 2);
    res[i] = max(res[i], tmp);
  }
  for (int i = 0; i < Q; ++i)
    cout << res[i] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(n) begin(n), end(n)
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<char> vchar;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
template <typename Set> struct Monoid {
  using Op = function<Set(Set, Set)>;
  const Op f;
  const Set e;
  Monoid(const Op F, const Set E) : f(F), e(E) {}
};
template <typename Set> struct SegmentTree {
  using Op = function<Set(Set, Set)>;
  Monoid<Set> M;
  Set e;
  int size; // 一番下以外の区間たち全部の数
  vector<Set> seg;
  SegmentTree(int n, const Monoid<Set> &M) : M(M) {
    size = 1;
    while (size < n) {
      size <<= 1;
    }
    e = M.e;
    seg.assign(2 * size, e);
  }
  void set(int k, const Set &x) { seg[k + size] = x; }
  void build() {
    for (int k = size - 1; k > 0; k--) {
      seg[k] = M.f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }
  void update(int k, const Set &x) {
    k += size;
    seg[k] = x;
    while (k >>= 1) {
      seg[k] = M.f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }
  Set query(int a, int b) // 区間[a,b)に対して演算したやつを返す
  {
    Set L = e, R = e;
    for (a += size, b += size; a < b;
         a >>= 1, b >>= 1) // ループ一回ごとにa,bは親に向かう
    {
      if (a & 1) // a%2==1なら
      {
        L = M.f(L, seg[a++]);
      }
      if (b & 1) {
        R = M.f(seg[--b], R);
      }
    }
    return M.f(L, R);
  }
};
int main() {
  const int MAX = 5e5 + 1;
  int N, Q;
  cin >> N >> Q;

  typedef bitset<MAX> bit;
  bit bits;
  Monoid<bit> M([](bit a, bit b) { return a | b; }, (bit)0);
  SegmentTree<bit> seg(N, M);
  rep(i, N) {
    int c;
    cin >> c;
    bit b = 0;
    b[c] = 1;
    seg.update(i, b);
  }
  while (Q--) {
    int l, r;
    cin >> l >> r;
    bit q = seg.query(l - 1, r);
    cout << q.count() << "\\n";
  }
  return 0;
}