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

#include <bits/stdc++.h>

using namespace std;

struct point {
  int left, right, block, id;
};

bool cmp(const point &lhs, const point &rhs) {
  if (lhs.block < rhs.block)
    return true;
  else if (lhs.block > rhs.block)
    return false;
  else
    return lhs.right < rhs.right;
}

int main() {
  int n, q;
  scanf("%d", &n);
  scanf("%d", &q);
  vector<int> A(n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);

  int block_size = sqrt(n);

  vector<point> query(q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &query[i].left, &query[i].right);
    query[i].left--, query[i].right--;
    query[i].block = query[i].left / block_size;
    query[i].id = i;
  }

  sort(query.begin(), query.end(), cmp);

  vector<int> solution(q);
  vector<int> hash(1000100);

  int left = 0, right = 0, answer = 1;
  hash[A[0]] = 1;

  for (const point &task : query) {
    while (left < task.left) {
      hash[A[left]]--;
      answer -= (hash[A[left]] == 0);
      left++;
    }
    while (left > task.left) {
      left--;
      hash[A[left]]++;
      answer += (hash[A[left]] == 1);
    }
    while (right < task.right) {
      right++;
      hash[A[right]]++;
      answer += (hash[A[right]] == 1);
    }
    while (right > task.right) {
      hash[A[right]]--;
      answer -= (hash[A[right]] == 0);
      right--;
    }
    solution[task.id] = answer;
  }

  for (int answer : solution)
    printf("%d\\n", answer);

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <vector>

using namespace std;

#define ARRAY_SIZE(arr) sizeof(arr) / sizeof(arr[0])
#define INT_MIN -2147483647
#define INT_MAX 2147483647
#define INF 2000000000
#define INF_LL 9223372036854775807LL
#define PI acos(-1.0)
#define llu long long unsigned
#define ll long long int
#define ld long int
#define iter(i, a) for (typeof(a.begin()) i = a.begin(); i != a.end(); i++)
#define REP(p, a, b) for (int p = a; p < b; p++)
#define mod 1000000007
#define getchar_unlocked getchar
#define pb(f) push_back(f)
#define pob(f) pop_back(f)
#define pf(f) push_front(f)
#define pof(f) pop_front(f)
#define mkp(a, b) make_pair(a, b)
#define fst first
#define snd second
#define pii pair<int, int>
#define ins(a) insert(a)
#define N 30010
#define A 1000010
#define BLOCK 174
#define M 200010

int answer;
struct query {
  int l, r, ind;
} Q[M];
int ans[M];
int a[N];
int cnt[A];

void add(int index) {
  cnt[a[index]]++;
  if (cnt[a[index]] == 1)
    answer++;
}

void remove(int index) {
  cnt[a[index]]--;
  if (cnt[a[index]] == 0)
    answer--;
}

bool cmp(query f, query s) {
  if ((f.l / BLOCK) != (s.l / BLOCK))
    return (f.l / BLOCK) < (s.l / BLOCK);
  else
    return f.r < s.r;
}

int main() {
  ios_base::sync_with_stdio(false);
  int n, q;
  scanf("%d  %d", &n, &q);
  REP(i, 0, n) { scanf("%d", &a[i]); }
  REP(i, 0, q) {
    scanf("%d%d", &Q[i].l, &Q[i].r);
    Q[i].ind = i;
    Q[i].l--;
    Q[i].r--;
  }
  sort(Q, Q + q, cmp);
  int cl = 0, cr = 0;
  REP(i, 0, q) {
    int left = Q[i].l, right = Q[i].r;
    while (cl < left)
      remove(cl), cl++;
    while (cl > left)
      add(cl - 1), cl--;
    while (cr <= right)
      add(cr), cr++;
    while (cr > (right + 1))
      remove(cr - 1), cr--;
    ans[Q[i].ind] = answer;
  }
  REP(i, 0, q)
  printf("%d\\n", ans[i]);
  return 0;
}