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

#include <bits/stdc++.h>
using namespace std;

const int N = (int)5e5 + 5;
const int LogN = (int)22;

int L[4 * N * LogN];
int R[N * 4 * LogN];
int T[N * 4 * LogN];
int version;
int root[N * 4 * LogN];
int src[N];
unordered_map<int, int> pos;

void build(int i, int b, int e) {
  if (b == e) {
    T[i] = 0;
    return;
  }
  L[i] = ++version;
  R[i] = ++version;
  int mid = (b + e) >> 1;
  build(L[i], b, mid);
  build(R[i], mid + 1, e);
}

int update(int i, int b, int e, int pos, int v) {
  if (b > pos or e < pos)
    return i;
  int cur = ++version;
  if (b == e) {
    T[cur] = v;
    return cur;
  }
  int mid = (b + e) >> 1;
  L[cur] = update(L[i], b, mid, pos, v);
  R[cur] = update(R[i], mid + 1, e, pos, v);
  T[cur] = T[L[cur]] + T[R[cur]];
  return cur;
}

int query(int i, int b, int e, int l, int r) {
  if (e < l or b > r)
    return 0;
  if (l <= b and e <= r)
    return T[i];
  int mid = (b + e) >> 1;
  return query(L[i], b, mid, l, r) + query(R[i], mid + 1, e, l, r);
}

int main() {
  // cout << N * 4 * LogN<< '\\n' ;
  int test = 1, tc = 0;
  // scanf("%d", &test) ;
  while (test--) {
    int n, q;
    scanf("%d %d", &n, &q);
    // memset(L, 0, sizeof L) ;
    // memset(R, 0, sizeof R) ;
    // memset(T, 0, sizeof T) ;
    // memset(root, 0, sizeof root) ;
    // memset(src, 0, sizeof src) ;
    // pos.clear() ;
    version = 0;
    int cnt = 0;
    root[0] = 0;
    build(0, 1, n);
    for (int i = 1; i <= n; ++i) {
      int x;
      scanf("%d", &x);
      int k = pos[x];
      if (k) {
        ++cnt;
        root[cnt] = update(root[cnt - 1], 1, n, k, 0);
      }
      ++cnt;
      root[cnt] = update(root[cnt - 1], 1, n, i, 1);
      pos[x] = i;
      src[i] = cnt;
    }
    // printf("Case %d:\\n", ++tc);
    while (q--) {
      int l, r;
      scanf("%d %d", &l, &r);
      int res = query(root[src[r]], 1, n, l, r);
      printf("%d\\n", res);
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int N = (int)1e5 + 5;
const int LogN = (int)20;

int L[4 * N * LogN];
int R[N * 4 * LogN];
int T[N * 4 * LogN];
int version;
int root[N * 4 * LogN];
int src[N];
unordered_map<int, int> pos;

void build(int i, int b, int e) {
  if (b == e) {
    T[i] = 0;
    return;
  }
  L[i] = ++version;
  R[i] = ++version;
  int mid = (b + e) >> 1;
  build(L[i], b, mid);
  build(R[i], mid + 1, e);
}

int update(int i, int b, int e, int pos, int v) {
  if (b > pos or e < pos)
    return i;
  int cur = ++version;
  if (b == e) {
    T[cur] = v;
    return cur;
  }
  int mid = (b + e) >> 1;
  L[cur] = update(L[i], b, mid, pos, v);
  R[cur] = update(R[i], mid + 1, e, pos, v);
  T[cur] = T[L[cur]] + T[R[cur]];
  return cur;
}

int query(int i, int b, int e, int l, int r) {
  if (e < l or b > r)
    return 0;
  if (l <= b and e <= r)
    return T[i];
  int mid = (b + e) >> 1;
  return query(L[i], b, mid, l, r) + query(R[i], mid + 1, e, l, r);
}

int main() {
  // cout << N * 4 * LogN<< '\\n' ;
  int test = 1, tc = 0;
  // scanf("%d", &test) ;
  while (test--) {
    int n, q;
    scanf("%d %d", &n, &q);
    // memset(L, 0, sizeof L) ;
    // memset(R, 0, sizeof R) ;
    // memset(T, 0, sizeof T) ;
    // memset(root, 0, sizeof root) ;
    // memset(src, 0, sizeof src) ;
    // pos.clear() ;
    version = 0;
    int cnt = 0;
    root[0] = 0;
    build(0, 1, n);
    for (int i = 1; i <= n; ++i) {
      int x;
      scanf("%d", &x);
      int k = pos[x];
      if (k) {
        ++cnt;
        root[cnt] = update(root[cnt - 1], 1, n, k, 0);
      }
      ++cnt;
      root[cnt] = update(root[cnt - 1], 1, n, i, 1);
      pos[x] = i;
      src[i] = cnt;
    }
    // printf("Case %d:\\n", ++tc);
    while (q--) {
      int l, r;
      scanf("%d %d", &l, &r);
      int res = query(root[src[r]], 1, n, l, r);
      printf("%d\\n", res);
    }
  }
  return 0;
}