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;

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int, int, int> tpl;

#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define EXIST(m, v) (m).find((v)) != (m).end()
#define LB(a, x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a, x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define RREP(i, n) RFOR(i, n, 0)

#define en "\\n"

constexpr double EPS = 1e-9;
constexpr double PI = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL << 60;
constexpr long long MOD = 1000000007; // 998244353;

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

template <typename T> struct SegmentTree {
  typedef function<T(T, T)> F;
  typedef function<T(T, T)> G;
  int n;
  F f;
  G g;
  T e;

  vector<T> val;

  SegmentTree(vector<T> a, F f_, G g_, T e) : f(f_), g(g_), e(e) {
    int sz = a.size();
    n = 1;

    while (n < sz)
      n <<= 1;
    val.resize(2 * n - 1, e);
    for (int i = 0; i < sz; i++)
      val[i + n - 1] = a[i];
    for (int i = n - 2; i >= 0; i--)
      val[i] = f(val[i * 2 + 1], val[i * 2 + 2]);
  }

  SegmentTree() {}

  void update(int pos, T v) {
    // 0-indexed
    int k = pos + n - 1;
    val[k] = g(val[k], v);
    while (k > 0) {
      k = (k - 1) / 2;
      val[k] = f(val[k * 2 + 1], val[k * 2 + 2]);
    }
  }

  T get(int pos) {
    // 0-indexed
    int k = pos + n - 1;
    return val[k];
  }

  T query(int a, int b, int k = 0, int l = 0, int r = -1) {
    // 0-indexed
    // [a,b)に対してquery
    if (r < 0)
      r = n;
    if (r <= a || b <= l)
      return e;
    if (a <= l && r <= b)
      return val[k];
    T lv = query(a, b, k * 2 + 1, l, (l + r) / 2);
    T rv = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return f(lv, rv);
  }
};

void Main() {
  int N, Q;
  cin >> N >> Q;
  vector<bitset<500000>> a(N);
  REP(i, N) {
    int c;
    cin >> c;
    c--;
    a[i][c] = 1;
  }

  auto f = [](bitset<500000> a, bitset<500000> b) { return a | b; };
  auto g = [](bitset<500000> a, bitset<500000> b) { return b; };

  bitset<500000> e;
  SegmentTree<bitset<500000>> st(a, f, g, e);
  REP(_, Q) {
    int l, r;
    cin >> l >> r;
    l--;
    auto s = st.query(l, r);
    int ans = s.count();
    cout << ans << en;
  }
  return;
}

int main(void) {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(15);
  int t = 1; // cin>>t;
  REP(_, t) Main();
  return 0;
}