Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define fst first
#define sc second
#define snd second
#define sz(X) (int)(X).size()
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define fornr(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define forab(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(X) X.begin(), X.end()

typedef long long ll;
typedef vector<pair<ll, ll>> vpll;
typedef pair<int, int> pii;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef double dbl;
typedef long double ld;
typedef vector<int> vi;

const int M = 1e6 + 10;
const int N = 30000;
const int INF = 1e9;
int pos[M], a[N], nxt[N];

vi t[4 * N];

void build(int v, int tl, int tr) {
  if (tl == tr)
    t[v].resize(1, nxt[tl]);
  else {
    int tm = (tl + tr) / 2;
    build(2 * v, tl, tm);
    build(2 * v + 1, tm + 1, tr);
    merge(all(t[2 * v]), all(t[2 * v + 1]), back_inserter(t[v]));
  }
}

int count_less(int v, int tl, int tr, int l, int r, int x) {
  if (l <= tl && tr <= r)
    return (int)(upper_bound(all(t[v]), x) - t[v].begin());
  if (tr < l || r < tl)
    return 0;
  int tm = (tl + tr) / 2;
  return count_less(2 * v, tl, tm, l, r, x) +
         count_less(2 * v + 1, tm + 1, tr, l, r, x);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, q;
  cin >> n >> q;
  forn(i, n) cin >> a[i];
  fill(pos, pos + M, INF);
  fornr(i, n) nxt[i] = pos[a[i]], pos[a[i]] = i;
  build(1, 0, n - 1);
  while (q--) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    cout << (r - l + 1) - count_less(1, 0, n - 1, l, r, r) << "\n";
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define fst first
#define sc second
#define snd second
#define sz(X) (int)(X).size()
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define fornr(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define forab(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(X) X.begin(), X.end()

typedef long long ll;
typedef vector<pair<ll, ll>> vpll;
typedef pair<int, int> pii;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef double dbl;
typedef long double ld;
typedef vector<int> vi;

const int M = 1e6 + 10;
const int N = 1e6;
const int INF = 1e9;
int pos[M], a[N], nxt[N];

vi t[4 * N];

void build(int v, int tl, int tr) {
  if (tl == tr)
    t[v].resize(1, nxt[tl]);
  else {
    int tm = (tl + tr) / 2;
    build(2 * v, tl, tm);
    build(2 * v + 1, tm + 1, tr);
    merge(all(t[2 * v]), all(t[2 * v + 1]), back_inserter(t[v]));
  }
}

int count_less(int v, int tl, int tr, int l, int r, int x) {
  if (l <= tl && tr <= r)
    return (int)(upper_bound(all(t[v]), x) - t[v].begin());
  if (tr < l || r < tl)
    return 0;
  int tm = (tl + tr) / 2;
  return count_less(2 * v, tl, tm, l, r, x) +
         count_less(2 * v + 1, tm + 1, tr, l, r, x);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, q;
  cin >> n >> q;
  forn(i, n) cin >> a[i];
  fill(pos, pos + M, INF);
  fornr(i, n) nxt[i] = pos[a[i]], pos[a[i]] = i;
  build(1, 0, n - 1);
  while (q--) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    cout << (r - l + 1) - count_less(1, 0, n - 1, l, r, r) << "\n";
  }
  return 0;
}