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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, s, n) for (int i = (int)s; i < (int)n; i++)
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define All(x) x.begin(), x.end()
#define Range(x, i, j) x.begin() + i, x.begin() + j
#define lbidx(x, y) lower_bound(x.begin(), x.end(), y) - x.begin()
#define ubidx(x, y) upper_bound(x.begin(), x.end(), y) - x.begin()
#define llbidx(x, y, z)                                                        \\
  lower_bound(x.begin(), x.end(), z) -                                         \\
      lower_bound(x.begin(), x.end(), y) // 二要素間の距離
#define deg2rad(deg) ((((double)deg) / ((double)360) * 2 * M_PI))
#define rad2deg(rad) ((((double)rad) / (double)2 / M_PI) * (double)360)
#define Find(set, element) set.find(element) != set.end()
#define Decimal(x) printf("%.10f\\n", x) // 小数点を10桁まで表示
// debug用
#define PrintVec(x)                                                            \\
  for (auto elementPrintVec : x) {                                             \\
    cout << elementPrintVec << " ";                                            \\
  }                                                                            \\
  cout << "\\n";
#define debug(x) cerr << #x << ": " << (x) << "\\n";
#define endl "\\n"
// gcj print用
#define Case(x) printf("Case #%d: ", x);

typedef pair<int, int> PI;
typedef pair<ll, ll> PLL;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<int>>> vvvl;
typedef vector<PI> vpi;
typedef vector<vector<PI>> vvpi;
typedef vector<vector<vector<PI>>> vvvpi;
typedef vector<PLL> vpl;
typedef vector<vector<PLL>> vvpl;
typedef vector<vector<vector<PLL>>> vvvpl;

int POWINT(int x, int n) {
  int ret = 1;
  while (n > 0) {
    if (n & 1)
      ret *= x;
    x *= x;
    n >>= 1;
  }
  return ret;
};

ll POWLL(ll x, int n) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1)
      ret *= x;
    x *= x;
    n >>= 1;
  }
  return ret;
};

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
};

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
};

void print() { cout << endl; }

template <class T> void print(vector<T> &vec) {
  for (auto &a : vec) {
    cout << a;
    if (&a != &vec.back())
      cout << " ";
  }
  cout << endl;
}

template <class T> void print(vector<T> &vec, ll k) {
  ll n = vec.size();
  k = min(k, n);
  rep(i, 0, k - 1) cout << vec[i] << " ";
  cout << vec[k - 1] << endl;
}

template <class T> void print(vector<vector<T>> &df) {
  for (auto &vec : df) {
    print(vec);
  }
}

template <class T, class U> void print(pair<T, U> &p) {
  cout << p.first << " " << p.second << "\\n";
}

const int MAX_N = 200005;
const int INF = 1e9 + 7;

vi a(MAX_N), ans(MAX_N);
vi graph[MAX_N];
vi dp(MAX_N, INF);

void dfs(int u, int p = -1) {
  int i = lbidx(dp, a[u]);
  int old = dp[i];
  dp[i] = a[u];
  ans[u] = lbidx(dp, INF) - 1; // dp[0] = -INF分を引く
  // if (p != -1) ans[v] = max(ans[v], ans[p])
  for (int v : graph[u]) {
    if (v == p)
      continue;
    dfs(v, u);
  }
  dp[i] = old;
};

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

  int n;
  cin >> n;
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, n - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    graph[v].pb(u);
    graph[u].pb(v);
  }

  dp[0] = -INF;
  dfs(0);

  rep(i, 0, n) { cout << ans[i] << endl; }

  return 0;
};


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, s, n) for (int i = (int)s; i < (int)n; i++)
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define All(x) x.begin(), x.end()
#define Range(x, i, j) x.begin() + i, x.begin() + j
#define lbidx(x, y) lower_bound(x.begin(), x.end(), y) - x.begin()
#define ubidx(x, y) upper_bound(x.begin(), x.end(), y) - x.begin()
#define llbidx(x, y, z)                                                        \\
  lower_bound(x.begin(), x.end(), z) -                                         \\
      lower_bound(x.begin(), x.end(), y) // 二要素間の距離
