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

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define N (1000000000 + 7)
// #define N (998244353)
#define INF 1e16
typedef long long ll;
typedef pair<int, int> P;
typedef vector<ll> vec;
typedef vector<vec> mat;

template <typename T> struct BIT {
private:
  vector<T> array;
  int length;

public:
  BIT(int _n = 0) : array(_n + 1), length(_n) {}

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

  T sum(int i, int j) {
    T ret1 = sum(i - 1);
    T ret2 = sum(j);
    return ret2 - ret1;
  }

  void add(int i, T x) {
    while (i <= length) {
      array[i] += x;
      i += i & (-i);
    }
  }
};

int main(void) {
  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<int> pi(n + 1, -1);
  vector<vector<int>> ps(n + 1);
  for (int i = 0; i < n; i++) {
    int l = pi[a[i]];
    if (l != -1)
      ps[l].push_back(i + 1);
    pi[a[i]] = i + 1;
  }
  vector<vector<P>> qs(n + 1);
  for (int qi = 0; qi < q; qi++) {
    int l, r;
    cin >> l >> r;
    qs[l].emplace_back(r, qi);
  }
  BIT<int> d(n + 1);
  vector<int> ans(q);
  for (int x = n; x >= 1; x--) {
    for (int y : ps[x]) {
      d.add(y, 1);
    }
    for (P query : qs[x]) {
      int r = query.first, i = query.second;
      ans[i] = r - x + 1 - d.sum(r);
    }
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define N (1000000000 + 7)
// #define N (998244353)
#define INF 1e16
typedef long long ll;
typedef pair<int, int> P;
typedef vector<ll> vec;
typedef vector<vec> mat;

template <typename T> struct BIT {
private:
  vector<T> array;
  int length;

public:
  BIT(int _n = 0) : array(_n + 1), length(_n) {}

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

  T sum(int i, int j) {
    T ret1 = sum(i - 1);
    T ret2 = sum(j);
    return ret2 - ret1;
  }

  void add(int i, T x) {
    while (i <= length) {
      array[i] += x;
      i += i & (-i);
    }
  }
};

int main(void) {
  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<int> pi(n + 1, -1);
  vector<vector<int>> ps(n + 1);
  for (int i = 0; i < n; i++) {
    int l = pi[a[i]];
    if (l != -1)
      ps[l].push_back(i + 1);
    pi[a[i]] = i + 1;
  }
  vector<vector<P>> qs(n + 1);
  for (int qi = 0; qi < q; qi++) {
    int l, r;
    cin >> l >> r;
    qs[l].emplace_back(r, qi);
  }
  BIT<int> d(n + 1);
  vector<int> ans(n);
  for (int x = n; x >= 1; x--) {
    for (int y : ps[x]) {
      d.add(y, 1);
    }
    for (P query : qs[x]) {
      int r = query.first, i = query.second;
      ans[i] = r - x + 1 - d.sum(r);
    }
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}