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: Runtime Error)


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
using VVI = vector<VI>;
using VVL = vector<VL>;
using PII = std::pair<int, int>;
using PLL = std::pair<ll, ll>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define rep3(i, s, n, d) for (int i = (s); i < (int)(n); i += (d))
#define allpt(v) (v).begin(), (v).end()
#define allpt_c(v) (v).cbegin(), (v).cend()
#define allpt_r(v) (v).rbegin(), (v).rend()

const int mod = 1e9 + 7, mod2 = 998244353, mod3 = 1e9 + 9;
const ll inf = 1e18;

const string wsp = " ";
const string tb = "\t";
const string rt = "\n";

template <typename T>
void show1dvec(const vector<T> &v)
{
    if (v.size() == 0)
        return;
    int n = v.size() - 1;
    rep(i, n) cout << v[i] << wsp;
    cout << v[n] << rt;

    return;
}

template <typename T>
void show2dvec(const vector<vector<T>> &v)
{
    int n = v.size();
    rep(i, n) show1dvec(v[i]);
}

template <typename T, typename S>
void show1dpair(const vector<pair<T, S>> &v)
{
    int n = v.size();
    rep(i, n) cout << v[i].first << wsp << v[i].second << rt;
    return;
}

template <typename T, typename S>
void pairzip(const vector<pair<T, S>> &v, vector<T> &t, vector<T> &s)
{
    int n = v.size();
    rep(i, n)
    {
        t.push_back(v[i].first);
        s.push_back(v[i].second);
    }
    return;
}

template <typename T>
void maxvec(vector<T> &v)
{
    T s = v[0];
    int n = v.size();
    rep(i, n - 1)
    {
        if (s > v[i + 1])
        {
            v[i + 1] = s;
        }
        s = v[i + 1];
    }
}

template <typename T, typename S>
bool myfind(T t, S s)
{
    return find(t.cbegin(), t.cend(), s) != t.cend();
}

bool check(int y, int x, int h, int w)
{
    return 0 <= y && y < h && 0 <= x && x < w;
}

template <typename T>
vector<T> cumsum(const vector<T> &v)
{
    T s = 0;
    vector<T> ret;

    rep(i, v.size())
    {
        s += v[i];
        s %= mod;
        ret.emplace_back(s);
    }
    return ret;
}

bool iskadomatsu(int a, int b, int c)
{
    return (a != b && b != c && c != a) && ((a > b && b < c) || (a < b && b > c));
}

VS split(string s, char c)
{
    VS ret;
    string part;
    s += c;
    rep(i, s.length())
    {
        if (s[i] == c)
        {
            ret.emplace_back(part);
            part = "";
        }
        else if (s[i] != c)
        {
            part += s[i];
        }
    }
    return ret;
}

string removezero(string &s)
{
    string ret;
    for (auto z : s)
        if (z != '0')
            ret += z;
    return ret;
}

ll sumdigit(ll x)
{
    ll ans{0};
    while (x > 0)
    {
        ans += x % 10;
        x /= 10;
    }
    return ans;
}

template <typename T, typename S, typename R>
ll pow_mod(T p, S q, R mod)
{
    ll ret = 1, r = p;
    while (q)
    {
        if (q % 2)
            ret *= r, ret %= mod;
        r = (r * r) % mod, q /= 2;
    }
    return ret % mod;
}

ll comb(int a, int b, const VL &frac_list, const VL &frac_inv_list)
{
    ll ret = frac_list[a];
    ret *= frac_inv_list[b];
    ret %= mod;
    ret *= frac_inv_list[a - b];
    ret %= mod;
    return ret;
}

class RangeMinorMaxorSumQuery // 0-index
{
    int const intmax = 2147483647;
    int const intmin = 0;
    vector<int> sgt;
    int n;
    int k;

public:
    RangeMinorMaxorSumQuery(int n1, int f = -1)
    {
        if (f == -1)
            f = intmax;
        else if (f == 0)
            f = intmin;
        int na = 1;
        int ka = 0;
        while (na < n1)
        {
            na *= 2;
            ka++;
        }
        for (int i = 0; i < 2 * na; i++)
            sgt.emplace_back(f);
        n = na;
        k = ka;
    }

    void update_max(int i, int x)
    {
        i += n;
        sgt[i] = x;
        while (i > 1)
        {
            i /= 2;
            sgt[i] = max(sgt[2 * i], sgt[2 * i + 1]);
        }
    }

    int getmax(int a, int b, int k = 1, int l = 0, int r = -1) //閉区間 l <= x < r とする
    {
        if (r == -1)
            r = n;
        if (r <= a || b <= l)
            return intmin;
        if (a == l && b == r)
            return sgt[k];
        else
            return max(getmax(a, min(b, (l + r) / 2), 2 * k, l, (l + r) / 2), getmax(max(a, (l + r) / 2), b, 2 * k + 1, (l + r) / 2, r));
    }

    void printsegtree()
    {
        for (int i = 0; i < 2 * n; i++)
        {
            cout << sgt[i] << " ";
        }
        cout << endl;
    }
};

void dfs(int n, int x, VI &a, VI &parent, VI &ans, const VVI &connect, RangeMinorMaxorSumQuery &sgt)
{
    int y;
    y = sgt.getmax(a[x], a[x] + 1);
    ans[x] = sgt.getmax(0, a[x]) + 1;
    sgt.update_max(a[x], ans[x]);

    for (auto i : connect[x])
        if (i != parent[x])
        {
            parent[i] = x;
            dfs(n, i, a, parent, ans, connect, sgt);
        }

    sgt.update_max(a[x], y);
}



int main()
{

    // cin.tie(0);
    // ios::sync_with_stdio(false);

#ifdef DEBUG
    cout << "DEBUG MODE" << endl;
    ifstream in("input.txt"); //for debug
    cin.rdbuf(in.rdbuf());    //for debug
#endif

    int n, x, u, v;
    cin >> n;
    VI a(n), b(n), parent(n, -1), ans(n, 0);
    VVI connect(n);
    rep(i, n)
    {
        cin >> x;
        a[i] = b[i] = x;
    }
    sort(allpt(b));
    b.erase(unique(allpt(b)), b.end());

    map<int, int> compress;
    rep(i, b.size())
    {
        compress[b[i]] = i;
    }
    rep(i, n) a[i] = compress[a[i]];

    rep(i, 9)
    {
        cin >> u >> v;
        u--, v--;
        connect[u].emplace_back(v);
        connect[v].emplace_back(u);
    }

    RangeMinorMaxorSumQuery sgt(n, 0);

    dfs(n, 0, a, parent, ans, connect, sgt);

    rep(i, n) cout << ans[i] << rt;

    return 0;
}