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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define fi first
#define se second
long long mo = 1000000007;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef pair<ll, Pll> Plp;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <class A> void PR(A a, ll n) {
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  cout << "\\n";
}
ld PI = 3.14159265358979323846;

template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
  T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
};

int main() {
  ll N, Q;
  cin >> N >> Q;
  vector<ll> a(N), l(Q), r(Q), pi(N + 1, -1);
  rep(i, N) { cin >> a[i]; }
  vector<vector<Pll>> qs(N);
  rep(i, Q) {
    cin >> l[i] >> r[i];
    l[i]--;
    r[i]--;
    qs[l[i]].push_back(Pll(r[i], i));
  }
  vector<vector<ll>> ps(N);
  rep(i, N) {
    ll l = pi[a[i]];
    if (l != -1)
      ps[l].push_back(i);
    pi[a[i]] = i;
  }
  BIT<ll> d(N);
  vector<ll> ans(Q);
  for (ll i = N - 1; i >= 0; i--) {
    for (auto &y : ps[i])
      d.add(y, 1);
    for (auto &p : qs[i]) {
      ans[p.se] = (p.fi - i + 1) - d.sum(p.fi);
    }
  }
  rep(i, Q) cout << ans[i] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define fi first
#define se second
long long mo = 1000000007;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef pair<ll, Pll> Plp;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <class A> void PR(A a, ll n) {
  rep(i, n) {
    if (i)
      cout << ' ';
    cout << a[i];
  }
  cout << "\\n";
}
ld PI = 3.14159265358979323846;

template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
  T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
};

int main() {
  ll N, Q;
  cin >> N >> Q;
  vector<ll> a(N), l(Q), r(Q), pi(N + 1, -1);
  rep(i, N) { cin >> a[i]; }
  vector<vector<Pll>> qs(N);
  rep(i, Q) {
    cin >> l[i] >> r[i];
    l[i]--;
    r[i]--;
    qs[l[i]].push_back(Pll(r[i], i));
  }
  vector<vector<ll>> ps(N);
  rep(i, N) {
    ll l = pi[a[i]];
    if (l != -1)
      ps[l].push_back(i);
    pi[a[i]] = i;
  }
  BIT<ll> d(N);
  vector<ll> ans(N);
  for (ll i = N - 1; i >= 0; i--) {
    for (auto &y : ps[i])
      d.add(y, 1);
    for (auto &p : qs[i]) {
      ans[p.se] = (p.fi - i + 1) - d.sum(p.fi);
    }
  }
  rep(i, Q) cout << ans[i] << endl;
}