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 <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define all(x) x.begin(), x.end()

template <typename T1, typename T2> inline void chkmin(T1 &x, const T2 & y) {if (x > y) x = y;}
template <typename T1, typename T2> inline void chkmax(T1 &x, const T2 & y) {if (x < y) x = y;}

const int MAXN = 2e5 + 10;
int n;
vector<int> a;
vector<int> g[MAXN];

const int MAXMEM = 5e7 + 10;

struct node{
    int l, r;
    int val;
    node() {
        l = -1, r = -1, val = 0;
    }
};

node tree[MAXMEM];
int topQ = 0;

int clone(int root) {
    tree[topQ] = tree[root];
    topQ++;
    assert(topQ < MAXMEM);
    return topQ - 1;
}

int build(int l, int r) {
    if (l == r - 1) {
        tree[topQ] = node();
        topQ++;
        //cout << l << " " << r << " " << topQ << endl;
        return topQ - 1;
    }
    tree[topQ] = node();
    int root = topQ;
    topQ++;
    int m = (l + r) / 2;
    tree[root].l = build(l, m);
    tree[root].r = build(m, r);
    //cout << l << " " << r << " " << root << " " << tree[root].l << " " << tree[root].r << endl;
    return root;
}

int upd(int root, int tl, int tr, int pos, int val) {
    if (tl >= pos + 1 || tr <= pos) return root;
    if (tl == tr - 1) {
        root = clone(root);
        chkmax(tree[root].val, val);
        return root;
    }
    int tm = (tl + tr) / 2;
    root = clone(root);
    tree[root].l = upd(tree[root].l, tl, tm, pos, val);
    tree[root].r = upd(tree[root].r, tm, tr, pos, val);
    tree[root].val = max(tree[tree[root].l].val, tree[tree[root].r].val);
    return root;
}

int upd(int root, int pos, int val) {
    return upd(root, 0, n, pos, val);
}

int get(int root, int tl, int tr, int l, int r) {
    assert(root != -1);
    //cout << root << " " << tl << " " << tr << " " << l << " " << r << endl;
    if (tl >= r || tr <= l) return 0;
    if (tl >= l && tr <= r) return tree[root].val;
    int tm = (tl + tr) / 2;
    return max(get(tree[root].l, tl, tm, l, r), get(tree[root].r, tm, tr, l, r));
}

int get(int root, int l, int r) {
    return get(root, 0, n, l, r + 1);
}

void read() {
    cin >> n;
    a.resize(n);
    for (auto &i : a) {
        cin >> i;
    }
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
}

void make() {
    auto have = a;
    sort(all(have));
    have.resize(unique(all(have)) - have.begin());
    for (auto &i : a) {
        i = lower_bound(all(have), i) - have.begin();
    }
}

vector<int> ans;

void dfs(int v, int p, int root) {
    //cout << v << " " << p << " " << root << endl; 
    int have = get(root, 0, a[v] - 1);
    //cout << "puhh" << endl;
    ans[v] = have + 1;
    root = upd(root, a[v], have + 1);
    for (auto i : g[v]) {
        if (i == p) continue;
        dfs(i, v, root);
    }
}

void solve() {
    ans.resize(n);
    int root = build(0, n);
    /*cout << "build = " << endl;
    for (int i = 0; i < topQ; i++) {
        cout << tree[i].l << " " << tree[i].r << endl; 
    }
    cout << endl;*/
    dfs(0, 0, root);
}

void run() {
    make();
    solve();
}

void write() {
    for (auto i : ans) {
        cout << i << "\n"; 
    }
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    read();
    run();
    write();
    return 0;
}