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

#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
using i64 = int64_t;
using u64 = uint64_t;

#define CPP_STR(x) CPP_STR_I(x)
#define CPP_CAT(x, y) CPP_CAT_I(x, y)
#define CPP_STR_I(args...) #args
#define CPP_CAT_I(x, y) x##y

constexpr int64_t MOD = 1'000'000'007LL;
const int INF = 1001001001;

#define INT(NAME)                                                              \\
  int NAME;                                                                    \\
  cin >> NAME;
#define INT64(NAME)                                                            \\
  int64_t NAME;                                                                \\
  cin >> NAME;
#define VECTOR(NAME, TYPE, SIZE)                                               \\
  std::vector<TYPE> NAME((SIZE));                                              \\
  for (int xxxx_i = 0; xxxx_i < SIZE; xxxx_i++)                                \\
    cin >> NAME[xxxx_i];
#define VECTOR_CIN(NAME, SIZE)                                                 \\
  (NAME).resize(SIZE);                                                         \\
  for (int xxxx_i = 0; xxxx_i < SIZE; xxxx_i++)                                \\
    cin >> NAME[xxxx_i];
#define FOR(i, start, end)                                                     \\
  for (i64 i = (start), CPP_CAT(i, xxxx_end) = (end);                          \\
       i < CPP_CAT(i, xxxx_end); ++i)
#define REP(i, n) FOR(i, 0, n)

vector<int> A, ans;
vector<vector<int>> tree;
vector<int> dp;

void dfs(int u, int par = -1) {
  int pos = lower_bound(dp.begin(), dp.end(), A[u]) - dp.begin();
  int old = dp[pos];
  dp[pos] = A[u];
  ans[u] = int(lower_bound(dp.begin(), dp.end(), INF) - dp.begin()) - 1;
  for (int v : tree[u]) {
    if (v == par)
      continue;
    dfs(v, u);
  }
  dp[pos] = old;
}

void solve() {
  INT(N)
  VECTOR_CIN(A, N);
  tree.resize(N);
  REP(i, N - 1) {
    INT(u);
    INT(v);
    u--;
    v--;
    tree[u].push_back(v);
    tree[v].push_back(u);
  }

  ans.resize(N);
  dp.resize(N + 1);
  fill(dp.begin(), dp.end(), INF);
  dp[0] = -INF;

  dfs(0);
  REP(i, N) cout << ans[i] << endl;
}

int main() {
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
using i64 = int64_t;
using u64 = uint64_t;

#define CPP_STR(x) CPP_STR_I(x)
#define CPP_CAT(x, y) CPP_CAT_I(x, y)
#define CPP_STR_I(args...) #args
#define CPP_CAT_I(x, y) x##y

constexpr int64_t MOD = 1'000'000'007LL;

#define INT(NAME)                                                              \\
  int NAME;                                                                    \\
  cin >> NAME;
#define INT64(NAME)                                                            \\
  int64_t NAME;                                                                \\
  cin >> NAME;
#define VECTOR(NAME, TYPE, SIZE)                                               \\
  std::vector<TYPE> NAME((SIZE));                                              \\
  for (int xxxx_i = 0; xxxx_i < SIZE; xxxx_i++)                                \\
    cin >> NAME[xxxx_i];
#define FOR(i, start, end)                                                     \\
  for (i64 i = (start), CPP_CAT(i, xxxx_end) = (end);                          \\
       i < CPP_CAT(i, xxxx_end); ++i)
#define REP(i, n) FOR(i, 0, n)

void dfs(int u, int par, vector<int> *dp, vector<size_t> *ans,
         const vector<int> &A, const vector<vector<int>> &tree) {
  if (A[u] > dp->back()) {
    dp->push_back(A[u]);
  } else {
    auto it = lower_bound(dp->begin(), dp->end(), A[u]);
    *it = A[u];
  }
  ans->at(u) = max(ans->at(u), dp->size());

  for (int v : tree[u]) {
    if (v != par) {
      dfs(v, u, dp, ans, A, tree);
      auto val = dp->back();
      dp->pop_back();
      if (val < A[u])
        dp->push_back(A[u]);
    }
  }
}

void solve() {
  INT(N)
  VECTOR(A, int, N)
  vector<vector<int>> tree(N);
  REP(i, N - 1) {
    INT(u);
    INT(v);
    u--;
    v--;
    tree[u].push_back(v);
    tree[v].push_back(u);
  }

  vector<size_t> ans(N);
  fill(ans.begin(), ans.end(), 1);
  vector<int> dp{A[0]};
  dfs(0, -1, &dp, &ans, A, tree);
  REP(i, N) cout << ans[i] << endl;
}

int main() {
  solve();
  return 0;
}