Problem:
Score : 600 points

Problem Statement

We have a tree with N vertices, whose i-th edge connects Vertex u_i and Vertex v_i.
Vertex i has an integer a_i written on it.
For every integer k from 1 through N, solve the following problem:

We will make a sequence by lining up the integers written on the vertices along the shortest path from Vertex 1 to Vertex k, in the order they appear. Find the length of the longest increasing subsequence of this sequence.

Here, the longest increasing subsequence of a sequence A of length L is the subsequence A_{i_1} , A_{i_2} , ... , A_{i_M} with the greatest possible value of M such that 1 \leq i_1 < i_2 < ... < i_M \leq L and A_{i_1} < A_{i_2} < ... < A_{i_M}.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq a_i \leq 10^9

1 \leq u_i , v_i \leq N

u_i \neq v_i

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}

Output

Print N lines. The k-th line, print the length of the longest increasing subsequence of the sequence obtained from the shortest path from Vertex 1 to Vertex k.

Sample Input 1

10
1 2 5 3 4 6 7 3 2 4
1 2
2 3
3 4
4 5
3 6
6 7
1 8
8 9
9 10

Sample Output 1

1
2
3
3
4
4
5
2
2
3

For example, the sequence A obtained from the shortest path from Vertex 1 to Vertex 5 is 1,2,5,3,4. Its longest increasing subsequence is A_1, A_2, A_4, A_5, with the length of 4.

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <fstream>
#include <algorithm>
#include <iomanip>
#include <random>
#include <ctime>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <cstring>
#include <queue>
#include <deque>
#include <stack>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define ld long double
#define ull unsigned long long
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fast_io cout.tie(0), cin.tie(0), ios_base::sync_with_stdio(0)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using namespace std;
//using namespace __gnu_pbds;
//typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
ld eps = (ld)1 / 1e9;
ll inf = 1e18, mod2 = 998244353, mod1 = 1e9 + 7;
ll sqr(ll a) { return a * a; }
ll qb(ll a) { return a * a * a; }
ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); }
ll binpow(ll a, ll b, ll mod) { return b ? (b % 2 ? (a * (sqr(binpow(a, b / 2, mod)) % mod)) % mod : sqr(binpow(a, b / 2, mod)) % mod) : 1; }
ll binmult(ll a, ll b, ll mod) { return b ? (b % 2 ? (2 * binmult(a, b / 2, mod) + a) % mod : (2 * binmult(a, b / 2, mod)) % mod) : 0; }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct str { ll tl, tr, zn; vector<pll> vc; };
ll a[200001], ans[200001], nw = 1, lm = 1000000001;
vector<ll> gr[200001];
vector<pll> tmp;
vector<str> nDO;
void update(ll l, ll r, ll ind, ll v, ll now, bool tp)
{
    if (l == r)
    {
        if (tp) nDO[v].vc.pb({now, max(now, nDO[v].vc.back().second)});
        else nDO[v].vc.pop_back();
        nDO[v].zn = nDO[v].vc.back().second;
        return;
    }
    int m = (l + r) / 2;
    if (ind <= m)
    {
        if (nDO[v].tl == -1) nDO.pb({-1, -1, 0, tmp}), nDO[v].tl = nw, nw++;
        update(l, m, ind, nDO[v].tl, now, tp);
    }
    else
    {
        if (nDO[v].tr == -1) nDO.pb({-1, -1, 0, tmp}), nDO[v].tr = nw, nw++;
        update(m + 1, r, ind, nDO[v].tr, now, tp);
    }
    int zl = 0, zr = 0;
    if (nDO[v].tl >= 0) zl = nDO[nDO[v].tl].zn;
    if (nDO[v].tr >= 0) zr = nDO[nDO[v].tr].zn;
    nDO[v].zn = max(zl, zr);
}
ll get(ll l, ll r, ll tl, ll tr, ll v)
{
    if (l == tl && r == tr) return nDO[v].zn;
    if (l > r || tl > tr) return 0;
    ll m = (l + r) / 2;
    if (tl > m)
    {
        if (nDO[v].tr == -1) return 0;
        else return get(m + 1, r, tl, tr, nDO[v].tr);
    }
    else if (tr <= m)
    {
        if (nDO[v].tl == -1) return 0;
        else return get(l, m, tl, tr, nDO[v].tl);
    }
    else
    {
        int zl = 0, zr = 0;
        if (nDO[v].tl >= 0) zl = get(l, m, tl, m, nDO[v].tl);
        if (nDO[v].tr >= 0) zr = get(m + 1, r, m + 1, tr, nDO[v].tr);
        return max(zl, zr);
    }
}
void dfs(ll v, ll pr)
{
    ll x = get(1, lm, 1, a[v] - 1, 0);
    ans[v] = x + 1;
    update(1, lm, a[v], 0, ans[v], 1);
    for (auto f: gr[v])
    {
        if (f == pr) continue;
        dfs(f, v);
    }
    update(1, lm, a[v], 0, ans[v], 0);
}

int main()
{
    fast_io;
    ll n, i, x, y;
    cin >> n;
    for (i = 1; i <= n; i++) cin >> a[i];
    tmp.pb({0, 0});
    nDO.pb({-1, -1, 0, tmp});
    for (i = 1; i < n; i++)
    {
        cin >> x >> y;
        gr[x].pb(y);
        gr[y].pb(x);
    }
    dfs(1, 0);
    for (i = 1; i <= n; i++) cout << ans[i] << '\n';
    return 0;
}