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 <stdio.h>
#include <stdlib.h>

#define INF 1111111111

#define KI_MAX (1 << 18) /* 262144 */

int ki[KI_MAX * 2 - 1];

void ki_set(int idx, int value) {
	int pos = KI_MAX - 1 + idx;
	ki[pos] = value;
	do {
		pos = (pos - 1) / 2;
		ki[pos] = ki[pos * 2 + 1] < ki[pos * 2 + 2] ? ki[pos * 2 + 1] : ki[pos * 2 + 2];
	} while (pos > 0);
}

int ki_get_i(int idx, int qstart, int qend, int sstart, int send) {
	if (qend <= sstart || send <= qstart) { /* 完全にはずれている */
		return INF;
	} else if (qstart <= sstart && send <= qend) { /* セグメントがクエリに完全に含まれる */
		return ki[idx];
	} else {
		int smid = sstart + (send - sstart) / 2;
		int left = ki_get_i(idx * 2 + 1, qstart, qend, sstart, smid);
		int right = ki_get_i(idx * 2 + 2, qstart, qend, smid, send);
		return left < right ? left : right;
	}
}

int ki_get(int start, int end) {
	return start < end ? ki_get_i(0, start, end, 0, KI_MAX) : INF;
}

int N;
int a[271828];

int ec[271828];
int* es[271828];

void ae(int f, int t) {
	es[f] = realloc(es[f], sizeof(*es[f]) * (ec[f] + 1));
	if (es[f] == NULL) exit(2);
	es[f][ec[f]++] = t;
}

int kotae[271828];

int mareitaso(int query) {
	int l = 0, r = N + 99;
	while (l + 1 < r) {
		int m = l + (r - l) / 2;
		if (ki_get(m, N + 100) < query) l = m; else r = m;
	}
	return l;
}

void dfs(int node, int parent) {
	int neko = mareitaso(a[node]) + 1;
	int bak = ki_get(neko, neko + 1);
	int i;
	if (bak > a[node]) ki_set(neko, a[node]);
	kotae[node] = mareitaso(INF);
	for (i = 0; i < ec[node]; i++) {
		int next = es[node][i];
		if (next != parent) dfs(next, node);
	}
	ki_set(neko, bak);
}

int main(void) {
	int i;
	if (scanf("%d", &N) != 1) return 1;
	for (i = 1; i <= N; i++) {
		if (scanf("%d", &a[i]) != 1) return 1;
	}
	for (i = 1; i <= N + 100; i++) ki_set(i, INF);
	for (i = 1; i <= N - 1; i++) {
		int u, v;
		if (scanf("%d%d", &u, &v) != 2) return 1;
		ae(u, v);
		ae(v, u);
	}
	dfs(1, 0);
	for (i = 1; i <= N; i++) {
		printf("%d\n", kotae[i]);
	}
	return 0;
}