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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace __gnu_pbds;
using namespace std;
typedef tree<pair<int, int>, null_type, less_equal<pair<int, int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
#define int long long
#define double long double
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define lb(v, z) lower_bound(all(v), z)
#define ub(v, z) upper_bound(all(v), z)
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define rdp(i, a, b) for (int i = (a); i >= (b); i--)
#define rlt(i, a, b) for (int i = (a); i < (b); i++)
#define cns(z) cout << (z) << ' '
#define cnl(z) cout << (z) << '\\n'
#define M1 1000000007
#define M2 998244353
#define MAXN 300005
#define INF (1ll << 60)
#define endl "\\n"
#define garr(ip)                                                               \\
  for (auto &x : ip)                                                           \\
    cin >> x;
#define parr(ip)                                                               \\
  for (auto &x : ip)                                                           \\
    cout << x << " ";                                                          \\
  cout << endl;
#define nl cout << endl
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vp;

vi a, ans;
vector<vi> g;
vi dq;

void dfs(int s, int p = 0) {
  auto it = lb(dq, a[s]);
  bool flag = false;
  int num = 0;
  if (it == dq.end()) {
    dq.push_back(a[s]);
  } else {
    flag = true;
    num = *it;
    *it = a[s];
  }
  ans[s] = dq.size();
  for (auto it : g[s])
    if (it != p)
      dfs(it, s);
  it = lb(dq, a[s]);
  if (flag)
    *it = num;
  else
    dq.pop_back();
}

void solve(int testCaseNumber = 1) {
  int n;
  cin >> n;
  a = vi(n + 1);
  ans = vi(n + 1);
  g = vector<vi>(n + 1);
  rep(i, 1, n) cin >> a[i];
  rlt(i, 1, n) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1);
  rep(i, 1, n) cnl(ans[i]);
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  solve();
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace __gnu_pbds;
using namespace std;
typedef tree<pair<int, int>, null_type, less_equal<pair<int, int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
#define int long long
#define double long double
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define lb(v, z) lower_bound(all(v), z)
#define ub(v, z) upper_bound(all(v), z)
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define rdp(i, a, b) for (int i = (a); i >= (b); i--)
#define rlt(i, a, b) for (int i = (a); i < (b); i++)
#define cns(z) cout << (z) << ' '
#define cnl(z) cout << (z) << '\\n'
#define M1 1000000007
#define M2 998244353
#define MAXN 300005
#define INF (1ll << 60)
#define endl "\\n"
#define garr(ip)                                                               \\
  for (auto &x : ip)                                                           \\
    cin >> x;
#define parr(ip)                                                               \\
  for (auto &x : ip)                                                           \\
    cout << x << " ";                                                          \\
  cout << endl;
#define nl cout << endl
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vp;

vi a, ans;
vector<vi> g;
vi dq;

void dfs(int s, int p = 0) {
  auto it = ub(dq, a[s]);
  bool flag = false;
  int num = 0;
  if (it == dq.end()) {
    dq.push_back(a[s]);
  } else {
    flag = true;
    num = *it;
    *it = a[s];
  }
  ans[s] = dq.size();
  for (auto it : g[s])
    if (it != p)
      dfs(it, s);
  if (flag)
    *it = num;
  else
    dq.pop_back();
}

void solve(int testCaseNumber = 1) {
  int n;
  cin >> n;
  a = vi(n + 1);
  ans = vi(n + 1);
  g = vector<vi>(n + 1);
  rep(i, 1, n) cin >> a[i];
  rlt(i, 1, n) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1);
  rep(i, 1, n) cnl(ans[i]);
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  solve();
}