Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repA(i, a, b) for (int i = a; i <= b; i++)
#define repD(i, b, a) for (int i = b; i >= a; i--)
#define fill(a, val) memset(a, val, sizeof(a))
#define f first
#define s second
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
int block;
const int maxN = 1e5 + 5;
int vis[maxN];
bool cmp(pair<int, pii> &a, pair<int, pii> &b) {
  if (a.s.f / block == b.s.f / block)
    return a.s.s < b.s.s;
  return a.s.f / block < b.s.f / block;
}
int main() {
  int n, q;
  cin >> n >> q;
  int a[n];
  rep(i, 0, n) cin >> a[i];
  block = sqrt(n);
  pair<int, pii> p[q];
  rep(i, 0, q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    p[i].f = i;
    p[i].s.f = l;
    p[i].s.s = r;
  }
  sort(p, p + q, cmp);
  int answer = 0;
  int curl = 0, curr = 0;
  fill(vis, 0);
  int res[q];
  rep(i, 0, q) {
    int pos = p[i].f;
    int l = p[i].s.f;
    int r = p[i].s.s;
    while (curl < l) {
      vis[a[curl]]--;
      if (vis[a[curl]] == 0)
        answer--;
      curl++;
    }
    while (curl > l) {
      vis[a[curl - 1]]++;
      if (vis[a[curl - 1]] == 1)
        answer++;
      curl--;
    }
    while (curr <= r) {
      vis[a[curr]]++;
      if (vis[a[curr]] == 1)
        answer++;
      curr++;
    }
    while (curr > r + 1) {
      vis[a[curr - 1]]--;
      if (vis[a[curr - 1]] == 0)
        answer--;
      curr--;
    }
    res[pos] = answer;
  }
  rep(i, 0, q) cout << res[i] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repA(i, a, b) for (int i = a; i <= b; i++)
#define repD(i, b, a) for (int i = b; i >= a; i--)
#define fill(a, val) memset(a, val, sizeof(a))
#define f first
#define s second
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
int block;
const int maxN = 5e5 + 5;
int vis[maxN];
bool cmp(pair<int, pii> &a, pair<int, pii> &b) {
  if (a.s.f / block == b.s.f / block)
    return a.s.s < b.s.s;
  return a.s.f / block < b.s.f / block;
}
int main() {
  int n, q;
  cin >> n >> q;
  int a[n];
  rep(i, 0, n) cin >> a[i];
  block = sqrt(n);
  pair<int, pii> p[q];
  rep(i, 0, q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    p[i].f = i;
    p[i].s.f = l;
    p[i].s.s = r;
  }
  sort(p, p + q, cmp);
  int answer = 0;
  int curl = 0, curr = 0;
  fill(vis, 0);
  int res[q];
  rep(i, 0, q) {
    int pos = p[i].f;
    int l = p[i].s.f;
    int r = p[i].s.s;
    while (curl < l) {
      vis[a[curl]]--;
      if (vis[a[curl]] == 0)
        answer--;
      curl++;
    }
    while (curl > l) {
      vis[a[curl - 1]]++;
      if (vis[a[curl - 1]] == 1)
        answer++;
      curl--;
    }
    while (curr <= r) {
      vis[a[curr]]++;
      if (vis[a[curr]] == 1)
        answer++;
      curr++;
    }
    while (curr > r + 1) {
      vis[a[curr - 1]]--;
      if (vis[a[curr - 1]] == 0)
        answer--;
      curr--;
    }
    res[pos] = answer;
  }
  rep(i, 0, q) cout << res[i] << endl;
}