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

#include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////
const long long int INF = 1LL << 60;
const long long int Mod = 1000000007;
using ll = long long int;
using ci = const int;
using vi = vector<int>;
using Vi = vector<long long int>;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define all(x) (x).begin(), (x).end()
#define rp(i, N) for (ll i = 0; i < (ll)N; i++)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
template <class T> bool chmax(T &former, const T &b) {
  if (former < b) {
    former = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &former, const T &b) {
  if (b < former) {
    former = b;
    return true;
  }
  return false;
}
template <class T> T sqar(T x) { return x * x; } // sqrt(x)は平方根;
#define Sort(v)                                                                \\
  std::sort(v.begin(), v.end(),                                                \\
            std::greater<decltype(v[0])>()) // 降順でVをソート
#define p_queue(v) priority_queue<v, vector<v>, greater<v>>
template <class T> inline void princ(T x) { cout << x << " "; };
template <class T> inline void print(T x) { cout << x << "\\n"; };
template <class T> inline void Yes(T condition) {
  if (condition)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
template <class T> inline void YES(T condition) {
  if (condition)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
///////////////////////////////////////////////////////////////////////////////////

matrix tr(200050, Vi(0));
vector<ll> tour;
void dfs(int v, int p) {
  tour.pb(v);
  for (auto u : tr[v]) {
    if (u != p)
      dfs(u, v);
  }
  tour.pb(-v);
}
void solve() {
  ll n;
  cin >> n;
  Vi a(n + 1);
  Vi where(n + 1);
  repi(i, 1, n + 1) cin >> a.at(i);
  tr[0].pb(1);
  tr[1].pb(0);
  rp(i, n - 1) {
    ll u, v;
    cin >> u >> v;
    tr[u].pb(v);
    tr[v].pb(u);
  }
  dfs(1, 0);
  vector<stack<ll>> dp(n + 1);
  vector<ll> ans(n + 1);
  rp(i, n + 1) dp[i].push(INF);
  for (auto v : tour) {
    if (v > 0) {
      ll ok = n, ng = 0;
      ll mid = (ok + ng) / 2;
      if (dp[0].top() >= a[v]) {
        dp[0].push(a[v]);
        where[v] = 0;
      } else {
        while (ok - ng > 1) {
          if (dp[mid].top() >= a[v])
            ok = mid;
          else
            ng = mid;
          mid = (ok + ng) / 2;
        }
        dp[ok].push(a[v]);
        where[v] = ok;
      }
      ok = 0;
      ng = n;
      mid = n / 2;
      while (ng - ok > 1) {
        if (dp[mid].top() == INF)
          ng = mid;
        else
          ok = mid;
        mid = (ok + ng) / 2;
      }
      ans[v] = ok + 1;
    } else {
      v = -v;
      dp[where[v]].pop();
    }
  }
  repi(i, 1, n + 1) { print(ans[i]); }
  return;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(30);
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////
const long long int INF = 1LL << 60;
const long long int Mod = 1000000007;
using ll = long long int;
using ci = const int;
using vi = vector<int>;
using Vi = vector<long long int>;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define all(x) (x).begin(), (x).end()
#define rp(i, N) for (ll i = 0; i < (ll)N; i++)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
template <class T> bool chmax(T &former, const T &b) {
  if (former < b) {
    former = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &former, const T &b) {
  if (b < former) {
    former = b;
    return true;
  }
  return false;
}
template <class T> T sqar(T x) { return x * x; } // sqrt(x)は平方根;
#define Sort(v)                                                                \\
  std::sort(v.begin(), v.end(),                                                \\
            std::greater<decltype(v[0])>()) // 降順でVをソート
#define p_queue(v) priority_queue<v, vector<v>, greater<v>>
template <class T> inline void princ(T x) { cout << x << " "; };
template <class T> inline void print(T x) { cout << x << "\\n"; };
template <class T> inline void Yes(T condition) {
  if (condition)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
template <class T> inline void YES(T condition) {
  if (condition)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
///////////////////////////////////////////////////////////////////////////////////

int check[200050];
void solve() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> ans(n + 1);
  rp(i, n) cin >> a.at(i);
  matrix gra(n, Vi(0));
  matrix path(n, Vi(0));
  rp(i, n - 1) {
    ll u, v;
    cin >> u >> v;
    u--;
    v--;
    gra[u].pb(v);
    gra[v].pb(u);
  }
  queue<PLL> que;
  path[0].pb(0);
  for (auto l : gra[0]) {
    path[l] = path[0];
    path[l].pb(l);
    que.push(mp(l, 0));
  }

  while (!que.empty()) {
    PLL pl = que.front();
    que.pop();
    for (auto ve : gra[pl.first]) {
      if (ve == pl.second)
        continue;
      check[pl.first] = 1;
      path[ve] = path[pl.first];
      path[ve].pb(ve);
      que.push(mp(ve, pl.first));
    }
  }

  rp(i, n) {
    if (check[i] == 1)
      continue;
    int len = path[i].size();
    ll dp[len];
    fill(dp, dp + (len), INF);
    rp(j, len) {
      *lower_bound(dp, dp + len, a[path[i][j]]) = a[path[i][j]];
      ans[path[i][j]] = lower_bound(dp, dp + len, INF) - dp;
    }
  }

  rp(i, n) { print(ans[i]); }
  return;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(30);
  solve();
  return 0;
}