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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define rep(i, m, n) for (int(i) = (m); (i) < (n); ++(i))
#define rREP(i, m, n) for (int(i) = (n)-1; (i) >= (m); --(i))
#define all(x) (x).begin(), (x).end()
#define out(y, x, h, w) (y) < 0 || (x) < 0 || (y) >= (h) || (x) >= (w)
constexpr double PI = 3.1415926535897932;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
inline void init() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

// Binary Indexed Tree
// 1-indexed
template <class T> class BIT {
private:
  vector<T> arr;
  const int n;

public:
  // 初期化
  BIT(int _n) : arr(_n + 1, 0), n(_n) {}

  // １番目からi番目までの和
  T sum(int i) {
    T s = 0;
    while (i > 0) {
      s += arr[i];
      i -= i & (-i); // LSB減算
    }
    return s;
  }

  //[i, j]の要素の総和
  T sum(int i, int j) {
    T ret_i = sum(i - 1);
    T ret_j = sum(j);
    return ret_j - ret_i;
  }

  // i番目に要素xを追加
  void add(int i, T x) {
    while (i <= n) {
      arr[i] += x;
      i += i & (-i);
    }
  }
};

int main() {
  init();

  int N, Q;
  cin >> N >> Q;
  vector<int> A(N);
  BIT<int> bit(N);
  //(r,l)
  vector<tuple<int, int, int>> query(Q);
  vector<int> appear(N, -1);
  vector<int> ans(Q);
  rep(i, 0, N) cin >> A[i];
  rep(i, 0, Q) {
    int a, b;
    cin >> a >> b;
    query[i] = {b, a, i};
  }
  sort(all(query));
  int now = -1;
  rep(i, 0, Q) {
    int l, r, j;
    tie(r, l, j) = query[i];
    while (now + 1 < r) {
      now++;
      int &index = appear[A[now] - 1];
      if (index == -1) {
        index = now;
        bit.add(now + 1, 1);
      } else {
        bit.add(index + 1, -1);
        index = now;
        bit.add(now + 1, 1);
      }
    }
    ans[j] = bit.sum(l, r);
  }
  rep(i, 0, Q) cout << ans[i] << "\\n";

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define rep(i, m, n) for (int(i) = (m); (i) < (n); ++(i))
#define rREP(i, m, n) for (int(i) = (n)-1; (i) >= (m); --(i))
#define all(x) (x).begin(), (x).end()
#define out(y, x, h, w) (y) < 0 || (x) < 0 || (y) >= (h) || (x) >= (w)
constexpr int INF = 1000000001;
constexpr ll mod = 1000000007;
constexpr double PI = 3.1415926535897932;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
inline void init() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

template <class T> class segtree {
  int n = 1;
  vector<T> dat;
  T def = 0; // Default value

public:
  segtree(int _n) {
    while (n < _n)
      n *= 2;
    dat.resize(2 * n - 1, def);
  }
  segtree(vector<T> a) {
    int _n = a.size();
    while (n < _n)
      n *= 2;
    dat.resize(2 * n - 1, def);
    rep(i, 0, _n) dat[i + n - 1] = a[i];
    rREP(i, 0, n - 1) dat[i] = fun(dat[2 * i + 1], dat[2 * i + 2]);
  }

  // sum,min,max,etc...
  T fun(T x, T y) { return x | y; }

  // assignment,chmin,chmax,etc...
  void update(int i, T a) {
    i += n - 1;
    dat[i] = a;
    while (i > 0) {
      --i /= 2;
      dat[i] = fun(dat[2 * i + 1], dat[2 * i + 2]);
    }
  }

  T value(int a, int b, int k, int l, int r) {
    if (b <= l || r <= a)
      return def;
    if (a <= l && r <= b)
      return dat[k];
    T s = value(a, b, 2 * k + 1, l, (l + r) / 2);
    T t = value(a, b, 2 * k + 2, (l + r) / 2, r);
    return fun(s, t);
  }
  T value(int a, int b) { return value(a, b, 0, 0, n); }

  T operator[](int k) { return dat[k + n - 1]; }
};

int main() {
  init();

  int N, Q;
  cin >> N >> Q;
  vector<bitset<500000>> vec(N, 0);
  rep(i, 0, N) {
    int a;
    cin >> a;
    --a;
    vec[i].set(a);
  }
  segtree<bitset<500000>> seg(vec);
  rep(i, 0, Q) {
    int l, r;
    cin >> l >> r;
    --l;
    cout << seg.value(l, r).count() << "\\n";
  }

  return 0;
}