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

#include <bits/stdc++.h>
#define in inline
#define re register
using namespace std;
in int read() {
  re int t = 0;
  re char v = getchar();
  while (v < '0' || v > '9')
    v = getchar();
  while (v >= '0' && v <= '9') {
    t = (t << 3) + (t << 1) + (v ^ 48);
    v = getchar();
  }
  return t;
}
int n, m, rt[200002], head[200002], a[200002], ls[20000002], rs[20000002],
    ans[200002], u, v, cnt, tot, s[20000002], la, r[200002], val[20000002];
struct edge {
  int to, next;
} e[400002];
inline void bu(re int x, re int y) {
  e[++tot].to = y;
  e[tot].next = head[x];
  head[x] = tot;
}
inline int insert(re int v, re int l, re int r, re int x, re int vv) {
  int y = ++cnt;
  s[y] = s[x] + 1;
  val[y] = max(val[x], vv);
  if (l == r) {
    val[y] = max(val[y], vv);
    return y;
  }
  ls[y] = ls[x];
  rs[y] = rs[x];
  int mid = (l + r) >> 1;
  if (v <= mid)
    ls[y] = insert(v, l, mid, ls[x], vv);
  else
    rs[y] = insert(v, mid + 1, r, rs[x], vv);
  return y;
}
inline int find(re int p, re int l, re int r, re int x) {
  if (!p)
    return 0;
  if (r <= x)
    return val[p];
  re int mid = l + r >> 1;
  if (x > mid)
    return max(find(ls[p], l, mid, x), find(rs[p], mid + 1, r, x));
  else
    return find(ls[p], l, mid, x);
}
inline void dfs(re int x, re int y) {
  ans[x] = find(rt[y], 1, 1e9 + 1, a[x]);
  rt[x] = insert(a[x] + 1, 1, 1e9 + 1, rt[y], ans[x] + 1);
  ans[x] = max(ans[x], ans[y]);
  for (re int i = head[x]; i; i = e[i].next)
    if (e[i].to != y)
      dfs(e[i].to, x);
}
int main() {
  n = read();
  for (re int i = 1; i <= n; ++i)
    a[i] = read();
  for (re int i = 1; i < n; ++i) {
    u = read();
    v = read();
    bu(u, v);
    bu(v, u);
  }
  dfs(1, 1);
  for (re int i = 1; i <= n; ++i)
    printf("%d\\n", ans[i] + 1);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define in inline
#define re register
using namespace std;
in int read() {
  re int t = 0;
  re char v = getchar();
  while (v < '0' || v > '9')
    v = getchar();
  while (v >= '0' && v <= '9') {
    t = (t << 3) + (t << 1) + (v ^ 48);
    v = getchar();
  }
  return t;
}
int n, m, rt[200002], head[1000002], a[1000002], ls[2000002], rs[2000002],
    ans[100002], u, v, cnt, tot, s[2000002], la, r[100002], val[2000002];
struct edge {
  int to, next;
} e[200002];
inline void bu(re int x, re int y) {
  e[++tot].to = y;
  e[tot].next = head[x];
  head[x] = tot;
}
inline int insert(re int v, re int l, re int r, re int x, re int vv) {
  int y = ++cnt;
  s[y] = s[x] + 1;
  if (l == r) {
    val[y] = vv;
    return y;
  }
  ls[y] = ls[x];
  rs[y] = rs[x];
  int mid = (l + r) >> 1;
  if (v <= mid)
    ls[y] = insert(v, l, mid, ls[x], vv);
  else
    rs[y] = insert(v, mid + 1, r, rs[x], vv);
  val[y] = max(val[ls[y]], val[rs[y]]);
  return y;
}
inline int find(re int p, re int l, re int r, re int x) {
  if (!p)
    return 0;
  if (r <= x)
    return val[p];
  re int mid = l + r >> 1;
  if (x > mid)
    return max(find(ls[p], l, mid, x), find(rs[p], mid + 1, r, x));
  else
    return find(ls[p], l, mid, x);
}
inline void dfs(re int x, re int y) {
  ans[x] = find(rt[y], 1, n, a[x]) + 1;
  rt[x] = insert(a[x], 1, n, rt[y], ans[x]);
  for (re int i = head[x]; i; i = e[i].next)
    if (e[i].to != y)
      dfs(e[i].to, x);
}
int main() {
  n = read();
  for (re int i = 1; i <= n; ++i) {
    a[i] = read();
    r[i] = a[i];
  }
  sort(r + 1, r + n + 1);
  int tmp = unique(r + 1, r + n + 1) - r - 1;
  for (re int i = 1; i <= n; ++i) {
    a[i] = lower_bound(r + 1, r + tmp + 1, a[i]) - r;
  }
  for (re int i = 1; i < n; ++i) {
    u = read();
    v = read();
    bu(u, v);
    bu(v, u);
  }
  dfs(1, 0);
  for (re int i = 1; i <= n; ++i)
    printf("%d\\n", ans[i]);
}