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

#include <bits/stdc++.h>

using namespace std;
const int N = 200200;
#include <iostream>
#include <map>
using namespace std;

#define N 500500

int t[N * 25];
int L[N * 25];
int R[N * 25];
int f[N];
#define next jklsjgklsjg
#define prev kfjgls
int prev[N];
int next[N];
int a[N];
int next_free = 1;
int n = N;

void build(int v, int tl, int tr) {
  if (tl == tr) {
    t[v] = prev[tl] == 0;
  } else {
    int tm = (tl + tr) >> 1;
    L[v] = ++next_free;
    R[v] = ++next_free;
    build(L[v], tl, tm);
    build(R[v], tm + 1, tr);
    t[v] = t[L[v]] + t[R[v]];
  }
}

void update(int v, int orig_v, int pos, int tl, int tr) {
  if (tl == tr) {
    t[v] = 1;
  } else {
    int tm = (tl + tr) >> 1;
    if (pos <= tm) {
      L[v] = ++next_free;
      R[v] = R[orig_v];
      update(L[v], L[orig_v], pos, tl, tm);
    } else {
      L[v] = L[orig_v];
      R[v] = ++next_free;
      update(R[v], R[orig_v], pos, tm + 1, tr);
    }
    t[v] = t[L[v]] + t[R[v]];
  }
}
int getsum(int l, int r, int v, int tl, int tr) {
  if (l > tr || r < tl)
    return 0;
  if (l <= tl && tr <= r)
    return t[v];
  int tm = (tl + tr) >> 1;
  return getsum(l, r, L[v], tl, tm) + getsum(l, r, R[v], tm + 1, tr);
}
int distinct(int l, int r) { return getsum(l, r, f[l], 1, n); }

void solve() {
  cin >> n;
  int q;
  cin >> q;
  map<int, int> last;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (last[a[i]] == 0) {
      last[a[i]] = i;
    } else {
      prev[i] = last[a[i]];
      next[prev[i]] = i;
      last[a[i]] = i;
    }
  }

  build(0, 1, n);

  for (int i = 2; i <= n; i++) {
    if (next[i - 1] != 0) {
      f[i] = ++next_free;
      update(f[i], f[i - 1], next[i - 1], 1, n);
    } else
      f[i] = f[i - 1];
  }
  int l, r;
  while (q--) {
    cin >> l >> r;
    cout << distinct(l, r) << endl;
  }
}

int main() {
#ifdef QWERTY
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);

  int t = 1;
  //        cin >> t;

  for (int i = 1; i <= t; i++) {
    //                cout << "Case #" << i << ':' << endl;
    solve();
  }

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
const int N = 200200;
#include <iostream>
#include <map>
using namespace std;

#define N 500500

int t[N * 20];
int L[N * 20];
int R[N * 20];
int f[N];
#define next jklsjgklsjg
#define prev kfjgls
int prev[N];
int next[N];
int a[N];
int next_free = 1;
int n = N;

void build(int v, int tl, int tr) {
  if (tl == tr) {
    t[v] = prev[tl] == 0;
  } else {
    int tm = (tl + tr) >> 1;
    L[v] = ++next_free;
    R[v] = ++next_free;
    build(L[v], tl, tm);
    build(R[v], tm + 1, tr);
    t[v] = t[L[v]] + t[R[v]];
  }
}

void update(int v, int orig_v, int pos, int tl, int tr) {
  if (tl == tr) {
    t[v] = 1;
  } else {
    int tm = (tl + tr) >> 1;
    if (pos <= tm) {
      L[v] = ++next_free;
      R[v] = R[orig_v];
      update(L[v], L[orig_v], pos, tl, tm);
    } else {
      L[v] = L[orig_v];
      R[v] = ++next_free;
      update(R[v], R[orig_v], pos, tm + 1, tr);
    }
    t[v] = t[L[v]] + t[R[v]];
  }
}
int getsum(int l, int r, int v, int tl, int tr) {
  if (l > tr || r < tl)
    return 0;
  if (l <= tl && tr <= r)
    return t[v];
  int tm = (tl + tr) >> 1;
  return getsum(l, r, L[v], tl, tm) + getsum(l, r, R[v], tm + 1, tr);
}
int distinct(int l, int r) { return getsum(l, r, f[l], 1, n); }

void solve() {
  cin >> n;
  int q;
  cin >> q;
  map<int, int> last;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (last[a[i]] == 0) {
      last[a[i]] = i;
    } else {
      prev[i] = last[a[i]];
      next[prev[i]] = i;
      last[a[i]] = i;
    }
  }

  build(0, 1, n);

  for (int i = 2; i <= n; i++) {
    if (next[i - 1] != 0) {
      f[i] = ++next_free;
      update(f[i], f[i - 1], next[i - 1], 1, n);
    } else
      f[i] = f[i - 1];
  }
  int l, r;
  while (q--) {
    cin >> l >> r;
    cout << distinct(l, r) << endl;
  }
}

int main() {
#ifdef QWERTY
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);

  int t = 1;
  //        cin >> t;

  for (int i = 1; i <= t; i++) {
    //                cout << "Case #" << i << ':' << endl;
    solve();
  }

  return 0;
}