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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define all(v) v.begin(), v.end()
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-10;

template <typename T> struct BinaryIndexedTree {
  vector<T> data;

  BinaryIndexedTree(int sz) { data.assign(++sz, 0); }

  T sum(int k) {
    T ret = 0;
    for (++k; k > 0; k -= k & -k)
      ret += data[k];
    return (ret);
  }

  void add(int k, T x) {
    for (++k; k < data.size(); k += k & -k)
      data[k] += x;
  }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  vector<int> C(n);
  rep(i, n) {
    cin >> C[i];
    C[i]--;
  }
  vector<pair<int, int>> Q(q);
  vector<int> id(q);
  rep(i, q) {
    cin >> Q[i].first >> Q[i].second;
    Q[i].first--;
    Q[i].second--;
    id[i] = i;
  }
  sort(all(id), [&](int i, int j) { return Q[i].second < Q[j].second; });
  vector<int> last(n, -1);
  BinaryIndexedTree<int> bit(n);
  int tmp = -1;
  vector<int> ans(q);
  rep(i, q) {
    while (tmp < Q[id[i]].second) {
      tmp++;
      if (last[C[tmp]] != -1)
        bit.add(last[C[tmp]], -1);
      last[C[tmp]] = tmp;
      bit.add(tmp, 1);
    }
    ans[id[i]] = bit.sum(Q[id[i]].second) - bit.sum(Q[id[i]].first - 1);
  }
  rep(i, q) cout << ans[i] << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define all(v) v.begin(), v.end()
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-10;

template <typename Monoid> struct SegmentTree {
  using F = function<Monoid(Monoid, Monoid)>;

  int sz;
  vector<Monoid> seg;

  const F f;
  const Monoid M1;

  SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
    sz = 1;
    while (sz < n)
      sz <<= 1;
    seg.assign(2 * sz, M1);
  }

  void set(int k, const Monoid &x) { seg[k + sz] = x; }

  void build() {
    for (int k = sz - 1; k > 0; k--) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  void update(int k, const Monoid &x) {
    k += sz;
    seg[k] = x;
    while (k >>= 1) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  Monoid query(int a, int b) {
    Monoid L = M1, R = M1;
    for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if (a & 1)
        L = f(L, seg[a++]);
      if (b & 1)
        R = f(seg[--b], R);
    }
    return f(L, R);
  }

  Monoid operator[](const int &k) const { return seg[k + sz]; }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, q;
  cin >> n >> q;
  vector<int> C(n);
  SegmentTree<bitset<500005>> seg(
      n + 1, [](bitset<500005> a, bitset<500005> b) { return a | b; },
      bitset<500005>());
  rep(i, n) {
    cin >> C[i];
    C[i]--;
    bitset<500005> b;
    b.set(C[i], 1);
    seg.update(i, b);
  }
  while (q--) {
    int r, l;
    cin >> l >> r;
    cout << seg.query(l - 1, r).count() << "\\n";
  }
  return 0;
}