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

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i32 = int32_t;
using i64 = int64_t;
constexpr char newl = '\\n';

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
// #define F first
// #define S second
#define debug(x) cout << #x << ": " << x << '\\n';

#if 1

template <typename F> struct FixPoint : F {
  FixPoint(F &&f) : F(forward<F>(f)) {}
  template <typename... Args> decltype(auto) operator()(Args &&...args) const {
    return F::operator()(*this, forward<Args>(args)...);
  }
};
template <typename F> inline decltype(auto) MFP(F &&f) {
  return FixPoint<F>{forward<F>(f)};
}

// http://nocotan.github.io/%E3%82%A2%E3%83%AB%E3%82%B4%E3%83%AA%E3%82%BA%E3%83%A0/2017/01/23/lis-copy.html
int lis(const vector<int> &ns) {

  vector<int> dp(ns.size(), INT32_MAX);

  F0R(i, ns.size()) { *lower_bound(dp.begin(), dp.end(), ns[i]) = ns[i]; }

  return lower_bound(dp.begin(), dp.end(), INT32_MAX) - dp.begin();
}

// 数値一つごとに呼び出す、計算のレジュームが可能なLIS、Undo対応版
pair<int, int> lisc(vector<int> &dp, int n) {
  pair<int, int> result;
  auto it = lower_bound(dp.begin(), dp.end(), n);
  auto prev = *it;
  *it = n;

  return {it - dp.begin(), prev};
}

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

#if 0
  // debug lis test
  cout << lis({ 1, 2, 5, 3, 4 }) << newl;
  return 0;
#endif

  int N;
  cin >> N;
  vector<int> a(N);
  F0R(i, N) { cin >> a[i]; }
  vector<vector<int>> G(N, vector<int>());
  F0R(i, N - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }

  vector<int> results(N);
  vector<int> dp(N, INT32_MAX);
  MFP([&](auto dfs, int v, int p) -> void {
    auto undo = lisc(dp, a[v]);
    results[v] = lower_bound(dp.begin(), dp.end(), INT32_MAX) - dp.begin();

    for (auto e : G[v]) {
      if (e == p)
        continue;
      dfs(e, v);
    }

    dp[undo.first] = undo.second;
  })
  (0, -1);

  F0R(i, N) { cout << results[i] << newl; }
}
#endif


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i32 = int32_t;
using i64 = int64_t;
constexpr char newl = '\\n';

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, b) FOR(i, 0, b)
#define RFO(i, a, b) for (int i = ((b)-1); i >= (a); i--)
#define RF0(i, b) RFO(i, 0, b)
// #define F first
// #define S second
#define debug(x) cout << #x << ": " << x << '\\n';

#if 1

template <typename F> struct FixPoint : F {
  FixPoint(F &&f) : F(forward<F>(f)) {}
  template <typename... Args> decltype(auto) operator()(Args &&...args) const {
    return F::operator()(*this, forward<Args>(args)...);
  }
};
template <typename F> inline decltype(auto) MFP(F &&f) {
  return FixPoint<F>{forward<F>(f)};
}

// http://nocotan.github.io/%E3%82%A2%E3%83%AB%E3%82%B4%E3%83%AA%E3%82%BA%E3%83%A0/2017/01/23/lis-copy.html
int lis(const vector<int> &ns) {

  vector<int> dp(ns.size(), INT32_MAX);

  F0R(i, ns.size()) { *lower_bound(dp.begin(), dp.end(), ns[i]) = ns[i]; }

  return lower_bound(dp.begin(), dp.end(), INT32_MAX) - dp.begin();
}

// 数値一つごとに呼び出す、計算のレジュームが可能なLIS
int lisc(vector<int> &dp, int n) {
  auto it = lower_bound(dp.begin(), dp.end(), n);
  if (it == dp.end())
    dp.push_back(n);
  else
    *it = n;

  return dp.size();
}

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

#if 0
  // debug lis test
  cout << lis({ 1, 2, 5, 3, 4 }) << newl;
  return 0;
#endif

  int N;
  cin >> N;
  vector<int> a(N);
  F0R(i, N) { cin >> a[i]; }
  vector<vector<int>> G(N, vector<int>());
  F0R(i, N - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }

  vector<int> results(N);
  vector<int> ns;
  MFP([&](auto dfs, int v, int p) -> void {
    ns.push_back(a[v]);
    results[v] = lis(ns);

    for (auto e : G[v]) {
      if (e == p)
        continue;
      dfs(e, v);
    }

    ns.resize(ns.size() - 1);
  })
  (0, -1);

  F0R(i, N) { cout << results[i] << newl; }
}
#endif