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

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define rep(i, a, b) for (i = (a); i <= (b); i++)
#define ll int
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pb push_back
#define F first
#define S second
#define mod 1000000007
#define maxn 200005
#define inf 1e18
#define boost                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
#define fr freopen("source.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define SET(a, b) memset(a, b, sizeof(a))
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define all(x) x.begin(), x.end()
#define ps(x, y) fixed << setprecision(y) << x
// template<typename T>
// using ordered_set =
// tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  return out << "(" << a.F << ", " << a.S << ")";
}

template <class A> ostream &operator<<(ostream &out, const vector<A> &a) {
  out << "[";
  for (auto it = a.begin(); it != a.end(); ++it) {
    if (it != a.begin())
      out << ", ";
    out << *it;
  }
  return out << "]";
}
ll a[maxn], ans[maxn];
vector<ll> adj[maxn];
multiset<ll> s;
void dfs(ll i, ll par) {
  s.insert(a[i]);
  auto it = s.lower_bound(a[i]);
  it++;
  ll e = -1;
  if (it != s.end()) {
    e = *it;
    s.erase(it);
  }
  ans[i] = s.size();
  for (auto p : adj[i]) {
    if (p != par)
      dfs(p, i);
  }
  if (e != -1)
    s.insert(e);
  s.erase(s.find(a[i]));
}
int main() {
  boost;
  ll n, i, j, x, t, y;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  for (i = 1; i < n; i++) {
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  dfs(1, 0);
  for (i = 1; i <= n; i++)
    cout << ans[i] << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define rep(i, a, b) for (i = (a); i <= (b); i++)
#define ll int
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pb push_back
#define F first
#define S second
#define mod 1000000007
#define maxn 200005
#define inf 1e18
#define boost                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
#define fr freopen("source.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define SET(a, b) memset(a, b, sizeof(a))
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define all(x) x.begin(), x.end()
#define ps(x, y) fixed << setprecision(y) << x
// template<typename T>
// using ordered_set =
// tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  return out << "(" << a.F << ", " << a.S << ")";
}

template <class A> ostream &operator<<(ostream &out, const vector<A> &a) {
  out << "[";
  for (auto it = a.begin(); it != a.end(); ++it) {
    if (it != a.begin())
      out << ", ";
    out << *it;
  }
  return out << "]";
}
ll a[maxn], ans[maxn];
vector<ll> adj[maxn];
void dfs(ll i, ll par, multiset<ll> s) {
  s.insert(a[i]);
  auto it = s.lower_bound(a[i]);
  it++;
  if (it != s.end())
    s.erase(it);
  ans[i] = s.size();
  for (auto p : adj[i]) {
    if (p != par)
      dfs(p, i, s);
  }
}
int main() {
  boost;
  ll n, i, j, x, t, y;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> a[i];
  for (i = 1; i < n; i++) {
    cin >> x >> y;
    adj[x].pb(y);
    adj[y].pb(x);
  }
  multiset<ll> s;
  dfs(1, 0, s);
  for (i = 1; i <= n; i++)
    cout << ans[i] << "\\n";
}