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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define INF 1e14
#define dd double
#define MOD 1000000007
#define my_sizeof(type) ((char *)(&type + 1) - (char *)(&type))
#define nl cout << endl
#define fill(a, val) memset(a, val, sizeof(a))
#define mp make_pair
#define endl "\\n"
#define pb push_back
#define ff first
#define ss second
#define SIZE 200005
#define all(v) v.begin(), v.end()
#define s(ar, n) sort(ar, ar + n)
#define rs(ar, n) sort(ar, ar + n, greater<ll>())
#define oa(a, n)                                                               \\
  for (ll i = 0; i < n; i++)                                                   \\
    cout << a[i] << " ";                                                       \\
  nl
#define cn(a, n)                                                               \\
  for (ll i = 0; i < n; i++)                                                   \\
    cin >> a[i];
#define maxa(ar, N) *max_element(ar, ar + N)
#define mina(ar, N) *min_element(ar, ar + N)
#define fastio()                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define printclock                                                             \\
  cerr << "Time : "                                                            \\
       << 1000 * (long double)clock() / (long double)CLOCKS_PER_SEC << "ms\\n";
ll power(ll x, ll y) {
  ll res = 1;
  x = x;
  while (y > 0) {
    if (y & 1)
      res = (res * x);
    y = y >> 1;
    x = (x * x);
  }
  return res;
}
ll logtwo(ll n) {
  if (n == 1)
    return 0;
  return logtwo(n / 2) + 1;
}
vector<ll> adj[200005];
stack<ll> s[200005];
vector<ll> v(200005);
ll ar[200005];
ll ans[200005];
void dfs(int cur, int par) {
  for (auto child : adj[cur]) {
    if (child == par)
      continue;
    auto it = lower_bound(v.begin(), v.begin() + ans[cur], ar[child]);
    if (it == v.begin() + ans[cur]) {
      v[ans[cur]] = ar[child];
      s[ans[cur]].push(ar[child]);
      ans[child] = ans[cur] + 1;
    } else {
      *it = ar[child];
      s[it - v.begin()].push(ar[child]);
      ans[child] = ans[cur];
    }
    dfs(child, cur);
  }
  auto it = lower_bound(v.begin(), v.begin() + ans[cur], ar[cur]);
  s[it - v.begin()].pop();
  *it = 0;
  if (s[it - v.begin()].size())
    *it = s[it - v.begin()].top();
}
signed main() {
  fastio();
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--) {
    ll N;
    cin >> N;
    cn(ar, N);
    for (ll i = 0; i < N - 1; i++) {
      ll a, b;
      cin >> a >> b;
      a--;
      b--;
      adj[a].pb(b);
      adj[b].pb(a);
    }
    ans[0] = 1;
    s[0].push(ar[0]);
    v[0] = ar[0];
    dfs(0, -1);
    for (ll i = 0; i < N; i++)
      cout << ans[i] << endl;
  }
  printclock;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define INF 1e14
#define dd double
#define MOD 1000000007
#define my_sizeof(type) ((char *)(&type + 1) - (char *)(&type))
#define nl cout << endl
#define fill(a, val) memset(a, val, sizeof(a))
#define mp make_pair
#define endl "\\n"
#define pb push_back
#define ff first
#define ss second
#define SIZE 200005
#define all(v) v.begin(), v.end()
#define s(ar, n) sort(ar, ar + n)
#define rs(ar, n) sort(ar, ar + n, greater<ll>())
#define oa(a, n)                                                               \\
  for (ll i = 0; i < n; i++)                                                   \\
    cout << a[i] << " ";                                                       \\
  nl
#define cn(a, n)                                                               \\
  for (ll i = 0; i < n; i++)                                                   \\
    cin >> a[i];
#define maxa(ar, N) *max_element(ar, ar + N)
#define mina(ar, N) *min_element(ar, ar + N)
#define fastio()                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define printclock                                                             \\
  cerr << "Time : "                                                            \\
       << 1000 * (long double)clock() / (long double)CLOCKS_PER_SEC << "ms\\n";
ll power(ll x, ll y) {
  ll res = 1;
  x = x;
  while (y > 0) {
    if (y & 1)
      res = (res * x);
    y = y >> 1;
    x = (x * x);
  }
  return res;
}
ll logtwo(ll n) {
  if (n == 1)
    return 0;
  return logtwo(n / 2) + 1;
}
vector<ll> adj[200005];
vector<ll> lis[200005];
ll ar[200005];
ll ans[200005];
void dfs(int cur, ll par) {
  for (auto child : adj[cur]) {
    if (child == par)
      continue;
    lis[child] = lis[cur];
    ans[child] = ans[cur];
    auto it = lower_bound(lis[child].begin(), lis[child].begin() + ans[child],
                          ar[child]);
    if (it == lis[child].begin() + ans[child]) {
      lis[child].pb(ar[child]);
      ans[child]++;
    } else
      *it = ar[child];
    // cout<<ans[child]<<endl;
    dfs(child, cur);
  }
  lis[cur].clear();
}
signed main() {
  fastio();
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--) {
    ll N;
    cin >> N;
    cn(ar, N);
    for (ll i = 0; i < N - 1; i++) {
      ll a, b;
      cin >> a >> b;
      a--;
      b--;
      adj[a].pb(b);
      adj[b].pb(a);
    }
    lis[0].pb(ar[0]);
    ans[0] = 1;
    dfs(0, -1);
    for (ll i = 0; i < N; i++)
      cout << ans[i] << endl;
  }
  printclock;
}