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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define LL_MAX LLONG_MAX
#define ULL_MAX ULLONG_MAX
#define ull unsigned long long
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define pb(x) push_back(x)
#define re() pop_back()
#define eb(x) emplace_back(x)
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
#define all(a) a.begin(), a.end()
#define msort(a) sort(a.begin(), a.end())
#define pii pair<int, int>
#define sz(x) int(x.size())
#define mk make_pair
#define endl '\\n'

const int N = 5e5 + 4;
int dat[N], last[N], c[N], ans[N];
vector<int> qi[N], qs[N];
void update(int i, int val) {
  while (i < N) {
    dat[i] += val;
    i += i & -i;
  }
}

int sum(int i) {
  int sum = 0;
  while (i) {
    sum += dat[i];
    i -= i & -i;
  }
  return sum;
}

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

void sol() {
  int n, q;
  cin >> n >> q;
  for (int i = 1; i < n + 1; i++) {
    cin >> c[i];
  }

  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    qi[y].eb(i);
    qs[y].eb(x);
  }

  for (int i = 1; i < n + 1; i++) {
    if (last[c[i]] > 0) {
      update(last[c[i]], -1);
    }
    update(i, 1);
    last[c[i]] = i;
    for (int j = 0; j < sz(qs[i]); j++) {
      ans[qi[i][j]] = rangesum(qs[i][j], i);
    }
  }

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

int main() {
  IOS;
  ll t = 1; // cin>>t;
  while (t--) {
    sol();
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define LL_MAX LLONG_MAX
#define ULL_MAX ULLONG_MAX
#define ull unsigned long long
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define pb(x) push_back(x)
#define re() pop_back()
#define eb(x) emplace_back(x)
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
#define all(a) a.begin(), a.end()
#define msort(a) sort(a.begin(), a.end())
#define pii pair<int, int>
#define sz(x) int(x.size())
#define mk make_pair
#define endl '\\n'

const int N = 5e5 + 4;
vector<int> dat(N);
vector<int> last(N), c(N);

void update(int i, int val) {
  while (i < N) {
    dat[i] += val;
    i += i & -i;
  }
}

int sum(int i) {
  int sum = 0;
  while (i) {
    sum += dat[i];
    i -= i & -i;
  }
  return sum;
}

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

void sol() {
  int n, q;
  cin >> n >> q;
  for (int i = 1; i < n + 1; i++) {
    cin >> c[i];
  }

  int ans[n + 1];
  vector<int> qi[n + 1], qs[n + 1];
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    qi[y].eb(i);
    qs[y].eb(x);
  }

  for (int i = 1; i < n + 1; i++) {
    if (last[c[i]] > 0) {
      update(last[c[i]], -1);
    }
    update(i, 1);
    last[c[i]] = i;
    for (int j = 0; j < sz(qs[i]); j++) {
      ans[qi[i][j]] = rangesum(qs[i][j], i);
    }
  }

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

int main() {
  IOS;
  ll t = 1; // cin>>t;
  while (t--) {
    sol();
  }
}