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

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define ll long long
#define prl pair<ll, ll>
#define pri pair<int, int>
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define db double
#define N 1000005
#define mod 1000000007
#define INF 2147483647
using namespace std;
mt19937 mrand(random_device{}());
uniform_int_distribution<ll> dist(0, 100000000);
ll gcd(ll a, ll b) { return a ? gcd(b % a, a) : b; }
ll qpow(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
int n;
int a[N], c[N]; // 对应原数组和树状数组
inline int lowbit(int x) { return x & (-x); }
void modify(int i, int k) { // 在i位置加上k
  while (i <= n) {
    c[i] += k;
    i += lowbit(i);
  }
}
int getsum(int i) { // 求A[1 - i]的和
  int res = 0;
  while (i > 0) {
    res += c[i];
    i -= lowbit(i);
  }
  return res;
}
int loc[N];
struct query {
  int l, r, id;
  bool operator<(query &b) { return r < b.r; }
} Q[N];
int ans[N];
int main() {
  IOS;
  int q;
  cin >> n >> q;
  rep(i, 1, n + 1) { cin >> a[i]; }
  rep(i, 0, q) {
    cin >> Q[i].l >> Q[i].r;
    Q[i].id = i + 1;
  }
  sort(Q, Q + q);
  int cur = 0;
  rep(i, 1, n + 1) {
    if (loc[a[i]]) {
      modify(loc[a[i]], -1);
    }
    modify(i, 1);
    loc[a[i]] = i;
    while (cur < q && Q[cur].r == i) {
      ans[Q[cur].id] = getsum(i);
      if (Q[cur].l > 1)
        ans[Q[cur].id] -= getsum(Q[cur].l - 1);
      cur++;
    }
    if (cur == q)
      break;
  }
  rep(i, 1, q + 1) cout << ans[i] << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define ll long long
#define prl pair<ll, ll>
#define pri pair<int, int>
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define db double
#define N 100005
#define mod 1000000007
#define INF 2147483647
using namespace std;
mt19937 mrand(random_device{}());
uniform_int_distribution<ll> dist(0, 100000000);
ll gcd(ll a, ll b) { return a ? gcd(b % a, a) : b; }
ll qpow(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
int n;
int a[N], c[N]; // 对应原数组和树状数组
inline int lowbit(int x) { return x & (-x); }
void modify(int i, int k) { // 在i位置加上k
  while (i <= n) {
    c[i] += k;
    i += lowbit(i);
  }
}
int getsum(int i) { // 求A[1 - i]的和
  int res = 0;
  while (i > 0) {
    res += c[i];
    i -= lowbit(i);
  }
  return res;
}
int loc[N];
struct query {
  int l, r, id;
  bool operator<(query &b) { return r < b.r; }
} Q[N];
int ans[N];
int main() {
  IOS;
  int q;
  cin >> n >> q;
  rep(i, 1, n + 1) { cin >> a[i]; }
  rep(i, 0, q) {
    cin >> Q[i].l >> Q[i].r;
    Q[i].id = i + 1;
  }
  sort(Q, Q + q);
  int cur = 0;
  rep(i, 1, n + 1) {
    if (loc[a[i]]) {
      modify(loc[a[i]], -1);
    }
    modify(i, 1);
    loc[a[i]] = i;
    while (cur < q && Q[cur].r == i) {
      ans[Q[cur].id] = getsum(i);
      if (Q[cur].l > 1)
        ans[Q[cur].id] -= getsum(Q[cur].l - 1);
      cur++;
    }
    if (cur == q)
      break;
  }
  rep(i, 1, q + 1) cout << ans[i] << '\\n';
  return 0;
}