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

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int(i) = 1; (i) <= (n); (i++))
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define yn                                                                     \\
  { puts("Yes"); }                                                             \\
  else {                                                                       \\
    puts("No");                                                                \\
  }
#define printd(x) printf("%.12f\\n", x);
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using ll = long long;

const int MAX_N = 200005;

vector<vector<int>> g(MAX_N);
vector<int> ans(MAX_N, -1);
vector<int> a(MAX_N);
vector<int> dp(MAX_N, INT_MAX);

void dfs(int s, int p = -1) {
  int id = lower_bound(rng(dp), a[s]) - dp.begin();
  int old = dp[id];
  chmin(dp[id], a[s]);
  ans[s] = int(lower_bound(rng(dp), INT_MAX) - dp.begin()) - 1;
  for (int v : g[s]) {
    if (v == p)
      continue;
    dfs(v, s);
  }
  dp[id] = old;
}

int main() {
  int n;
  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);
  }

  dp[0] = -INT_MAX;

  dfs(0);

  rep(i, n) cout << ans[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int(i) = 1; (i) <= (n); (i++))
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define yn                                                                     \\
  { puts("Yes"); }                                                             \\
  else {                                                                       \\
    puts("No");                                                                \\
  }
#define printd(x) printf("%.12f\\n", x);
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using ll = long long;

vector<vector<int>> g(200005);
vector<int> ans(200005, -1);
vector<int> a(200005);
bool visited[200005];

void dfs(int s, vector<int> &vec, int lis) {
  vector<int> copy = vec;
  auto lb = lower_bound(rng(copy), a[s]);
  chmin(copy[lb - copy.begin()], a[s]);
  chmax(lis, (int)(lb - copy.begin()));
  ans[s] = lis;
  visited[s] = true;
  for (int v : g[s]) {
    if (visited[v])
      continue;
    dfs(v, copy, lis);
  }
}

int main() {
  int n;
  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);
  }

  vector<int> v(n + 1, INT_MAX);
  v[0] = -INT_MAX;
  dfs(0, v, 1);

  rep(i, n) cout << ans[i] << endl;
  return 0;
}