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

#include <iostream>
#include <vector>
#define pb push_back
using namespace std;

int A[200005], res[200005];
vector<int> adj[200005];
int lis[200005];
int bin_search(int sz, int v) {
  int l = 0, r = sz - 1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    int prev = lis[mid];
    int next = lis[mid + 1];
    if (prev < v && v <= next)
      return mid + 1;
    if (next < v)
      l = mid + 1;
    else
      r = mid - 1;
  }
}
void solve(int node, int sz, int parent) {
  res[node] = sz;
  for (int i = 0; i < (int)adj[node].size(); i++) {
    int next = adj[node][i];
    if (next == parent)
      continue;
    if (lis[0] >= A[next - 1]) {
      int v = lis[0];
      lis[0] = A[next - 1];
      solve(next, sz, node);
      lis[0] = v;
    } else if (lis[sz - 1] < A[next - 1]) {
      lis[sz] = A[next - 1];
      solve(next, sz + 1, node);
      lis[sz] = 0;
    } else {
      int pos = bin_search(sz, A[next - 1]);
      int v = lis[pos];
      lis[pos] = A[next - 1];
      solve(next, sz, node);
      lis[pos] = v;
    }
  }
}
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < N - 1; i++) {
    int n1, n2;
    cin >> n1 >> n2;
    adj[n1].pb(n2);
    adj[n2].pb(n1);
  }
  lis[0] = A[0];
  solve(1, 1, -1);
  for (int i = 1; i <= N; i++) {
    cout << res[i] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <vector>
#define pb push_back
using namespace std;
struct vertex {
  vertex *l, *r;
  int v;
  vertex(vertex *l, vertex *r) : l(l), r(r), v(0) {}
  vertex(int sum) : l(nullptr), r(nullptr), v(sum) {}
};
int A[200005], res[200005];
vertex *build(int l, int r) {
  if (l == r)
    return new vertex(0);
  int mid = (l + r) >> 1;
  return new vertex(build(l, mid), build(mid + 1, r));
}
vertex *update(vertex *node, int l, int r, int pos, int v) {
  if (l == r)
    return new vertex(v);
  int mid = (l + r) >> 1;
  if (mid <= pos)
    return new vertex(update(node->l, l, mid, pos, v), node->r);
  else
    return new vertex(node->l, update(node->r, mid + 1, r, pos, v));
}
int query(vertex *node, int l, int r, int pos) {
  if (l == r)
    return node->v;
  int mid = (l + r) >> 1;
  if (mid <= pos)
    return query(node->l, l, mid, pos);
  else
    return query(node->r, mid + 1, r, pos);
}
vector<int> adj[200005];
vertex *save[200005];
int bin_search(int sz, vertex *segtree, int v, int N) {
  int l = 0, r = sz - 1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    int prev = query(segtree, 0, N, mid);
    int next = query(segtree, 0, N, mid + 1);
    if (prev < v && v <= next)
      return mid + 1;
    if (next < v)
      l = mid + 1;
    else
      r = mid - 1;
  }
}
int solve(int node, vertex *segtree, int sz, int parent, int N) {
  res[node] = sz;
  for (int i = 0; i < (int)adj[node].size(); i++) {
    int next = adj[node][i];
    if (next == parent)
      continue;
    int first = query(segtree, 0, N, 0);
    int last = query(segtree, 0, N, sz - 1);
    int new_sz = sz;
    vertex *next_state;
    if (first >= A[next - 1]) {
      next_state = update(segtree, 0, N, 0, A[next - 1]);
    } else if (last < A[next - 1]) {
      next_state = update(segtree, 0, N, sz, A[next - 1]);
      new_sz++;
    } else {
      int pos = bin_search(sz, segtree, A[next - 1], N);
      next_state = update(segtree, 0, N, pos, A[next - 1]);
    }
    solve(next, next_state, new_sz, node, N);
  }
}
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  for (int i = 0; i < N - 1; i++) {
    int n1, n2;
    cin >> n1 >> n2;
    adj[n1].pb(n2);
    adj[n2].pb(n1);
  }
  vertex *start = build(0, N);
  start = update(start, 0, N, 0, A[0]);
  solve(1, start, 1, -1, N);
  for (int i = 1; i <= N; i++) {
    cout << res[i] << endl;
  }
  return 0;
}