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: Accepted)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz = 2e5+5;

typedef pair <int,int> pii;
#define x first
#define y second

pii a[sz];

vector <int> g[sz];

int tree[sz*4], ans[sz];

void update(int node, int b, int e, int x, int v)
{
    if(x<b || e<x) return;
    if(b == e) {
        tree[node] += v;
        return;
    }

    int l=node<<1, r=l+1, m=b+e>>1;

    update(l, b, m, x, v);
    update(r, m+1, e, x, v);
    tree[node] = max(tree[l], tree[r]);
}

int segq(int node, int b, int e, int i, int j)
{
    if(i>j || e<i || j<b) return 0;
    if(i<=b && e<=j)
        return tree[node];

    int l=node<<1, r=l+1, m=b+e>>1;

    return max(segq(l, b, m, i, j), segq(r, m+1, e, i, j));
}

int heavy[sz], depth[sz], head[sz], par[sz], pos[sz], cpos;

int dfs(int u = 1)
{
    int subc = 1, mxc = 0;
    for(int v : g[u]) if(v ^ par[u]) {
        par[v] = u;
        depth[v] = depth[u] + 1;
        int nxtc = dfs(v);
        subc += nxtc;
        if(nxtc > mxc) mxc = nxtc, heavy[u] = v;
    }
    return subc;
}

void hfs(int u = 1, int h = 1)
{
    head[u] = h, pos[u] = ++cpos;
    int hv = heavy[u];
    if(hv) hfs(hv, h);

    for(int v : g[u])
        if(v ^ par[u] && v ^ hv)
            hfs(v, v);
}

int hldq(int u, int v, int n)
{
    int ans = 0;
    while(head[u] ^ head[v]) {
        if(depth[head[u]] < depth[head[v]])
            swap(u, v);
        ans = max(ans, segq(1, 1, n, pos[head[u]], pos[u]));
        u = par[head[u]];
    }

    if(depth[u] > depth[v]) swap(u, v);
    return max(ans, segq(1, 1, n, pos[u], pos[v]));
}

void go(int u = 1, int p = 0)
{
    for(int v : g[u]) if(v ^ p) {
        ans[v] = max(ans[v], ans[u]);
        go(v, u);
    }
}

int main()
{
    int n;
    cin >> n;
    for(int i=1; i<=n; i++) scanf("%d", &a[i].x), a[i].y = i;
    sort(a+1, a+n+1);

    for(int i=1; i<n; i++) {
        int u, v;
        scanf("%d %d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
    }

    dfs();
    hfs();

    for(int i=1; i<=n;) {
        vector <int> now;
        for(int j=i; i<=n && a[i].x==a[j].x; i++) {
            int u = a[i].y;
            now.push_back(u);
            ans[u] = hldq(1, u, n) + 1;
        }
        for(int u : now) update(1, 1, n, pos[u], ans[u]);
    }

    go();
    for(int i=1; i<=n; i++) printf("%d\n", ans[i]);
}