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

#include <bits/stdc++.h>
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define pi (double)acos(-1.0)
#define P pair<int, int>
#define PiP pair<ll, pair<ll, ll>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;

template <class Abel> struct BIT {
  vector<Abel> dat[2];
  Abel UNITY_SUM = 0;

  /* [1, n] */
  BIT(int n) { init(n); }
  void init(int n) {
    for (int iter = 0; iter < 2; ++iter)
      dat[iter].assign(n + 1, UNITY_SUM);
  }

  /* a, b are 1-indexed, [a, b) */
  inline void sub_add(int p, int a, Abel x) {
    for (int i = a; i < (int)dat[p].size(); i += i & -i)
      dat[p][i] = dat[p][i] + x;
  }
  inline void add(int a, int b, Abel x) {
    sub_add(0, a, x * -(a - 1));
    sub_add(1, a, x);
    sub_add(0, b, x * (b - 1));
    sub_add(1, b, x * (-1));
  }

  /* a is 1-indexed, [a, b) */
  inline Abel sub_sum(int p, int a) {
    Abel res = UNITY_SUM;
    for (int i = a; i > 0; i -= i & -i)
      res = res + dat[p][i];
    return res;
  }
  inline Abel sum(int a, int b) {
    return sub_sum(0, b - 1) + sub_sum(1, b - 1) * (b - 1) - sub_sum(0, a - 1) -
           sub_sum(1, a - 1) * (a - 1);
  }

  /* debug */
  void print() {
    for (int i = 1; i < (int)dat[0].size(); ++i)
      cout << sum(i, i + 1) << ",";
    cout << endl;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> c(n), ans(q), p(n, -1);
  vector<tuple<int, int, int>> r(q);
  rep(i, n) {
    cin >> c[i];
    c[i]--;
  }
  rep(i, q) {
    cin >> get<1>(r[i]) >> get<0>(r[i]);
    get<2>(r[i]) = i;
  }
  int j = 0;
  sort(all(r));
  BIT<int> bit(n);
  rep(i, q) {
    while (j < get<0>(r[i])) {
      if (p[c[j]] != -1)
        bit.add(p[c[j]] + 1, p[c[j]] + 2, -1);
      bit.add(j + 1, j + 2, 1);
      p[c[j]] = j;
      j++;
    }
    ans[get<2>(r[i])] = bit.sum(get<1>(r[i]), get<0>(r[i]) + 1);
  }
  rep(i, q) cout << ans[i] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define pi (double)acos(-1.0)
#define P pair<int, int>
#define PiP pair<ll, pair<ll, ll>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;

// --- 1-indexed --- //
template <class Abel> struct BIT {
  vector<Abel> dat[2];
  Abel UNITY_SUM = 0;

  BIT(int n) { init(n); }
  void init(int n) {
    for (int iter = 0; iter < 2; ++iter)
      dat[iter].assign(n + 1, UNITY_SUM);
  }

  inline void sub_add(int p, int a, Abel x) {
    for (int i = a; i < (int)dat[p].size(); i += i & -i)
      dat[p][i] = dat[p][i] + x;
  }
  inline void add(int a, int b, Abel x) {
    sub_add(0, a, x * -(a - 1));
    sub_add(1, a, x);
    sub_add(0, b, x * (b - 1));
    sub_add(1, b, x * (-1));
  }

  inline Abel sub_sum(int p, int a) {
    Abel res = UNITY_SUM;
    for (int i = a; i > 0; i -= i & -i)
      res = res + dat[p][i];
    return res;
  }
  inline Abel sum(int a, int b) {
    return sub_sum(0, b - 1) + sub_sum(1, b - 1) * (b - 1) - sub_sum(0, a - 1) -
           sub_sum(1, a - 1) * (a - 1);
  }

  void print() {
    for (int i = 1; i < (int)dat[0].size(); ++i)
      cout << sum(i, i + 1) << ",";
    cout << endl;
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> c(n), ans(n);
  vector<PiP> a(q);
  rep(i, n) {
    cin >> c[i];
    c[i]--;
  }
  rep(i, q) {
    cin >> a[i].second.first >> a[i].first;
    a[i].second.second = i;
  }
  sort(all(a));
  int now = 0;
  vector<int> p(n + 10, -1);
  BIT<int> bit(n + 10);
  rep(i, n) {
    if (p[c[i]] != -1) {
      bit.add(p[c[i]], p[c[i]] + 1, -1);
    }
    p[c[i]] = i + 1;
    bit.add(i + 1, i + 2, 1);
    while (a[now].first == i + 1) {
      ans[a[now].second.second] = bit.sum(a[now].second.first, i + 2);
      now++;
      if (now >= q)
        break;
    }
  }
  rep(i, q) cout << ans[i] << endl;
}