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

#include <bits/stdc++.h>
#define _                                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
const int inf = 2e9;
const int mod = (int)1e9 + 7;
using namespace std;
#if LOCAL
#include "../../tr.h"
#else
#define tr(...)
#define endl '\\n'
#endif
template <class T> void smin(T &a, T val) {
  if (a > val)
    a = val;
}
template <class T> void smax(T &a, T val) {
  if (a < val)
    a = val;
}
const int N = 5 * (int)1e5 + 10;

int n, a[N];
vector<int> adj[N];
vector<int> ans;

void dfs(int s, int p, vector<int> &v) {
  auto it = lower_bound(v.begin(), v.end(), a[s]);
  int changed = 0;
  if (it != v.end()) {
    changed = *it;
    *it = a[s];
  } else {
    v.push_back(a[s]);
  }
  ans[s] = lower_bound(v.begin(), v.end(), inf) - v.begin() - 1;
  for (auto i : adj[s]) {
    if (i != p) {
      dfs(i, s, v);
    }
  }
  if (changed) {
    *it = changed;
  } else {
    v.pop_back();
  }
}

void solve() {
  cin >> n;

  ans.resize(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i + 1 < n; ++i) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    swap(x, y);
    adj[x].push_back(y);
  }

  vector<int> x(n + 1, inf);
  x[0] = 0;
  dfs(1, -1, x);
  for (int i = 1; i <= n; ++i) {
    cout << ans[i] << "\\n";
  }
}

int32_t main() {
  _

      int t = 1;
  // cin >> t;

  while (t--) {
    solve();
    cout << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define _                                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
const int inf = 2e9;
const int mod = (int)1e9 + 7;
using namespace std;
#if LOCAL
#include "../../tr.h"
#else
#define tr(...)
#define endl '\\n'
#endif
template <class T> void smin(T &a, T val) {
  if (a > val)
    a = val;
}
template <class T> void smax(T &a, T val) {
  if (a < val)
    a = val;
}
const int N = 5 * (int)1e5 + 10;

int n, a[N];
vector<int> adj[N];
vector<int> ans;

void dfs(int s, int p, vector<int> v) {
  auto it = lower_bound(v.begin(), v.end(), a[s]);
  int changed = 0;
  if (it != v.end()) {
    changed = *it;
    *it = a[s];
  } else {
    v.push_back(a[s]);
  }
  ans[s] = (int)(v.size());
  for (auto i : adj[s]) {
    if (i != p) {
      dfs(i, s, v);
    }
  }
  if (changed) {
    *it = changed;
  } else {
    v.pop_back();
  }
}

void solve() {
  cin >> n;

  ans.resize(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i + 1 < n; ++i) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    swap(x, y);
    adj[x].push_back(y);
  }

  vector<int> x;
  dfs(1, -1, x);
  for (int i = 1; i <= n; ++i) {
    cout << ans[i] << "\\n";
  }
}

int32_t main() {
  _

      int t = 1;
  // cin >> t;

  while (t--) {
    solve();
    cout << endl;
  }

  return 0;
}