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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007;
// const ll mod = 998244353;
const int inf = 1e9 + 10;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int n;
vector<int> a(200200), G[200200];

vector<int> dp(200200, inf), ans(200200);
vector<bool> seen(200200, false);
int res = 0;
stack<pair<int, int>> s;

void dfs(int v = 0) {
  if (seen[v])
    return;
  seen[v] = true;
  if (res == 0) {
    dp[res] = a[v];
    s.push({res, inf});
    res++;
  } else {
    if (dp[res - 1] < a[v]) {
      dp[res] = a[v];
      s.push({res, inf});
      res++;
    } else {
      int idx = lower_bound(dp.begin(), dp.end(), a[v]) - dp.begin();
      s.push({idx, dp[idx]});
      dp[idx] = a[v];
    }
  }
  ans[v] = res;

  for (int u : G[v]) {
    dfs(u);
  }

  pair<int, int> p = s.top();
  s.pop();
  dp[p.first] = p.second;
  if (p.second == inf)
    res--;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(25);

  cin >> n;
  rep(i, n) cin >> a[i];
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }

  dfs();
  rep(i, n) cout << ans[i] << '\\n';

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007;
// const ll mod = 998244353;
const int inf = 1e9 + 10;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

vector<int> a, G[200200], res[200200];

void dfs(int v = 0, int p = -1) {
  for (int u : G[v]) {
    if (u == p)
      continue;
    res[u] = res[v];
    if (res[u][res[u].size() - 1] < a[u]) {
      res[u].push_back(a[u]);
    } else {
      *lower_bound(res[u].begin(), res[u].end(), a[u]) = a[u];
    }
    dfs(u, v);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(25);

  int n;
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i];
  rep(i, n - 1) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }

  res[0].push_back(a[0]);
  dfs();
  rep(i, n) cout << res[i].size() << '\\n';

  return 0;
}