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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000005
#define fi first
#define sc second
#define rep(i, x) for (long long i = 0; i < x; i++)
#define repn(i, x) for (long long i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
vector<pair<string, P>> vec;
// vector<vector<int>> data(3, vector<int>(4));

using Weight = ll;
using Flow = ll;
struct Edge {
  ll src, dst;
  Weight weight;
  Flow cap;
  Edge() : src(0), dst(0), weight(0) {}
  Edge(ll s, ll d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;

void add_edge(Graph &g, ll a, ll b, Weight w = 1) {
  g[a].emplace_back(a, b, w);
  g[b].emplace_back(b, a, w);
}
void add_arc(Graph &g, ll a, ll b, Weight w = 1) { g[a].emplace_back(a, b, w); }

Graph to_rooted(const Graph &g, ll r = 0) {
  ll n = g.size();
  Graph ng(n);
  std::vector<ll> ord(n, -1);
  std::queue<ll> q;
  q.push(r);
  ll k = 0;
  while (q.size()) {
    ll u = q.front();
    q.pop();
    for (auto &e : g[u]) {
      ll v = e.dst;
      if (ord[v] == -1) {
        ord[v] = k++;
        q.push(v);
        ng[u].emplace_back(e);
      }
    }
  }
  return ng;
}

ll res[200001];
bool used[200001];
vector<ll> dp(200001, INF);
vector<long long> a(200001), u(200001), v(200001);

Graph g(200001);

void solve(ll piv, long long N) {
  used[piv] = true;
  ll p = *lower_bound(dp.begin(), dp.end(), a[piv]);
  *lower_bound(dp.begin(), dp.end(), a[piv]) = a[piv];
  res[piv] = POSL(dp, INF);
  for (auto x : g[piv]) {
    if (!used[x.dst])
      solve(x.dst, N);
  }
  *lower_bound(dp.begin(), dp.end(), a[piv]) = p;
}

int main() {
  long long N;
  scanf("%lld", &N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &a[i]);
  }

  for (int i = 0; i < N - 1; i++) {
    scanf("%lld", &u[i]);
    scanf("%lld", &v[i]);
    u[i]--;
    v[i]--;
  }

  rep(i, N) { add_edge(g, u[i], v[i]); }

  solve(0, N);
  rep(i, N) cout << res[i] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000005
#define fi first
#define sc second
#define rep(i, x) for (long long i = 0; i < x; i++)
#define repn(i, x) for (long long i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
vector<pair<string, P>> vec;
// vector<vector<int>> data(3, vector<int>(4));

using Weight = ll;
using Flow = ll;
struct Edge {
  ll src, dst;
  Weight weight;
  Flow cap;
  Edge() : src(0), dst(0), weight(0) {}
  Edge(ll s, ll d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;

void add_edge(Graph &g, ll a, ll b, Weight w = 1) {
  g[a].emplace_back(a, b, w);
  g[b].emplace_back(b, a, w);
}
void add_arc(Graph &g, ll a, ll b, Weight w = 1) { g[a].emplace_back(a, b, w); }

Graph to_rooted(const Graph &g, ll r = 0) {
  ll n = g.size();
  Graph ng(n);
  std::vector<ll> ord(n, -1);
  std::queue<ll> q;
  q.push(r);
  ll k = 0;
  while (q.size()) {
    ll u = q.front();
    q.pop();
    for (auto &e : g[u]) {
      ll v = e.dst;
      if (ord[v] == -1) {
        ord[v] = k++;
        q.push(v);
        ng[u].emplace_back(e);
      }
    }
  }
  return ng;
}

ll res[200001];
bool used[200001];
vector<ll> dp(200001, INF);
vector<long long> a(200001), u(200001), v(200001);

void solve(Graph g, ll piv, long long N) {
  used[piv] = true;
  ll p = *lower_bound(dp.begin(), dp.end(), a[piv]);
  *lower_bound(dp.begin(), dp.end(), a[piv]) = a[piv];
  res[piv] = POSL(dp, INF);
  for (auto x : g[piv]) {
    if (!used[x.dst])
      solve(g, x.dst, N);
  }
  *lower_bound(dp.begin(), dp.end(), a[piv]) = p;
}

int main() {
  long long N;
  scanf("%lld", &N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &a[i]);
  }

  for (int i = 0; i < N - 1; i++) {
    scanf("%lld", &u[i]);
    scanf("%lld", &v[i]);
    u[i]--;
    v[i]--;
  }

  Graph g(N);
  rep(i, N) { add_edge(g, u[i], v[i]); }
  Graph h = to_rooted(g);

  solve(h, 0, N);
  rep(i, N) cout << res[i] << endl;
  return 0;
}