Language: C++

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <string>
#include <string.h>
#include <math.h>
#include <iomanip>
#include <stdio.h>
using namespace std;

#define int64 long long
#define pii pair <int, int>
#define fr first
#define sc second
#define heap priority_queue
struct piii{
    int64 fr, sc, th;
    bool operator < (const piii &b){
        if (fr != b.fr) return fr < b.fr;
        if (sc != b.sc) return sc < b.sc;
        return th < b.th;
    }
};
///////////////////////////////////////////////
///////////////////////////////////////////////

const int maxn = 2e5 + 5;
struct cmp{
    bool operator () (const pii &x, const pii &y){
        if (x.fr == y.fr) return x.sc < y.sc;
        return x.fr > y.fr;
    }
};
int n;
int a[maxn], f[maxn], p[maxn];
set <pii, cmp> fe[maxn];
vector <pii> b;
vector <int> e[maxn];

void add(int pos, pii val){
    if (pos > n) return;
    fe[pos].insert(val);
    add(pos + (pos & (-pos)), val);
}

void del(int pos, pii val){
    if (pos > n) return;
    fe[pos].erase(val);
    del(pos + (pos & (-pos)), val);
}

int get(int pos){
    if (pos == 0) return 0;
    if (fe[pos].empty()) return get(pos - (pos & (-pos)));
    return max((*fe[pos].begin()).fr, get(pos - (pos & (-pos))));
}

void dfs(int u, int par){
    int pos = lower_bound(b.begin(), b.end(), pii({a[u], u})) - b.begin() - 1;
    f[u] = get(pos) + 1;
    add(p[u], pii({f[u], u}));
    for (int v : e[u])
    if (v != par){
        dfs(v, u);
    }
    del(p[u], pii({f[u], u}));
}

int main(){
    cout << setprecision(2) << fixed;
    ios::sync_with_stdio(false), cin.tie(0);
    //freopen("input.txt", "r", stdin);
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    int u, v;
    for (int i = 1; i < n; i++){
        cin >> u >> v;
        e[u].push_back(v);
        e[v].push_back(u);
    }
    b.resize(n + 1);
    for (int i = 1; i <= n; i++) b[i] = {a[i], i};
    sort(b.begin() + 1, b.end());
    for (int i = 1; i <= n; i++) p[b[i].sc] = i;
    dfs(1, 0);
    for (int i = 1; i <= n; i++) cout << f[i] << endl;
    return 0;
}


Compiler output:
In file included from /usr/include/c++/14/set:62,
                 from a.cc:4:
/usr/include/c++/14/bits/stl_tree.h: In instantiation of 'static const _Key& std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_S_key(_Const_Link_type) [with _Key = std::pair<int, int>; _Val = std::pair<int, int>; _KeyOfValue = std::_Identity<std::pair<int, int> >; _Compare = cmp; _Alloc = std::allocator<std::pair<int, int> >; _Const_Link_type = const std::_Rb_tree_node<std::pair<int, int> >*]':
/usr/include/c++/14/bits/stl_tree.h:2116:47:   required from 'std::pair<std::_Rb_tree_node_base*, std::_Rb_tree_node_base*> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = std::pair<int, int>; _Val = std::pair<int, int>; _KeyOfValue = std::_Identity<std::pair<int, int> >; _Compare = cmp; _Alloc = std::allocator<std::pair<int, int> >; key_type = std::pair<int, int>]'
 2116 |           __comp = _M_impl._M_key_compare(__k, _S_key(__x));
      |                                                ~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:2169:4:   required from 'std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = const std::pair<int, int>&; _Key = std::pair<int, int>; _Val = std::pair<int, int>; _KeyOfValue = std::_Identity<std::pair<int, int> >; _Compare = cmp; _Alloc = std::allocator<std::pair<int, int> >]'
 2169 |         = _M_get_insert_unique_pos(_KeyOfValue()(__v));
      |           ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_set.h:514:25:   required from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = std::pair<int, int>; _Compare = cmp; _Alloc = std::allocator<std::pair<int, int> >; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Tp>, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other>::const_iterator = std::_Rb_tree<std::pair<int, int>, std::pair<int, int>, std::_Identity<std::pair<int, int> >, cmp, std::allocator<std::pair<int, int> > >::const_iterator; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key>::other = std::allocator<std::pair<int, int> >; typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<_Key> = __gnu_cxx::__alloc_traits<std::allocator<std::pair<int, int> >, std::pair<int, int> >::rebind<std::pair<int, int> >; typename _Alloc::value_type = std::pair<int, int>; value_type = std::pair<int, int>]'
  514 |           _M_t._M_insert_unique(__x);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~
a.cc:46:19:   required from here
   46 |     fe[pos].insert(val);
      |     ~~~~~~~~~~~~~~^~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: error: static assertion failed: comparison object must be invocable as const
  768 |               is_invocable_v<const _Compare&, const _Key&, const _Key&>,
      |               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_tree.h:768:15: note: 'std::is_invocable_v<const cmp&, const std::pair<int, int>&, const std::pair<int, int>&>' evaluates to false