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

#include <bits/stdc++.h>
using namespace std;

#define ld long double
#define int long long

#define pb push_back
#define F first
#define S second
#define I insert
#define sz(a) (int)((a).size())
#define all(a) (a).begin(), (a).end()

#define debug1(a) cout << " " << #a << " : " << a << "\\n"
#define debug2(a, b)                                                           \\
  cout << " " << #a << " : " << a << " " << #b << " : " << b << "\\n"
#define debug3(a, b, c)                                                        \\
  cout << " " << #a << " : " << a << " " << #b << " : " << b << " " << #c      \\
       << " : " << c << "\\n"
#define debug4(a, b, c, d)                                                     \\
  cout << " " << #a << " : " << a << " " << #b << " : " << b << " " << #c      \\
       << " : " << c << " " << #d << " : " << d << "\\n"

#define INF (int)100000000000000000
#define PI (double)3.14159265358979
// #define M 421412341324321
#define MOD 1000000007

int a[500005], c[500005];
vector<int> ans;
int cnt = 0;
int block_size = 800;

struct Query {
  int l, r, idx;
  bool operator<(Query other) const {
    return make_pair(l / block_size, r) <
           make_pair(other.l / block_size, other.r);
  }
};

void remove(int idx) {
  c[a[idx]]--;
  if (c[a[idx]] == 0)
    cnt--;
}
void add(int idx) {
  if (c[a[idx]] == 0)
    cnt++;
  c[a[idx]]++;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, q;
  cin >> n >> q;
  ans.resize(q);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    c[a[i]] = 0;
  }
  vector<Query> queries(q);

  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    queries[i].l = l;
    queries[i].r = r;
    queries[i].idx = i;
  }

  sort(queries.begin(), queries.end());

  int cur_l = 0;
  int cur_r = -1;
  for (Query q : queries) {
    while (cur_l > q.l) {
      cur_l--;
      add(cur_l);
    }
    while (cur_r < q.r) {
      cur_r++;
      add(cur_r);
    }
    while (cur_l < q.l) {
      remove(cur_l);
      cur_l++;
    }
    while (cur_r > q.r) {
      remove(cur_r);
      cur_r--;
    }
    ans[q.idx] = cnt;
  }
  for (auto j : ans)
    cout << j << "\\n";

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define ld long double
// #define int long long

#define pb push_back
#define F first
#define S second
#define I insert
#define sz(a) (int)((a).size())
#define all(a) (a).begin(), (a).end()

#define debug1(a) cout << " " << #a << " : " << a << "\\n"
#define debug2(a, b)                                                           \\
  cout << " " << #a << " : " << a << " " << #b << " : " << b << "\\n"
#define debug3(a, b, c)                                                        \\
  cout << " " << #a << " : " << a << " " << #b << " : " << b << " " << #c      \\
       << " : " << c << "\\n"
#define debug4(a, b, c, d)                                                     \\
  cout << " " << #a << " : " << a << " " << #b << " : " << b << " " << #c      \\
       << " : " << c << " " << #d << " : " << d << "\\n"

#define INF (int)100000000000000000
#define PI (double)3.14159265358979
// #define M 421412341324321
#define MOD 1000000007

int a[500005], c[500005];
vector<int> ans;
int cnt = 0;
int block_size = 800;

struct Query {
  int l, r, idx;
  bool operator<(Query other) const {
    return make_pair(l / block_size, r) <
           make_pair(other.l / block_size, other.r);
  }
};

void remove(int idx) {
  c[a[idx]]--;
  if (c[a[idx]] == 0)
    cnt--;
}
void add(int idx) {
  if (c[a[idx]] == 0)
    cnt++;
  c[a[idx]]++;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, q;
  cin >> n >> q;
  ans.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    c[a[i]] = 0;
  }
  vector<Query> queries(q);

  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    queries[i].l = l;
    queries[i].r = r;
    queries[i].idx = i;
  }

  sort(queries.begin(), queries.end());

  int cur_l = 0;
  int cur_r = -1;
  for (Query q : queries) {
    while (cur_l > q.l) {
      cur_l--;
      add(cur_l);
    }
    while (cur_r < q.r) {
      cur_r++;
      add(cur_r);
    }
    while (cur_l < q.l) {
      remove(cur_l);
      cur_l++;
    }
    while (cur_r > q.r) {
      remove(cur_r);
      cur_r--;
    }
    ans[q.idx] = cnt;
  }
  for (auto j : ans)
    cout << j << "\\n";

  return 0;
}