#define deg2rad(deg) ((((double)deg) / ((double)360) * 2 * M_PI))
#define rad2deg(rad) ((((double)rad) / (double)2 / M_PI) * (double)360)
#define Find(set, element) set.find(element) != set.end()
#define Decimal(x) printf("%.10f\\n", x) // 小数点を10桁まで表示
// debug用
#define PrintVec(x)                                                            \\
  for (auto elementPrintVec : x) {                                             \\
    cout << elementPrintVec << " ";                                            \\
  }                                                                            \\
  cout << "\\n";
#define debug(x) cerr << #x << ": " << (x) << "\\n";
#define endl "\\n"
// gcj print用
#define Case(x) printf("Case #%d: ", x);

typedef pair<int, int> PI;
typedef pair<ll, ll> PLL;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<int>>> vvvl;
typedef vector<PI> vpi;
typedef vector<vector<PI>> vvpi;
typedef vector<vector<vector<PI>>> vvvpi;
typedef vector<PLL> vpl;
typedef vector<vector<PLL>> vvpl;
typedef vector<vector<vector<PLL>>> vvvpl;

int POWINT(int x, int n) {
  int ret = 1;
  while (n > 0) {
    if (n & 1)
      ret *= x;
    x *= x;
    n >>= 1;
  }
  return ret;
};

ll POWLL(ll x, int n) {
  ll ret = 1;
  while (n > 0) {
    if (n & 1)
      ret *= x;
    x *= x;
    n >>= 1;
  }
  return ret;
};

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
};

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
};

void print() { cout << endl; }

template <class T> void print(vector<T> &vec) {
  for (auto &a : vec) {
    cout << a;
    if (&a != &vec.back())
      cout << " ";
  }
  cout << endl;
}

template <class T> void print(vector<T> &vec, ll k) {
  ll n = vec.size();
  k = min(k, n);
  rep(i, 0, k - 1) cout << vec[i] << " ";
  cout << vec[k - 1] << endl;
}

template <class T> void print(vector<vector<T>> &df) {
  for (auto &vec : df) {
    print(vec);
  }
}

template <class T, class U> void print(pair<T, U> &p) {
  cout << p.first << " " << p.second << "\\n";
}

vector<int> LIS(vector<int> &x, int s, int e) {
  int INFTY = 1e9 + 7;
  vector<int> ret(e - s, INFTY);
  ret[0] = x[s];
  int size = 1;
  for (int i = s + 1; i < e; i++) {
    int tmp = x[i];
    // 注意: 以上、以下の場合 <= , >=となる
    if (ret[size - 1] < tmp) {
      ret[size++] = tmp;
    } else {
      // 注意: 以上、以下の場合upper_boundとなる
      *lower_bound(ret.begin(), ret.end(), tmp) = tmp;
    }
  }
  return vector<int>(ret.begin(), ret.begin() + size);
};

const int MAX_N = 200005;
vi A(MAX_N);
vvi graph(MAX_N);
vector<bool> done(MAX_N, false);

vvi dp(MAX_N);

void solve() {
  queue<int> q;
  q.push(0);
  dp[0].pb(A[0]);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (done[u])
      continue;
    done[u] = true;

    for (int v : graph[u]) {
      if (done[v])
        continue;
      dp[v] = dp[u];

      if (A[v] > dp[u][dp[u].size() - 1]) {
        dp[v].pb(A[v]);
      } else {
        *lower_bound(dp[v].begin(), dp[v].end(), A[v]) = A[v];
      }
      q.push(v);
    }
  }
};

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

  int N;
  cin >> N;
  rep(i, 0, N) { cin >> A[i]; }

  int u, v;
  rep(i, 0, N - 1) {
    cin >> u >> v;
    u--;
    v--;
    graph[u].pb(v);
    graph[v].pb(u);
  }

  solve();

  rep(i, 0, N) { cout << dp[i].size() << endl; }

  return 0;
};