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

#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::chrono;
using namespace __gnu_pbds;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define fi first
#define se second
#define int long long
#define pb push_back
#define emp emplace_back
#define vv(x) vector<x>
#define mp(x, y) map<x, y>
#define dq(x) deque<x>
#define pql(x) priority_queue<x>
#define pqs(x) priority_queue<x, vv(x), greater<x>>
#define mod 1000000007
#define forf(i, a, b) for (int i = a; i < b; i++)
#define it(x) x::iterator
#define ll long long
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d)                                                              \\
  for (long blockTime = 0;                                                     \\
       (blockTime == 0 ? (blockTime = clock()) != 0 : false);                  \\
       debug("%s time : %.4fs", d,                                             \\
             (double)(clock() - blockTime) / CLOCKS_PER_SEC))
#define vii vector<int>
#define big 2e18
#define sm -1e9
#define mkr make_pair
#define vpi vector<pair<int, int>>
#define pii pair<int, int>
#define rng 500005
#define sz(x) (int)x.size()
#define rv(x) reverse(x.begin(), x.end())
#define out(x) cout << x.fi << " " << x.se << endl;

#define ordered_set                                                            \\
  tree<pii, null_type, less<pii>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

void pr_init() {

#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);

#endif
}
vector<int> a, asc, ans;
int ptr = 0;
vector<vii> G;

void recsolve(int v, int p = -1) {
  int ind = lower_bound(asc.begin(), asc.end(), a[v]) - asc.begin();
  int was = asc[ind];
  asc[ind] = a[v];
  while (asc[ptr] < mod)
    ++ptr;
  while (ptr > 0 && asc[ptr - 1] == mod)
    --ptr;
  ans[v] = ptr;
  for (auto i : G[v])
    if (i != p)
      recsolve(i, v);
  asc[ind] = was;
}

void solve() {

  int n;
  cin >> n;
  a = vector<int>(n);
  forf(i, 0, n) cin >> a[i];
  G = vector<vii>(n);
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    G[u].pb(v);
    G[v].pb(u);
  }
  ans = vector<int>(n);
  asc = vector<int>(n + 1, mod);
  recsolve(0);
  for (auto i : ans)
    cout << i << '\\n';
}

int32_t main() {
  pr_init();
  // fastio;

  auto start = high_resolution_clock::now();

  solve();
  auto stop = high_resolution_clock::now();
  auto duration = duration_cast<microseconds>(stop - start);

  // cout << "Time taken by function: "
  //      << duration.count() << " microseconds" << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::chrono;
using namespace __gnu_pbds;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define fi first
#define se second
#define int long long
#define pb push_back
#define emp emplace_back
#define vv(x) vector<x>
#define mp(x, y) map<x, y>
#define dq(x) deque<x>
#define pql(x) priority_queue<x>
#define pqs(x) priority_queue<x, vv(x), greater<x>>
#define M 1000000007
#define forf(i, a, b) for (int i = a; i < b; i++)
#define it(x) x::iterator
#define ll long long
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d)                                                              \\
  for (long blockTime = 0;                                                     \\
       (blockTime == 0 ? (blockTime = clock()) != 0 : false);                  \\
       debug("%s time : %.4fs", d,                                             \\
             (double)(clock() - blockTime) / CLOCKS_PER_SEC))
#define vii vector<int>
#define big 2e18
#define sm -1e9
#define mkr make_pair
#define vpi vector<pair<int, int>>
#define pii pair<int, int>
#define rng 500005
#define sz(x) (int)x.size()
#define rv(x) reverse(x.begin(), x.end())
#define out(x) cout << x.fi << " " << x.se << endl;

#define ordered_set                                                            \\
  tree<pii, null_type, less<pii>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

void pr_init() {

#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);

#endif
}

vii ar, an;
vector<vii> gp;
int n;
inline int CeilIndex(vector<int> &v, int l, int r, int key) {
  while (r - l > 1) {
    int m = l + (r - l) / 2;
    if (v[m] >= key)
      r = m;
    else
      l = m;
  }

  return r;
}

void dfs(int u, int p, vii &v, vii &tail, int i, int &length) {
  v.pb(ar[u]);
  int cl = length;
  vii ctail = tail;
  if (i == 0)
    tail[0] = v[0], tail.pb(0);
  else {
    if (v[i] < tail[0])
      tail[0] = v[i];
    else if (v[i] > tail[length - 1])
      tail[length++] = v[i], tail.pb(0);
    else
      tail[CeilIndex(tail, -1, length - 1, v[i])] = v[i];
  }
  an[u] = length;
  for (auto x : gp[u])
    if (x != p) {
      dfs(x, u, v, tail, i + 1, length);
    }
  v.pop_back();
  tail = ctail;
  length = cl;
}
void solve() {

  cin >> n;
  ar.assign(n + 1, 0);
  forf(i, 1, n + 1) cin >> ar[i];

  gp.assign(n + 1, vii());

  an.assign(n + 1, 0);

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

  vii v, tail;
  tail.assign(1, 0);
  int vl = 1;
  dfs(1, -1, v, tail, 0, vl);

  for (int i = 1; i <= n; i++) {
    printf("%d\\n", an[i]);
  }
}

int32_t main() {
  pr_init();
  // fastio;

  auto start = high_resolution_clock::now();

  solve();
  auto stop = high_resolution_clock::now();
  auto duration = duration_cast<microseconds>(stop - start);

  // cout << "Time taken by function: "
  //      << duration.count() << " microseconds" << endl;
}