Problem p02698 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp>     //required

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef vector<ll> VL;
typedef vector<int> VI;
typedef pair<ll, ll> PLL;
typedef pair<int, int> PII;
#define pb push_back

#define F first
#define S second
#define ALL(a) a.begin(), a.end()
#define fr(i, x, y) for (ll i = x; i < y; i++)
#define frr(i, x, y) for (ll i = x - 1; i >= y; i--)
#define inf 1e18 + 1
const ll mod = 998244353;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
VL arr, ans, d, a;
vector<VL> adj;

void dfs(ll v, ll p) {

  int j = lower_bound(ALL(d), a.back()) - d.begin();
  PLL tmp = {-1, -1};
  if (d[j - 1] < a.back() && a.back() < d[j]) {
    tmp = {j, d[j]};
    d[j] = a.back();
  }
  // ll ct=-1;
  // fr(i,0,d.size())
  // {
  //     if(d[i]<inf)
  //     {
  //         ct=i;
  //     }
  // }
  ans[v] = lower_bound(ALL(d), inf) - d.begin() - 1;
  for (auto u : adj[v]) {
    if (u != p) {
      d.pb(inf);
      a.pb(arr[u]);
      dfs(u, v);
      d.pop_back();
      a.pop_back();
    }
  }
  if (tmp.F != -1) {
    d[tmp.F] = tmp.S;
  }
  return;
}
void solve() {
  ll n;
  cin >> n;
  arr.assign(n, 0);
  adj.assign(n, VL());
  ans.assign(n, 0);
  fr(i, 0, n) cin >> arr[i];
  fr(i, 0, n - 1) {
    ll x, y;
    cin >> x >> y;
    x--, y--;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  // VL d,a;
  d.pb(-inf);
  d.pb(inf);
  a.pb(arr[0]);
  dfs(0, -1);
  d.pop_back();
  a.pop_back();
  fr(i, 0, n) { cout << ans[i] << "\\n"; }
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp>     //required

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef int ll;
typedef vector<ll> VL;
typedef vector<int> VI;
typedef pair<ll, ll> PLL;
typedef pair<int, int> PII;
#define pb push_back

#define F first
#define S second
#define ALL(a) a.begin(), a.end()
#define fr(i, x, y) for (ll i = x; i < y; i++)
#define frr(i, x, y) for (ll i = x - 1; i >= y; i--)
#define inf 10000000000000000
const ll mod = 998244353;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
VL arr, ans, d, a;
vector<VL> adj;

void dfs(ll v, ll p) {

  ll sz = a.size();
  fr(i, 0, sz - 1) {
    if (a[i] < a[sz - 1]) {
      d[sz - 1] = max(d[sz - 1], d[i] + 1);
    }
  }
  ans[v] = *max_element(ALL(d));
  for (auto u : adj[v]) {
    if (u != p) {
      d.pb(1LL);
      a.pb(arr[u]);
      dfs(u, v);
      d.pop_back();
      a.pop_back();
    }
  }
  return;
}
void solve() {
  ll n;
  cin >> n;
  arr.assign(n, 0);
  adj.assign(n, VL());
  ans.assign(n, 0);
  fr(i, 0, n) cin >> arr[i];
  fr(i, 0, n - 1) {
    ll x, y;
    cin >> x >> y;
    x--, y--;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  // VL d,a;
  d.pb(1LL);
  a.pb(arr[0]);
  dfs(0, -1);
  d.pop_back();
  a.pop_back();
  fr(i, 0, n) { cout << ans[i] << "\\n"; }
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}