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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
// priority_queue<int,vector<int>, greater<int> > q2;

vector<VI> G;
VI A;
int N;
VI ans, dp;

void dfs(int now, int pa) {

  int tmp = *LB(dp, A[now]);
  *LB(dp, A[now]) = A[now];
  ans[now] = LB(dp, INF) - dp.begin();

  fore(to, G[now]) {
    if (to != pa)
      dfs(to, now);
  }
  *LB(dp, A[now]) = tmp;
}

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

  cin >> N;
  G.resize(N);
  A.resize(N);
  ans.resize(N);
  dp.resize(N + 10, INF);

  REP(i, N) cin >> A[i];
  REP(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  dfs(0, -1);

  REP(i, N) cout << ans[i] << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
// priority_queue<int,vector<int>, greater<int> > q2;

vector<VI> G;
VI A;
int N;
VI ans;

void dfs(int now, int pa, VI dp) {

  *LB(dp, A[now]) = A[now];
  ans[now] = LB(dp, INF) - dp.begin();
  if (dp.back() != INF)
    dp.push_back(INF);
  fore(to, G[now]) {
    if (to != pa)
      dfs(to, now, dp);
  }
}

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

  cin >> N;
  G.resize(N);
  A.resize(N);
  ans.resize(N);
  REP(i, N) cin >> A[i];
  REP(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  VI tmp;
  tmp.push_back(INF);
  dfs(0, -1, tmp);

  REP(i, N) cout << ans[i] << '\\n';
  return 0;
}