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

#include <algorithm>
#include <chrono>
#include <iostream>
#include <random>
#define rep(i, x) for (ll i = 0; i < x; i++)
using namespace std;
typedef long long ll;
const ll MOD = (ll)1e9 + 7;
const ll N = 5e5 + 5, inf = 1e9 + 5000;
ll add(ll x, ll y) {
  x += y;
  if (x >= MOD)
    return x - MOD;
  return x;
}
ll sub(ll x, ll y) {
  x -= y;
  if (x < 0)
    return x + MOD;
  return x;
}
ll mult(ll x, ll y) { return (x * y) % MOD; }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

struct FenwickTree {
  vector<int> bit; // binary indexed tree
  int n;

  FenwickTree(int n) {
    this->n = n;
    bit.assign(n, 0);
  }

  FenwickTree(vector<int> a) : FenwickTree(a.size()) {
    for (size_t i = 0; i < a.size(); i++)
      add(i, a[i]);
  }

  int sum(int r) {
    int ret = 0;
    for (; r >= 0; r = (r & (r + 1)) - 1)
      ret += bit[r];
    return ret;
  }

  int sum(int l, int r) { return sum(r) - sum(l - 1); }

  void add(int idx, int delta) {
    for (; idx < n; idx = idx | (idx + 1))
      bit[idx] += delta;
  }
};

vector<pair<int, int>> right_segs[1000000]; // left, id

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  FenwickTree t(1000000);
  vector<int> last(1000000, -1);
  vector<int> ans(1000000);

  rep(i, q) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    right_segs[r].push_back({l, i});
  }

  for (int i = 0; i < n; i++) {
    if (last[a[i]] != -1) {
      t.add(last[a[i]], -1);
    }
    t.add(i, 1);
    last[a[i]] = i;

    for (auto p : right_segs[i]) {
      ans[p.second] = t.sum(p.first, i);
    }
  }

  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <chrono>
#include <iostream>
#include <random>
#define rep(i, x) for (ll i = 0; i < x; i++)
using namespace std;
typedef long long ll;
const ll MOD = (ll)1e9 + 7;
const ll N = 5e5 + 5, inf = 1e9 + 5000;
ll add(ll x, ll y) {
  x += y;
  if (x >= MOD)
    return x - MOD;
  return x;
}
ll sub(ll x, ll y) {
  x -= y;
  if (x < 0)
    return x + MOD;
  return x;
}
ll mult(ll x, ll y) { return (x * y) % MOD; }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

struct FenwickTree {
  vector<int> bit; // binary indexed tree
  int n;

  FenwickTree(int n) {
    this->n = n;
    bit.assign(n, 0);
  }

  FenwickTree(vector<int> a) : FenwickTree(a.size()) {
    for (size_t i = 0; i < a.size(); i++)
      add(i, a[i]);
  }

  int sum(int r) {
    int ret = 0;
    for (; r >= 0; r = (r & (r + 1)) - 1)
      ret += bit[r];
    return ret;
  }

  int sum(int l, int r) { return sum(r) - sum(l - 1); }

  void add(int idx, int delta) {
    for (; idx < n; idx = idx | (idx + 1))
      bit[idx] += delta;
  }
};

vector<pair<int, int>> right_segs[500005]; // left, id

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  FenwickTree t(500005);
  vector<int> last(500005, -1);
  vector<int> ans(n);

  rep(i, q) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    right_segs[r].push_back({l, i});
  }

  for (int i = 0; i < n; i++) {
    if (last[a[i]] != -1) {
      t.add(last[a[i]], -1);
    }
    t.add(i, 1);
    last[a[i]] = i;

    for (auto p : right_segs[i]) {
      ans[p.second] = t.sum(p.first, i);
    }
  }

  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}