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

// By Mohit Gupta
#include <bits/stdc++.h>
using namespace std;
// DEBUG
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define printint(v) copy(begin(v), end(v), ostream_iterator<int>(cout, " "))
#define printintv(s, e) copy(s, e, ostream_iterator<int>(cout, " "))
#define forstl(i, v) for (auto &i : v)
#define all(v) (v).begin(), (v).end()
#define forn(i, e) for (int i = 0; i < e; i++)
#define forsn(i, s, e) for (int i = s; i < e; i++)
#define rforn(i, s) for (int i = s; i >= 0; i--)
#define rforsn(i, s, e) for (int i = s; i >= e; i--)
#define clz(a) __builtin_clz(a) // count leading zeroes
#define ctz(a) __builtin_ctz(a) // count trailing zeroes
#define popc(a)                                                                \\
  __builtin_popcount(a) // count set bits (for ints only diff for ll)
// https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html for other in built
#define ln "\\n"
#define dbg(x) cout << #x << " = " << x << ln
#define pb push_back
#define fi first
#define se second
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
typedef long long ll;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<double, double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int>> vv32;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
typedef map<int, int> m32;
#pragma GCC optimize("-O2")
const int LIM = 2e5 + 5, MOD = 998244353;
int t, n, m, k, x, y;

v32 adj[LIM];
int a[LIM];
int ans[LIM];

void dfs(int u, int par, v32 &v) {
  int len = v.size();
  int i;
  int prev;
  if (len == 0) {
    v.pb(a[u]);
    i = 0;
  } else {
    i = lower_bound(v.begin(), v.end(), a[u]) - v.begin();
    if (i == len)
      v.pb(a[u]);
    else
      prev = v[i], v[i] = a[u];
  }
  ans[u] = v.size();
  // if(u==10) printint(v);
  for (auto nxt : adj[u]) {
    if (nxt != par)
      dfs(nxt, u, v);
  }

  // restore the vector
  if (i == len)
    v.pop_back();
  else
    v[i] = prev;
}

int32_t main() {
  IOS;
  cin >> n;
  forn(i, n) { cin >> a[i + 1]; }
  forn(i, n - 1) {
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  v32 v;
  dfs(1, 0, v);
  forsn(i, 1, n + 1) cout << ans[i] << ln;
}


Error: TLE

Fixed submission:

// By Mohit Gupta
#include <bits/stdc++.h>
using namespace std;
// DEBUG
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define printint(v) copy(begin(v), end(v), ostream_iterator<int>(cout, " "))
#define printintv(s, e) copy(s, e, ostream_iterator<int>(cout, " "))
#define forstl(i, v) for (auto &i : v)
#define all(v) (v).begin(), (v).end()
#define forn(i, e) for (int i = 0; i < e; i++)
#define forsn(i, s, e) for (int i = s; i < e; i++)
#define rforn(i, s) for (int i = s; i >= 0; i--)
#define rforsn(i, s, e) for (int i = s; i >= e; i--)
#define clz(a) __builtin_clz(a) // count leading zeroes
#define ctz(a) __builtin_ctz(a) // count trailing zeroes
#define popc(a)                                                                \\
  __builtin_popcount(a) // count set bits (for ints only diff for ll)
// https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html for other in built
#define ln "\\n"
#define dbg(x) cout << #x << " = " << x << ln
#define pb push_back
#define fi first
#define se second
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
typedef long long ll;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<double, double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int>> vv32;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
typedef map<int, int> m32;
#pragma GCC optimize("-O2")
const int LIM = 2e5 + 5, MOD = 998244353;
int t, n, m, k, x, y;

v32 adj[LIM];
int a[LIM];
int ans[LIM];

void dfs(int u, int par, v32 v) {
  int len = v.size();
  if (len == 0) {
    v.pb(a[u]);
  } else {
    int i = lower_bound(v.begin(), v.end(), a[u]) - v.begin();
    if (i == len)
      v.pb(a[u]);
    else
      v[i] = a[u];
  }
  ans[u] = v.size();
  // if(u==10) printint(v);
  for (auto nxt : adj[u]) {
    if (nxt != par)
      dfs(nxt, u, v);
  }
}

int32_t main() {
  IOS;
  cin >> n;
  forn(i, n) { cin >> a[i + 1]; }
  forn(i, n - 1) {
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  v32 v;
  dfs(1, 0, v);
  forsn(i, 1, n + 1) cout << ans[i] << ln;
}