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

#include <bits/stdc++.h>

using namespace std;

const int maxn = 5 * (int)1e5 + 100;
int n, a[maxn], b[maxn], t[maxn * 70], lt[maxn * 70], rt[maxn * 70],
    tVal[maxn * 70], tot;
int lst[maxn]; // 记录离散化后数组的上次出现位置
int q;

void init() {
  tot = 0;
  memset(lst, -1, sizeof(lst));
}
int build(int l, int r) {
  int node = ++tot;
  tVal[node] = 0;
  int mid = (l + r) >> 1;
  if (l < r) {
    lt[node] = build(l, mid);
    rt[node] = build(mid + 1, r);
  }
  return node;
}
int update(int l, int r, int par, int p, int val) {
  int node = ++tot;
  lt[node] = lt[par];
  rt[node] = rt[par];
  tVal[node] = tVal[par] + val;
  int mid = (l + r) >> 1;
  if (l < r) {
    if (p <= mid)
      lt[node] = update(l, mid, lt[par], p, val);
    else
      rt[node] = update(mid + 1, r, rt[par], p, val);
  }
  return node;
}
int query(int l, int r, int p, int node) {
  if (l == r)
    return tVal[node];
  int mid = (l + r) >> 1;
  if (p <= mid)
    return (query(l, mid, p, lt[node]) + tVal[rt[node]]);
  else
    return query(mid + 1, r, p, rt[node]);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
#ifdef LOCAL_DEFINE
  freopen("input.txt", "r", stdin);
#endif

  init();
  cin >> n >> q;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b + 1, b + 1 + n);
  int len = unique(b + 1, b + 1 + n) - b;
  for (int i = 1; i <= n; ++i)
    a[i] = lower_bound(b + 1, b + 1 + len, a[i]) - b;

  t[0] = build(1, n);
  for (int i = 1; i <= n; ++i) {
    if (lst[a[i]] == -1)
      t[i] = update(1, n, t[i - 1], i, 1);
    else {
      int temp = update(1, n, t[i - 1], lst[a[i]], -1);
      t[i] = update(1, n, temp, i, 1);
    }
    lst[a[i]] = i;
  }
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << query(1, n, l, t[r]) << '\\n';
  }

#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\\n";
#endif
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int maxn = 3 * (int)1e4 + 100;
int n, a[maxn], b[maxn], t[maxn * 40], lt[maxn * 40], rt[maxn * 40],
    tVal[maxn * 40], tot;
int lst[maxn]; // 记录离散化后数组的上次出现位置
int q;

void init() {
  tot = 0;
  memset(lst, -1, sizeof(lst));
}
int build(int l, int r) {
  int node = ++tot;
  tVal[node] = 0;
  int mid = (l + r) >> 1;
  if (l < r) {
    lt[node] = build(l, mid);
    rt[node] = build(mid + 1, r);
  }
  return node;
}
int update(int l, int r, int par, int p, int val) {
  int node = ++tot;
  lt[node] = lt[par];
  rt[node] = rt[par];
  tVal[node] = tVal[par] + val;
  int mid = (l + r) >> 1;
  if (l < r) {
    if (p <= mid)
      lt[node] = update(l, mid, lt[par], p, val);
    else
      rt[node] = update(mid + 1, r, rt[par], p, val);
  }
  return node;
}
int query(int l, int r, int p, int node) {
  if (l == r)
    return tVal[node];
  int mid = (l + r) >> 1;
  if (p <= mid)
    return (query(l, mid, p, lt[node]) + tVal[rt[node]]);
  else
    return query(mid + 1, r, p, rt[node]);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
#ifdef LOCAL_DEFINE
  freopen("input.txt", "r", stdin);
#endif

  init();
  cin >> n >> q;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b + 1, b + 1 + n);
  int len = unique(b + 1, b + 1 + n) - b;
  for (int i = 1; i <= n; ++i)
    a[i] = lower_bound(b + 1, b + 1 + len, a[i]) - b;

  t[0] = build(1, n);
  for (int i = 1; i <= n; ++i) {
    if (lst[a[i]] == -1)
      t[i] = update(1, n, t[i - 1], i, 1);
    else {
      int temp = update(1, n, t[i - 1], lst[a[i]], -1);
      t[i] = update(1, n, temp, i, 1);
    }
    lst[a[i]] = i;
  }
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << query(1, n, l, t[r]) << '\\n';
  }

#ifdef LOCAL_DEFINE
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\\n";
#endif
  return 0;
}