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

#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)

using namespace std;

static const ll INF = 1e15;

template <typename T> static inline void chmin(T &ref, const T value) {
  if (ref > value)
    ref = value;
}

template <typename T> static inline void chmax(T &ref, const T value) {
  if (ref < value)
    ref = value;
}

struct BIT {
  vector<ll> bit_;
  const ll n_;

  BIT(ll n) : n_(n) { bit_.resize(n + 1LL, 0); }

  ll sum(int i) {
    ll s = 0;
    while (i > 0) {
      s += bit_[i];
      i -= i & (-i);
    }
    return s;
  }

  void add(int i, ll x) {
    while (i <= n_) {
      bit_[i] += x;
      i += (i & (-i));
    }
  }
};

int main() {
  ll n, q;
  cin >> n >> q;
  vector<int> cs(n);
  vector<int> prev(n + 1, -1);
  vector<int> cols(n + 1, -1);
  rep(i, n) {
    cin >> cs[i];
    prev[i] = cols[cs[i]];
    cols[cs[i]] = i;
  }
  vector<vector<P>> rs(n);
  rep(i, q) {
    int left, right;
    cin >> left >> right;
    left--;
    right--;
    rs[right].emplace_back(left, i);
  }
  BIT b(n + 1);
  vector<int> ans(q);
  rep(i, n) {
    auto p = prev[i];
    if (p >= 0) {
      b.add(p + 1, -1);
      b.add(i + 1, 1);
    } else {
      b.add(i + 1, 1);
    }
    for (auto f : rs[i]) {
      auto left = f.first;
      auto d = b.sum(i + 1) - b.sum(left);
      ans[f.second] = d;
    }
  }
  for (auto p : ans) {
    cout << p << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)

using namespace std;

static const ll INF = 1e15;

template <typename T> static inline void chmin(T &ref, const T value) {
  if (ref > value)
    ref = value;
}

template <typename T> static inline void chmax(T &ref, const T value) {
  if (ref < value)
    ref = value;
}

constexpr int maxN = 500010;

class SegmentTree {
private:
  ll n;
  vector<bitset<maxN>> vertex;

public:
  void init(int sourceN) {
    n = 1;
    while (n < sourceN)
      n *= 2;
    vertex.resize(2 * n - 1);
  }

  void update(ll i, ll x) {
    ll k = i + n - 1;
    vertex[k].set(x);
    while (k > 0) {
      k = (k - 1) / 2;
      vertex[k] |= vertex[2 * k + 1];
      vertex[k] |= vertex[2 * k + 2];
    }
    return;
  }

  void query(ll l, ll r, bitset<maxN> &v) {
    l += n - 1;
    r += n - 1;
    for (; l <= r; l /= 2, r = r / 2 - 1) {
      if (l % 2 == 0) {
        v |= vertex[l];
      }
      if (r & 1) {
        v |= vertex[r];
      }
    }
  }
};

SegmentTree t;
bitset<maxN> v;

int main() {
  ll n, q;
  cin >> n >> q;
  vector<ll> cs(n);
  rep(i, n) {
    cin >> cs[i];
    cs[i]--;
  }
  vector<P> qs(q);
  rep(i, q) {
    cin >> qs[i].first >> qs[i].second;
    qs[i].first--;
    qs[i].second--;
  }
  t.init(n);
  rep(i, n) { t.update(i, cs[i]); }
  rep(i, q) {
    v.reset();
    t.query(qs[i].first, qs[i].second, v);
    cout << v.count() << endl;
    // cout << i << endl;
    // for (auto p : m) {
    //	cout << "    " << p.first << "," << p.second << endl;
    // }
  }
  return 0;
}