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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;

const ll inf = 1LL << 60;

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}

typedef pair<ll, ll> P;
vec d;
struct edge {
  ll to, cost;
};
vector<vector<edge>> G;

void dijkstra(ll s, ll V) {
  priority_queue<P, vector<P>, greater<P>> que;
  d.assign(V, inf);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    ll v = p.second;
    if (d[v] < p.first)
      continue;
    for (edge e : G[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

vec ans;
ll N;
vec a;
stack<tuple<ll, ll, ll>> history;

void dfs(ll from, ll v, vec &tmp) {
  auto itr = lower_bound(tmp.begin(), tmp.end(), a[v]);
  history.push({itr - tmp.begin(), *itr, a[v]});
  // cout << "push:" << itr-tmp.begin() << " " << *itr << " " << a[v] << "\\n";
  *itr = a[v];
  ans[v] = lower_bound(tmp.begin(), tmp.end(), inf) - tmp.begin();

  // cout << "v:" << v << "\\n";
  // Rep (i, N) {
  //     cout << tmp[i] << " \\n"[i==N-1];
  // }
  for (auto nv : G[v]) {
    if (ans[nv.to] != -1)
      continue;
    dfs(v, nv.to, tmp);
  }

  auto e = history.top();
  history.pop();
  tmp[get<0>(e)] = get<1>(e);

  // cout << "kaeri v:" << v << "\\n";
  // Rep (i, N) {
  //     cout << tmp[i] << " \\n"[i==N-1];
  // }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N;
  a.assign(N, 0);
  Rep(i, N) { cin >> a[i]; }

  G.assign(N, vector<edge>());
  ans.assign(N, -1);

  Rep(i, N - 1) {
    ll u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back({v, 1});
    G[v].push_back({u, 1});
  }

  vec tmp(N, inf);
  // tmp[0] = a[0];
  ans[0] = 1;
  dfs(-1, 0, tmp);

  Rep(i, N) { cout << ans[i] << "\\n"; }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;

const ll inf = 1LL << 60;

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}

typedef pair<ll, ll> P;
vec d;
struct edge {
  ll to, cost;
};
vector<vector<edge>> G;

void dijkstra(ll s, ll V) {
  priority_queue<P, vector<P>, greater<P>> que;
  d.assign(V, inf);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    ll v = p.second;
    if (d[v] < p.first)
      continue;
    for (edge e : G[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

vec ans;
ll N;
vec a;

void dfs(ll from, ll v, vec tmp) {
  *lower_bound(tmp.begin(), tmp.end(), a[v]) = a[v];
  ans[v] = lower_bound(tmp.begin(), tmp.end(), inf) - tmp.begin();

  // cout << "v:" << v << "\\n";
  // Rep (i, N) {
  //     cout << tmp[i] << " \\n"[i==N-1];
  // }
  for (auto nv : G[v]) {
    if (ans[nv.to] != -1)
      continue;
    dfs(v, nv.to, tmp);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N;
  a.assign(N, 0);
  Rep(i, N) { cin >> a[i]; }

  G.assign(N, vector<edge>());
  ans.assign(N, -1);

  Rep(i, N - 1) {
    ll u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back({v, 1});
    G[v].push_back({u, 1});
  }

  vec tmp(N, inf);
  // tmp[0] = a[0];
  ans[0] = 1;
  dfs(-1, 0, tmp);

  Rep(i, N) { cout << ans[i] << "\\n"; }
}