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 drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef set<int> S;
typedef queue<int> Q;
typedef queue<P> QP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;

int N;
vi A;
vi dst;
vvi G;
vi dp;

struct Log {
  int from, to, pos;
  Log(int f, int t, int p) : from(f), to(t), pos(p) {}
};

stack<Log> st;

void dfs(int k, int p = -1) {
  int pos = lower_bound(rng(dp), A[k]) - dp.begin();
  Log l(dp[pos], A[k], pos);
  st.push(l);
  dp[pos] = A[k];
  dst[k] = (lower_bound(rng(dp), INF) - dp.begin());
  for (int q : G[k]) {
    if (q == p)
      continue;
    dfs(q, k);
    Log last = st.top();
    st.pop();
    dp[last.pos] = last.from;
  }
}

int main() {
  cin >> N;
  A = vi(N);
  rep(i, N) { cin >> A[i]; }
  G = vvi(N);
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dst = vi(N);
  dp = vi(N, INF);
  dfs(0);
  rep(i, N) { cout << dst[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 drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef set<int> S;
typedef queue<int> Q;
typedef queue<P> QP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const int MOD = 1000000007;

int N;
vi A;
vi dst;
vvi G;

void dfs(int k, vi &dp, int p = -1) {
  *(lower_bound(rng(dp), A[k])) = A[k];
  if (dp.back() < INF) {
    dp.push_back(INF);
  }
  dst[k] = (lower_bound(rng(dp), INF) - dp.begin());
  vp ch;
  for (int q : G[k]) {
    if (q == p)
      continue;
    ch.emplace_back(A[q], q);
  }
  sort(rng(ch));
  reverse(rng(ch));
  vi dp2(dp);
  for (P &q : ch) {
    int pos = lower_bound(rng(dp), q.fi) - dp.begin();
    int val = dp[pos];
    dfs(q.se, dp2, k);
    dp2[pos] = val;
  }
}

int main() {
  cin >> N;
  A = vi(N);
  rep(i, N) { cin >> A[i]; }
  G = vvi(N);
  rep(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  dst = vi(N);
  vi dp;
  dp.push_back(INF);
  dfs(0, dp);
  rep(i, N) { cout << dst[i] << endl; }
  return 0;
}