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

/**
  If comrades that you trust gather around you, hope can take physical form and
  become visible. That’s what I believe.
  */
#include <bits/stdc++.h>
using namespace std;
/**
  (▀̿Ĺ̯▀̿ ̿) (ง ͠° ͟ل͜ ͡°)ง
  */
const int N = 2e5 + 10;
int f[N], a[N], ans[N];
vector<int> g[N];
int n;
auto dfs(int idx, int p = -1) -> void {
  int pos = lower_bound(f, f + n, a[idx]) - f;
  int pp = f[pos];
  f[pos] = a[idx];
  ans[idx] = lower_bound(f, f + n, 1 << 30) - f;
  for (auto v : g[idx]) {
    if (v == p)
      continue;
    dfs(v, idx);
  }
  f[pos] = pp;
}
auto main() -> int {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  vector<int> tt;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    tt.emplace_back(a[i]);
  }
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].emplace_back(v);
    g[v].emplace_back(u);
  }
  fill(f, f + n, 1 << 30);
  dfs(1);
  for (int i = 1; i <= n; ++i)
    cout << ans[i] << '\\n';

  return 0;
}


Error: 0

Fixed submission:

/**
  If comrades that you trust gather around you, hope can take physical form and
  become visible. That’s what I believe.
  */
#include <bits/stdc++.h>
using namespace std;
/**
  (▀̿Ĺ̯▀̿ ̿) (ง ͠° ͟ل͜ ͡°)ง
  */
const int N = 1e5 + 7;
int lazy[N << 2];
int t[N << 2];
int combine(int lhs, int rhs) { return max(lhs, rhs); }

auto build(int v, int l, int r) -> void {
  if (l == r) {
    t[v] = 1;
    return;
  }
  int mid = (l + r) >> 1;
  build(v << 1, l, mid);
  build(v << 1 | 1, mid + 1, r);
  t[v] = combine(t[v << 1], t[v << 1 | 1]);
}
auto push(int v, int l, int r) -> void {
  if (lazy[v] != -1) {
    t[v] = lazy[v];
    if (l < r) {
      lazy[v << 1] = lazy[v];
      lazy[(v << 1) ^ 1] = lazy[v];
    }
  }
  lazy[v] = -1;
}
void update(int v, int l, int r, int x, int y, int to_add) {
  push(v, l, r);
  if (l > y || r < x)
    return;
  if (l >= x && r <= y) {
    lazy[v] = to_add;
    push(v, l, r);
    return;
  }
  int mid = (l + r) >> 1;
  update(v << 1, l, mid, x, y, to_add);
  update((v << 1) ^ 1, mid + 1, r, x, y, to_add);
  t[v] = combine(t[v << 1], t[(v << 1) ^ 1]);
}

int get(int v, int l, int r, int x, int y) {
  if (l > y || r < x)
    return -1;
  push(v, l, r);
  if (l >= x && r <= y)
    return t[v];
  int mid = (l + r) >> 1;
  return combine(get(v << 1, l, mid, x, y),
                 get((v << 1) ^ 1, mid + 1, r, x, y));
}

int a[N];
int ans[N];
vector<int> g[N];
int n;
map<int, int> to_val;
auto dfs(int idx, int p = -1) -> void {
  auto nxt = to_val[a[idx]];
  if (idx == 1)
    ans[idx] = 1;
  else
    ans[idx] = get(1, 1, n + 10, 1, nxt - 1) + 1;
  update(1, 1, n + 10, nxt, nxt, ans[idx]);
  for (auto &v : g[idx]) {
    if (v == p)
      continue;
    dfs(v, idx);
  }
  update(1, 1, n + 10, nxt, nxt, 1);
}
auto main() -> int {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  vector<int> tt;
  memset(lazy, -1, sizeof lazy);
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    tt.emplace_back(a[i]);
  }
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].emplace_back(v);
    g[v].emplace_back(u);
  }
  sort(tt.begin(), tt.end());
  tt.erase(unique(tt.begin(), tt.end()), tt.end());
  for (int i = 0; i < tt.size(); ++i) {
    to_val[tt[i]] = i + 3;
  }
  build(1, 1, n + 10);
  dfs(1);
  for (int i = 1; i <= n; ++i)
    cout << ans[i] << '\\n';

  return 0;
}