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

#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define double long double
#define int long long
#define pb push_back
#define eb emplace_back
#define Z(c) (int)(c).size()
#define L(c) c[Z(c) - 1]
#define F first
#define S second
#define nl "\\n"
#define mii map<int, int>
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define rep(i, iv, n) for (int(i) = (iv); (i) < (n); ++(i))
#define FO fflush(stdout)
#define D1(x) cerr << "At line " << __LINE__ << " " << #x << "=" << x << nl
#define D2(x, y)                                                               \\
  cerr << "At line " << __LINE__ << " " << #x << "=" << x << " " << #y << "="  \\
       << y << nl
#define D3(x, y, z)                                                            \\
  cerr << "At line " << __LINE__ << " " << #x << "=" << x << " " << #y << "="  \\
       << y << " " << #z << "=" << z << nl
#define tr(it, ct) for (auto &(it) : (ct))
#define pi acos(-1)
#define prt(ct)                                                                \\
  for (auto &(it) : (ct))                                                      \\
    cerr << it << " ";                                                         \\
  cerr << nl
#define prtp(ct)                                                               \\
  for (auto &(it) : (ct))                                                      \\
    cerr << it.F << " " << it.S << nl;                                         \\
  cerr << nl

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<pii, pii> ppp;
typedef vector<pii> vp;
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    baap;

void swap(char &a, char &b) {
  auto tm = a;
  a = b;
  b = tm;
}

void mina(int &a, int b) { a = (a < b ? a : b); }

void maxa(int &a, int b) { a = (a > b ? a : b); }

const int mod = 1000000007;
const int mod2 = 998244353;
const int INF = 1e18;
const int N = 2e5 + 35;
const int M = 1005;

vi v, adj[N];

int ans[N];

void dfs(int node, int par, multiset<int> &s) {
  s.insert(v[node]);
  auto it = s.lower_bound(v[node]);
  ++it;
  int p = -1;
  if (it != s.end()) {
    p = *it;
    s.erase(it);
  }
  ans[node] = Z(s);
  tr(i, adj[node]) if (i != par) dfs(i, node, s);
  if (p != -1)
    s.insert(p);
  s.erase(s.find(v[node]));
}

void doit() {
  int n;
  cin >> n;
  v.resize(n + 1);
  rep(i, 1, n + 1) cin >> v[i];

  rep(i, 1, n) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  multiset<int> s;
  dfs(1, -1, s);
  rep(i, 1, n + 1) cout << ans[i] << " ";
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int tc = 1;
  //   cin>>tc;

  rep(i, 1, tc + 1) { // cout<<"Case #"<<i<<": ";

    doit();
    if (i < tc)
      cout << nl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define double long double
#define int long long
#define pb push_back
#define eb emplace_back
#define Z(c) (int)(c).size()
#define L(c) c[Z(c) - 1]
#define F first
#define S second
#define nl "\\n"
#define mii map<int, int>
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define rep(i, iv, n) for (int(i) = (iv); (i) < (n); ++(i))
#define FO fflush(stdout)
#define D1(x) cerr << "At line " << __LINE__ << " " << #x << "=" << x << nl
#define D2(x, y)                                                               \\
  cerr << "At line " << __LINE__ << " " << #x << "=" << x << " " << #y << "="  \\
       << y << nl
#define D3(x, y, z)                                                            \\
  cerr << "At line " << __LINE__ << " " << #x << "=" << x << " " << #y << "="  \\
       << y << " " << #z << "=" << z << nl
#define tr(it, ct) for (auto &(it) : (ct))
#define pi acos(-1)
#define prt(ct)                                                                \\
  for (auto &(it) : (ct))                                                      \\
    cerr << it << " ";                                                         \\
  cerr << nl
#define prtp(ct)                                                               \\
  for (auto &(it) : (ct))                                                      \\
    cerr << it.F << " " << it.S << nl;                                         \\
  cerr << nl

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<pii, pii> ppp;
typedef vector<pii> vp;
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    baap;

void swap(char &a, char &b) {
  auto tm = a;
  a = b;
  b = tm;
}

void mina(int &a, int b) { a = (a < b ? a : b); }

void maxa(int &a, int b) { a = (a > b ? a : b); }

const int mod = 1000000007;
const int mod2 = 998244353;
const int INF = 1e18;
const int N = 2e5 + 35;
const int M = 1005;

vi v, adj[N];

int ans[N];

void dfs(int node, int par, multiset<int> s) {
  s.insert(v[node]);
  auto it = s.lower_bound(v[node]);
  ++it;
  if (it != s.end())
    s.erase(it);
  ans[node] = Z(s);
  tr(i, adj[node]) if (i != par) dfs(i, node, s);
}

void doit() {
  int n;
  cin >> n;
  v.resize(n + 1);
  rep(i, 1, n + 1) cin >> v[i];

  rep(i, 1, n) {
    int x, y;
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }

  dfs(1, -1, multiset<int>());
  rep(i, 1, n + 1) cout << ans[i] << " ";
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int tc = 1;
  //   cin>>tc;

  rep(i, 1, tc + 1) { // cout<<"Case #"<<i<<": ";

    doit();
    if (i < tc)
      cout << nl;
  }

  return 0;
}