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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
#define INF (int)1e9
#define EPS (double)1e-9
#define MOD ((int)1e9 + 7)
using namespace std;
typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (const auto &elem : a) {
    if (cnt++)
      cerr << ' ';
    cerr << elem;
  }
  cerr << endl;
}

/*Binary Indexed Tree*/
template <typename Type> struct BIT {
  int n;
  vector<Type> dat; // i:=[1,n].

  BIT(int n_) : n(n_), dat(n_ + 1, 0) {}

  void init() { dat.assign(n + 1, 0); }
  void add(int i, Type x) { // i番目の要素にxを足す.
    for (; i <= n; i += i & -i)
      dat[i] += x;
  }
  Type sum(int i) { //[1,i]間の和を求める.
    Type res = 0;
    for (; i > 0; i -= i & -i)
      res += dat[i];
    return res;
  }
  Type rangesum(int l, int r) {
    return sum(r) - sum(l - 1);
  } //[l,r]間の和を求める.
};

int main() { // 解説参考.
  int n, q;
  cin >> n >> q;

  vi c(n + 1);
  for (int i = 1; i <= n; ++i)
    cin >> c[i];

  vvi ql(n + 1), qid(n + 1);
  REP(i, q) {
    int l, r;
    cin >> l >> r;
    ql[r].push_back(l);
    qid[r].push_back(i);
  }

  BIT<int> tree(n);
  vi p(n + 1, 0);
  vi ans(q);
  for (int i = 1; i <= n; ++i) {
    if (p[c[i]] > 0)
      tree.add(p[c[i]], -1);
    tree.add(i, 1);
    p[c[i]] = i;
    REP(j, ql[i].size()) ans[qid[i][j]] = tree.rangesum(ql[i][j], i);
  }

  for (auto elem : ans)
    cout << elem << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
#define INF (int)1e9
#define EPS (double)1e-9
#define MOD ((int)1e9 + 7)
using namespace std;
typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (const auto &elem : a) {
    if (cnt++)
      cerr << ' ';
    cerr << elem;
  }
  cerr << endl;
}

struct SegmentTree { // 適宜アレンジする!
  int n;
  vector<bitset<(int)5e5>> node;

  SegmentTree(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    node.resize(2 * n - 1, 0);
  }

  void update(int k, int a) {
    k += n - 1;
    node[k].set(a);
    while (k > 0) {
      k = (k - 1) / 2;
      node[k].set(a);
    }
  }
  bitset<(int)5e5> query(int a, int b, int k = 0, int l = 0,
                         int r = -1) { //[a,b).
    if (r == -1)
      r = n;
    if (b <= l or r <= a)
      return bitset<(int)5e5>(0);
    if (a <= l and r <= b)
      return node[k];
    else {
      bitset<(int)5e5> vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
      bitset<(int)5e5> vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
      return vl | vr;
    }
  }
};

int main() {
  int n, q;
  cin >> n >> q;

  SegmentTree ob(n);
  REP(i, n) {
    int c;
    cin >> c;
    c--;

    ob.update(i, c);
  }

  REP(i, q) {
    int l, r;
    cin >> l >> r;
    l--, r--;

    auto res = ob.query(l, r + 1);

    cout << res.count() << endl;
  }
}