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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = (int)(beg); i < (int)(end); i++)
#define FOR_LE(i, beg, end) for (int i = (int)(beg); i <= (int)(end); i++)
#define FOR_DW(i, beg, end) for (int i = (int)(beg); (int)(end) <= i; i--)
#define REP(n) for (int repeat_index = 0; repeat_index < (int)n; repeat_index++)

using namespace std;

void dfs(vector<int> &as, vector<vector<int>> &edge, vector<bool> &iss,
         vector<int> &dp, vector<int> &ans, int n, int p, int cur) {
  auto it = std::lower_bound(dp.begin(), dp.end(), as[p]);
  int v = (it - dp.begin());
  int pre = dp[v];
  if (as[p] < dp[v]) {
    dp[v] = as[p];
  }
  cur = max(cur, v);
  ans[p] = cur;
  iss[p] = true;

  for (auto b : edge[p]) {
    if (iss[b])
      continue;
    dfs(as, edge, iss, dp, ans, n, b, cur);
  }

  dp[v] = pre;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(20);

  int n;
  cin >> n;
  vector<int> as(n + 1);
  FOR_LE(i, 1, n) { cin >> as[i]; }
  vector<int> dp(n + 1);
  FOR_LE(i, 1, n) { dp[i] = INT_MAX / 2; }
  dp[0] = -1;

  vector<vector<int>> edge(n + 1);
  REP(n - 1) {
    int a, b;
    cin >> a >> b;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }

  vector<bool> iss(n + 1);
  vector<int> ans(n + 1);
  dfs(as, edge, iss, dp, ans, n, 1, 0);

  FOR_LE(i, 1, n) { cout << ans[i] << endl; }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = (int)(beg); i < (int)(end); i++)
#define FOR_LE(i, beg, end) for (int i = (int)(beg); i <= (int)(end); i++)
#define FOR_DW(i, beg, end) for (int i = (int)(beg); (int)(end) <= i; i--)
#define REP(n) for (int repeat_index = 0; repeat_index < (int)n; repeat_index++)

using namespace std;

void dfs(vector<int> &as, vector<vector<int>> &edge, vector<bool> &iss,
         vector<int> dp, vector<int> &ans, int n, int p, int cur) {
  auto it = std::lower_bound(dp.begin(), dp.end(), as[p]);
  int v = (it - dp.begin());
  int pre = dp[v];
  if (as[p] < dp[v]) {
    dp[v] = as[p];
  }
  cur = max(cur, v);
  ans[p] = cur;
  iss[p] = true;

  for (auto b : edge[p]) {
    if (iss[b])
      continue;
    dfs(as, edge, iss, dp, ans, n, b, cur);
  }

  dp[v] = pre;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(20);

  int n;
  cin >> n;
  vector<int> as(n + 1);
  FOR_LE(i, 1, n) { cin >> as[i]; }
  vector<int> dp(n + 1);
  FOR_LE(i, 1, n) { dp[i] = INT_MAX / 2; }
  dp[0] = -1;

  vector<vector<int>> edge(n + 1);
  REP(n - 1) {
    int a, b;
    cin >> a >> b;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }

  vector<bool> iss(n + 1);
  vector<int> ans(n + 1);
  dfs(as, edge, iss, dp, ans, n, 1, 0);

  FOR_LE(i, 1, n) { cout << ans[i] << endl; }

  return 0;
}