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

#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>

using ll = long long int;
const int INF = (1 << 30);
const ll INFLL = (1ll << 60);
const ll MOD = (ll)(1e9 + 7);

#define l_ength size

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

void add_mod(ll &a, ll b) {
  a = (a < MOD) ? a : (a - MOD);
  b = (b < MOD) ? b : (b - MOD);
  a += b;
  a = (a < MOD) ? a : (a - MOD);
}

int n;
int a[225816], segtree[816225], ans[225816];
std::vector<int> g[225816], x;

void update(int p, int v, int i = 0, int l = 0, int r = n) {
  if (!(l <= p && p < r)) {
    return;
  } else if (r - l > 1) {
    update(p, v, i * 2 + 1, l, (l + r) / 2);
    update(p, v, i * 2 + 2, (l + r) / 2, r);
    segtree[i] = std::max(segtree[i * 2 + 1], segtree[i * 2 + 2]);
  } else {
    segtree[i] = v;
  }
}

int query(int p, int q, int i = 0, int l = 0, int r = n) {
  if (r <= p || q <= l) {
    return 0;
  } else if (p <= l && r <= q) {
    return segtree[i];
  } else {
    return std::max(query(p, q, i * 2 + 1, l, (l + r) / 2),
                    query(p, q, i * 2 + 2, (l + r) / 2, r));
  }
}

void solve(int v, int p) {
  int i, q, before, u;
  q = (int)(lower_bound(x.begin(), x.end(), a[v]) - x.begin());
  before = query(q, q + 1);
  ans[v] = query(0, q) + 1;
  update(q, std::max(ans[v], before));
  for (i = (g[v].l_ength() - 1); i >= 0; --i) {
    u = g[v][i];
    if (u == p) {
      continue;
    }
    solve(u, v);
  }
  update(q, before);
}

void solve2(int v, int p) {
  int i, u;
  for (i = (g[v].l_ength() - 1); i >= 0; --i) {
    u = g[v][i];
    if (u == p) {
      continue;
    }
    ans[u] = std::max(ans[v], ans[u]);
    solve2(u, v);
  }
}

int main(void) {
  int u, v, i;
  std::cin >> n;
  for (i = 0; i < n; ++i) {
    std::cin >> a[i];
    x.push_back(a[i]);
  }
  std::sort(x.begin(), x.end());
  for (i = 1; i < n; ++i) {
    std::cin >> u >> v;
    --u;
    --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  solve(0, -1);
  solve2(0, -1);
  for (i = 0; i < n; ++i) {
    std::cout << ans[i] << std::endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>

using ll = long long int;
const int INF = (1 << 30);
const ll INFLL = (1ll << 60);
const ll MOD = (ll)(1e9 + 7);

#define l_ength size

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

void add_mod(ll &a, ll b) {
  a = (a < MOD) ? a : (a - MOD);
  b = (b < MOD) ? b : (b - MOD);
  a += b;
  a = (a < MOD) ? a : (a - MOD);
}

int n;
int a[225816], segtree[225816], ans[225816];
std::vector<int> g[225816], x;

void update(int p, int v, int i = 0, int l = 0, int r = n) {
  if (!(l <= p && p < r)) {
    return;
  } else if (r - l > 1) {
    update(p, v, i * 2 + 1, l, (l + r) / 2);
    update(p, v, i * 2 + 2, (l + r) / 2, r);
    segtree[i] = std::max(segtree[i * 2 + 1], segtree[i * 2 + 2]);
  } else {
    segtree[i] = v;
  }
}

int query(int p, int q, int i = 0, int l = 0, int r = n) {
  if (r <= p || q <= l) {
    return 0;
  } else if (p <= l && r <= q) {
    return segtree[i];
  } else {
    return std::max(query(p, q, i * 2 + 1, l, (l + r) / 2),
                    query(p, q, i * 2 + 2, (l + r) / 2, r));
  }
}

void solve(int v, int p) {
  int i, q, before, u;
  q = (int)(lower_bound(x.begin(), x.end(), a[v]) - x.begin());
  before = query(q, q + 1);
  ans[v] = query(0, q) + 1;
  update(q, std::max(ans[v], before));
  for (i = (g[v].l_ength() - 1); i >= 0; --i) {
    u = g[v][i];
    if (u == p) {
      continue;
    }
    solve(u, v);
  }
  update(q, before);
}

int main(void) {
  int u, v, i;
  std::cin >> n;
  for (i = 0; i < n; ++i) {
    std::cin >> a[i];
    x.push_back(a[i]);
  }
  std::sort(x.begin(), x.end());
  for (i = 1; i < n; ++i) {
    std::cin >> u >> v;
    --u;
    --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  solve(0, -1);
  for (i = 0; i < n; ++i) {
    std::cout << ans[i] << std::endl;
  }
  return 0;
}