Problem p02698 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define clr(a, h) memset(a, (h), sizeof(a))
#define mem(a, h) memset(a, (h), sizeof(a))
#define fi first
#define se second
#define por(a, b) (((a % MOD) * (b % MOD)) % MOD)
#define forg(i, b, e, c) for (ll i = (ll)b; i < (ll)e; i += c)
#define forr(i, b, e) for (ll i = b; i < e; i++)

using namespace std;
using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll> ii;
typedef pair<ii, ll> iii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
const ll INF = 1e9;
const double PI = acos(-1);
#define tam 120000
#define offset
const ll MOD = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
vector<int> colas[tam], g[tam];
ordered_set s;
int resp[tam], v[tam];
int lstuniv = 0;
void dfs(int num, int pa) {
  int delta = 0;
  int p = -1, ant;

  if (lstuniv == 0 || colas[lstuniv].back() < v[num]) {
    delta = 1;
    p = ++lstuniv;

    colas[lstuniv].push_back(v[num]);
    s.insert(v[num]);
  } else {
    if (s.find(v[num]) == s.end()) {
      int aux = *s.upper_bound(v[num]);
      p = s.order_of_key(aux + 1);
      ant = colas[p].back();
      s.erase(ant);
      colas[p].push_back(v[num]);
      s.insert(v[num]);
    }
  }
  // cout<<num<<": "<<pa<<endl;
  /*for(auto ite=s.begin();ite!=s.end();ite++)
      cout<<*ite<<' ';
  cout<<endl;*/
  resp[num] = s.size();
  for (int v : g[num]) {
    if (v == pa)
      continue;
    dfs(v, num);
  }
  if (p != -1) {
    lstuniv -= delta;
    int aux = colas[p].back();
    s.erase(aux);
    colas[p].pop_back();
    if (!colas[p].empty())
      s.insert(colas[p].back());
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  forr(i, 1, n + 1) cin >> v[i];
  int iz, der;
  forr(i, 0, n - 1) {
    cin >> iz >> der;
    g[iz].pb(der);
    g[der].pb(iz);
  }
  dfs(1, 1);
  forr(i, 1, n + 1) cout << resp[i] << endl;
}
// PLUS ULTRA!


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define clr(a, h) memset(a, (h), sizeof(a))
#define mem(a, h) memset(a, (h), sizeof(a))
#define fi first
#define se second
#define por(a, b) (((a % MOD) * (b % MOD)) % MOD)
#define forg(i, b, e, c) for (ll i = (ll)b; i < (ll)e; i += c)
#define forr(i, b, e) for (ll i = b; i < e; i++)

using namespace std;
using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll> ii;
typedef pair<ii, ll> iii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
const ll INF = 1e9;
const double PI = acos(-1);
#define tam 220000
#define offset
const ll MOD = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
vector<int> colas[tam], g[tam];
ordered_set s;
int resp[tam], v[tam];
int lstuniv = 0;
void dfs(int num, int pa) {
  int delta = 0;
  int p = -1, ant;

  if (lstuniv == 0 || colas[lstuniv].back() < v[num]) {
    delta = 1;
    p = ++lstuniv;

    colas[lstuniv].push_back(v[num]);
    s.insert(v[num]);
  } else {
    if (s.find(v[num]) == s.end()) {
      int aux = *s.upper_bound(v[num]);
      p = s.order_of_key(aux + 1);
      ant = colas[p].back();
      s.erase(ant);
      colas[p].push_back(v[num]);
      s.insert(v[num]);
    }
  }
  // cout<<num<<": "<<pa<<endl;
  /*for(auto ite=s.begin();ite!=s.end();ite++)
      cout<<*ite<<' ';
  cout<<endl;*/
  resp[num] = s.size();
  for (int v : g[num]) {
    if (v == pa)
      continue;
    dfs(v, num);
  }
  if (p != -1) {
    lstuniv -= delta;
    int aux = colas[p].back();
    s.erase(aux);
    colas[p].pop_back();
    if (!colas[p].empty())
      s.insert(colas[p].back());
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  forr(i, 1, n + 1) cin >> v[i];
  int iz, der;
  forr(i, 0, n - 1) {
    cin >> iz >> der;
    g[iz].pb(der);
    g[der].pb(iz);
  }
  dfs(1, 1);
  forr(i, 1, n + 1) cout << resp[i] << endl;
}
// PLUS ULTRA!