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

/*** 𝕒𝕜𝕤𝕙𝕒𝕪𝕜𝕦𝕞𝕒𝕣𝟡𝟡 ***/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using id_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define f(i, x, n) for (i = x; i < n; i++)
#define fr(i, x, n) for (i = x; i >= n; i--)
#define pu push_back
#define pp pop_back
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define SP(x) fixed << setprecision(x)
#define IOS()                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

const long long MOD = 1e9 + 7;
const long double PI = acos(-1);
const long long INF = INT_MAX;
const long long LINF = LLONG_MAX;
const long long N = 2e5 + 1;

typedef long long ll;
typedef long double ld;

ll n, length = 1, a[N], res[N];
vector<ll> v[N], seq, tail(N, INF);

void dfs(ll s, ll p) {
  ll val, ind = -1, mx = length;
  if (a[s] < tail[0]) {
    val = tail[0];
    ind = 0;
    tail[0] = a[s];
  } else if (a[s] > tail[length - 1])
    tail[length++] = a[s];
  else {
    ind = lower_bound(tail.begin(), tail.begin() + length, a[s]) - tail.begin();
    // ind = b_search(tail, 0, length, a[s]);
    if (ind == length)
      length++;
    val = tail[ind];
    tail[ind] = a[s];
  }
  res[s] = length;

  for (auto u : v[s])
    if (u != p)
      dfs(u, s);

  length = mx;
  if (ind != -1)
    tail[ind] = val;
}
void test_case() {
  ll i, j;
  cin >> n;
  f(i, 1, n + 1) { cin >> a[i]; }
  f(i, 0, n) {
    ll x, y;
    cin >> x >> y;
    v[x].pu(y);
    v[y].pu(x);
  }
  tail[0] = a[1];
  dfs(1, 0);
  f(i, 1, n + 1) { cout << res[i] << '\\n'; }
}

int32_t main() {
  IOS();

  int tc = 1;
  // cin>>tc;
  while (tc--)
    test_case();
  return 0;
}


Error: TLE

Fixed submission:

/*** 𝕒𝕜𝕤𝕙𝕒𝕪𝕜𝕦𝕞𝕒𝕣𝟡𝟡 ***/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using id_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define f(i, x, n) for (i = x; i < n; i++)
#define fr(i, x, n) for (i = x; i >= n; i--)
#define pu push_back
#define pp pop_back
#define F first
#define S second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define SP(x) fixed << setprecision(x)
#define IOS()                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

const long long MOD = 1e9 + 7;
const long double PI = acos(-1);
const long long INF = INT_MAX;
const long long LINF = LLONG_MAX;
const long long N = 2e5 + 1;

typedef long long ll;
typedef long double ld;

ll n, length = 1, a[N], res[N];
vector<ll> v[N], seq, tail(N, INF);

ll b_search(vector<ll> v, ll l, ll r, ll val) {
  while (l < r) {
    ll mid = (l + r) >> 1;
    if (v[mid] >= val)
      r = mid;
    else
      l = mid + 1;
  }
  return r;
}

void dfs(ll s, ll p) {
  ll val, ind = -1, mx = length;
  // if(a[s] < tail[0])
  // {
  //     val=tail[0];
  //     ind=0;
  //     tail[0] = a[s];
  // }
  // else if(a[s] > tail[length - 1])
  //     tail[length++] = a[s];
  // else
  {
    // ind = lower_bound(tail.begin(), tail.begin()+length, a[s]) -
    // tail.begin();
    ind = b_search(tail, 0, length, a[s]);
    if (ind == length)
      length++;
    val = tail[ind];
    tail[ind] = a[s];
  }
  res[s] = length;

  for (auto u : v[s])
    if (u != p)
      dfs(u, s);

  length = mx;
  if (ind != -1)
    tail[ind] = val;
}
void test_case() {
  ll i, j;
  cin >> n;
  f(i, 1, n + 1) { cin >> a[i]; }
  f(i, 0, n) {
    ll x, y;
    cin >> x >> y;
    v[x].pu(y);
    v[y].pu(x);
  }
  tail[0] = a[1];
  dfs(1, 0);
  f(i, 1, n + 1) { cout << res[i] << '\\n'; }
}

int32_t main() {
  IOS();

  int tc = 1;
  // cin>>tc;
  while (tc--)
    test_case();
  return 0;
}