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

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define mem(x) memset(x, 0, sizeof(x))
#define sca(x)                                                                 \\
  if (scanf("%d", &x)) {                                                       \\
  }
#define scaa(x, y)                                                             \\
  if (scanf("%d%d", &x, &y)) {                                                 \\
  }
#define prt(x) printf("%d\\n", x)
#define inf 0x7f7f7f7fconst int maxn = 1e5 + 5;
using namespace std;
const int maxn = 9e5 + 1000;
int n, m;
struct Tree {
  int ls, rs, val; // 左右儿子的编号, 和维护的一个值.
} tre[maxn * 40];
int idx = 0, root[maxn];
int build(int l, int r) {
  int nod = ++idx;
  tre[nod].val = 0;
  if (l == r)
    return nod;
  int mid = (l + r) >> 1;
  tre[nod].ls = build(l, mid);
  tre[nod].rs = build(mid + 1, r);
  return nod;
}
int update(int pre, int l, int r, int pos, int v) {
  int nod = ++idx;
  tre[nod] = tre[pre];
  tre[nod].val += v;
  if (l == r)
    return nod;
  int mid = (l + r) >> 1;
  if (pos <= mid)
    tre[nod].ls = update(tre[pre].ls, l, mid, pos, v);
  else
    tre[nod].rs = update(tre[pre].rs, mid + 1, r, pos, v);
  return nod;
}
int query(int id, int l, int r, int pos) {
  if (l == r)
    return tre[id].val;
  int mid = (l + r) >> 1;
  if (pos <= mid) {
    return tre[tre[id].rs].val + query(tre[id].ls, l, mid, pos);
  } else
    return query(tre[id].rs, mid + 1, r, pos);
}
int a[maxn], vis[maxn * 10];
int main() {
  scaa(n, m);
  for (int i = 1; i <= n; i++) {
    sca(a[i]);
  }
  root[0] = build(1, n); // 根节点
  for (int i = 1; i <= n; i++) {
    if (!vis[a[i]])
      root[i] = update(root[i - 1], 1, n, i, 1);
    else {
      int t = update(root[i - 1], 1, n, vis[a[i]], -1);
      root[i] = update(t, 1, n, i, 1);
    }
    vis[a[i]] = i;
  }
  while (m--) {
    int l, r;
    scaa(l, r);
    int tmp = query(root[r], 1, n, l);
    printf("%d\\n", tmp);
  }
}


Error: -11

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#define mem(x) memset(x, 0, sizeof(x))
#define sca(x)                                                                 \\
  if (scanf("%d", &x)) {                                                       \\
  }
#define scaa(x, y)                                                             \\
  if (scanf("%d%d", &x, &y)) {                                                 \\
  }
#define prt(x) printf("%d\\n", x)
#define inf 0x7f7f7f7fconst int maxn = 1e5 + 5;
using namespace std;
const int maxn = 5e5 + 1000;
int n, m;
struct Tree {
  int ls, rs, val; // 左右儿子的编号, 和维护的一个值.
} tre[maxn * 40];
int idx = 0, root[maxn];
int build(int l, int r) {
  int nod = ++idx;
  tre[nod].val = 0;
  if (l == r)
    return nod;
  int mid = (l + r) >> 1;
  tre[nod].ls = build(l, mid);
  tre[nod].rs = build(mid + 1, r);
  return nod;
}
int update(int pre, int l, int r, int pos, int v) {
  int nod = ++idx;
  tre[nod] = tre[pre];
  tre[nod].val += v;
  if (l == r)
    return nod;
  int mid = (l + r) >> 1;
  if (pos <= mid)
    tre[nod].ls = update(tre[pre].ls, l, mid, pos, v);
  else
    tre[nod].rs = update(tre[pre].rs, mid + 1, r, pos, v);
  return nod;
}
int query(int id, int l, int r, int pos) {
  if (l == r)
    return tre[id].val;
  int mid = (l + r) >> 1;
  if (pos <= mid) {
    return tre[tre[id].rs].val + query(tre[id].ls, l, mid, pos);
  } else
    return query(tre[id].rs, mid + 1, r, pos);
}
int a[maxn], vis[maxn * 10];
int main() {
  scaa(n, m);
  for (int i = 1; i <= n; i++) {
    sca(a[i]);
  }
  root[0] = build(1, n); // 根节点
  for (int i = 1; i <= n; i++) {
    if (!vis[a[i]])
      root[i] = update(root[i - 1], 1, n, i, 1);
    else {
      int t = update(root[i - 1], 1, n, vis[a[i]], -1);
      root[i] = update(t, 1, n, i, 1);
    }
    vis[a[i]] = i;
  }
  while (m--) {
    int l, r;
    scaa(l, r);
    int tmp = query(root[r], 1, n, l);
    printf("%d\\n", tmp);
  }
}