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

#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"

#define int long long
#define double long double
#define PI 3.141592653589793
#define FAST                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

// Use for file I/O;
#define FIN                                                                    \\
  string _fname = "paintbarn";                                                 \\
  string _is = _fname + ".in", _os = _fname + ".out";                          \\
  freopen(_is.c_str(), "r", stdin);                                            \\
  freopen(_os.c_str(), "w", stdout);

using namespace std;

const int INF = 4e18 + 10;
const int INF2 = 2e18 + 10;
const int INF3 = 2e10;
const int MAXN = 500001;
const int LOGN = 19;
const int MOD = 1e9 + 7;
const int EPS = 1e-9;
const int MULT = 2e5;
const int MCNST = MOD * MOD * 8;

int a[MAXN];
int cnt[MAXN];
int ans = 0, n, m;

void remove(int idx) {
  // cout << idx << " REMOVE\\n";
  int t = a[idx];
  cnt[t]--;
  if (cnt[t] == 0)
    ans--;
}

void add(int idx) {
  // cout << idx << " ADD\\n";
  int t = a[idx];
  if (cnt[t] == 0)
    ans++;
  cnt[t]++;
}

int get_answer() { return ans; }

const int block_size = 710;

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);
  }
};

vector<int> mo_s_algorithm(vector<Query> queries) {
  vector<int> answers(queries.size());
  sort(queries.begin(), queries.end());

  for (int i = 0; i < MAXN; i++) {
    cnt[i] = 0;
  }

  int cur_l = 0;
  int cur_r = -1;
  // invariant: data structure will always reflect the range [cur_l, cur_r]
  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--;
    }
    // cout << q.l << ' ' << q.r << ' ' << ans << endl << endl;
    answers[q.idx] = get_answer();
  }
  return answers;
}

signed main() {
  cin >> n;
  cin >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  vector<Query> q(m);
  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    q[i] = {l - 1, r - 1, i};
  }

  for (int x : mo_s_algorithm(q)) {
    cout << x << '\\n';
  }
}

Error: 0

Fixed submission:

#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"

#define int long long
#define double long double
#define PI 3.141592653589793
#define FAST                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

// Use for file I/O;
#define FIN                                                                    \\
  string _fname = "paintbarn";                                                 \\
  string _is = _fname + ".in", _os = _fname + ".out";                          \\
  freopen(_is.c_str(), "r", stdin);                                            \\
  freopen(_os.c_str(), "w", stdout);

using namespace std;

const int INF = 4e18 + 10;
const int INF2 = 2e18 + 10;
const int INF3 = 2e10;
const int MAXN = 400001;
const int LOGN = 19;
const int MOD = 1e9 + 7;
const int EPS = 1e-9;
const int MULT = 2e5;
const int MCNST = MOD * MOD * 8;

int a[MAXN];
int cnt[MAXN + 1];
int ans = 0, n, m;

void remove(int idx) {
  // cout << idx << " REMOVE\\n";
  int t = a[idx];
  cnt[t]--;
  if (cnt[t] == 0)
    ans--;
}

void add(int idx) {
  // cout << idx << " ADD\\n";
  int t = a[idx];
  if (cnt[t] == 0)
    ans++;
  cnt[t]++;
}

int get_answer() { return ans; }

const int block_size = 740;

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);
  }
};

vector<int> mo_s_algorithm(vector<Query> queries) {
  vector<int> answers(queries.size());
  sort(queries.begin(), queries.end());

  for (int i = 0; i < MAXN; i++) {
    cnt[i] = 0;
  }

  int cur_l = 0;
  int cur_r = -1;
  // invariant: data structure will always reflect the range [cur_l, cur_r]
  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--;
    }
    // cout << q.l << ' ' << q.r << ' ' << ans << endl << endl;
    answers[q.idx] = get_answer();
  }
  return answers;
}

signed main() {
  cin >> n;
  cin >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  vector<Query> q(m);
  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    q[i] = {l - 1, r - 1, i};
  }

  for (int x : mo_s_algorithm(q)) {
    cout << x << '\\n';
  }
}