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

#pragma optimization_level 3
#pragma GCC optimize(                                                          \\
    "Ofast") // Comment optimisations for an interatcive problem - use endl
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(10);
#define f first
#define s second
#define endl "\\n"
#define pb push_back
#define mset(a, x) memset(a, x, sizeof(a))
#define debv(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << it << " " << endl;
#define deb1(a) cout << a << " " << endl;
#define deb2(a, b) cout << a << " " << b << " " << endl;
#define deb3(a, b, c) cout << a << " " << b << " " << c << " " << endl;
#define deb4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef unordered_map<ll, ll> um;
typedef vector<pll> vpll;

const ll N = 1e6 + 5;
const ll mod = 1e9 + 7;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;

ll test = 1, n, u, v, a[N], dp[N];
vll adj[N];
multiset<int> s;

void dfs(int x, int par) {
  int del = -1;
  s.insert(a[x]);
  auto it = s.lower_bound(a[x]);
  it++;
  if (it != s.end())
    del = *it, s.erase(it);
  dp[x] = s.size();
  for (auto it : adj[x]) {
    if (it != par)
      dfs(it, x);
  }
  s.erase(a[x]);
  if (del != -1)
    s.insert(del);
}

void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i < n; i++) {
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    cout << dp[i] << endl;
}

int main() {
  fastio;
  // cin>>test;
  for (int i = 1; i <= test; i++) {
    solve();
  }
#ifndef ONLINE_JUDGE
  cout << "\\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\\n";
#endif
}

Error: TLE

Fixed submission:

#pragma optimization_level 3
#pragma GCC optimize(                                                          \\
    "Ofast") // Comment optimisations for an interatcive problem - use endl
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout << fixed;                                                               \\
  cout << setprecision(10);
#define f first
#define s second
#define endl "\\n"
#define pb push_back
#define mset(a, x) memset(a, x, sizeof(a))
#define debv(a)                                                                \\
  for (auto it : a)                                                            \\
    cout << it << " " << endl;
#define deb1(a) cout << a << " " << endl;
#define deb2(a, b) cout << a << " " << b << " " << endl;
#define deb3(a, b, c) cout << a << " " << b << " " << c << " " << endl;
#define deb4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef unordered_map<ll, ll> um;
typedef vector<pll> vpll;

const ll N = 1e6 + 5;
const ll mod = 1e9 + 7;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;

ll test = 1, n, u, v, a[N], dp[N];
vll adj[N];

void dfs(int x, int par, multiset<int> s) {
  s.insert(a[x]);
  auto it = s.lower_bound(a[x]);
  it++;
  if (it != s.end())
    s.erase(it);
  dp[x] = s.size();
  for (auto it : adj[x]) {
    if (it != par)
      dfs(it, x, s);
  }
}

void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i < n; i++) {
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  multiset<int> s;
  dfs(1, 0, s);
  for (int i = 1; i <= n; i++)
    cout << dp[i] << endl;
}

int main() {
  fastio;
  // cin>>test;
  for (int i = 1; i <= test; i++) {
    solve();
  }
}