Language: C++

#include <iostream>
#include <iosfwd>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cassert>
#include <cctype>
#include <climits>
#include <vector>
#include <bitset>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <deque>
#include <string>
#include <list>
#include <iterator>
#include <sstream>
#include <complex>
#include <fstream>
#include <functional>
#include <numeric>
#include <utility>
#include <algorithm>

using namespace std;

// MACROS_BEGIN
#define CLEAR() cerr << endl;
#define LET(x, a) __typeof(a) x = a
#define FOREACH(it, v) for (LET(it, (v).begin()); it != (v).end(); ++it)
#define REPEAT(i, n) for (int i = 0; i < (n); ++i)
// MACROS_END

// GENERIC_UTILITIES_BEGIN
template <class T> inline int size(const T& c) {return (int) c.size();}
inline long long two(int x) {return (1LL << (x));}

vector<string> split(string s, string delim) {
    s += delim[0];
    string tmp;
    vector<string> result;
    for (int i = 0; i < size(s); ++i) {
        if (delim.find(s[i]) == string::npos) {
            tmp.push_back(s[i]);
        }
        else {
            if (tmp != "") result.push_back(tmp);
            tmp.clear();
        }
    }
    return result;
}
// GENERIC_UTILITIES_END

// FAST_IO_BEGIN
// FAST_IO_END

// STANDARD_IO_BEGIN
#ifndef USING_FAST_IO
int readInt() {int N = -1; scanf("%d", &N); return N;}
double readDouble() {double D; scanf("%lf", &D); return D;}
string readString() {char buffer[1 << 20]; scanf("%s", buffer); return buffer;}
long long readLongLong() {long long N = -1; scanf("%lld", &N); return N;}
#endif // NOT DEFINED USING_FAST_IO
// STANDARD_IO_END


// OUTPUT_UTILITIES_BEGIN
template <class A, class B> ostream& operator << (ostream& o, const pair<A, B>& p);
template <class T> ostream& operator << (ostream& o, const vector<T>& v);
template <class A, class B> ostream& operator << (ostream& o, const map<A, B>& m);
template <class T> ostream& operator << (ostream& o, const set<T>& s);
template <class T> ostream& operator << (ostream& o, const queue<T>& q);
template <class T> ostream& operator << (ostream& o, const stack<T>& s);

template <class A, class B> ostream& operator << (ostream& o, const pair<A, B>& p) {
    o << "(" << p.first << "," << p.second << ")"; return o;
}

template <class T> ostream& operator << (ostream& o, const vector<T>& v) {
    o << "{"; bool first = true; FOREACH(it, v) { if (!first) o << ","; first = false; o << *it; } return o << "}";
}

template <class A, class B> ostream& operator << (ostream& o, const map<A, B>& m) {
    o << "{"; bool first = true; FOREACH(it, m) { if (!first) o << ","; first = false; o << *it; } return o << "}";
}

template <class T> ostream& operator << (ostream& o, const set<T>& s) {
    o << "{"; bool first = true; FOREACH(it, s) { if (!first) o << ","; first = false; o << *it; } return o << "}";
}

template <class T> ostream& operator << (ostream& o, const queue<T>& q) {
    o << "{"; bool first = true; queue<T> p = q; while (!p.empty()) { if (!first) o << ","; first = false; o << p.front(); p.pop(); } return o << "}";
}

template <class T> ostream& operator << (ostream& o, const stack<T>& s) {
    o << "{"; bool first = true; stack<T> r = s; while (!r.empty()) { if (!first) o << ","; first = false; o << r.top(); r.pop(); } return o << "}";
}
// OUTPUT_UTILITIES_END

// DEBUGGING_UTILITIES_BEGIN
// DEBUGGING SWITCH
#define PHOENIX_DEBUG

#ifdef PHOENIX_DEBUG
#define BUG(x) cerr << __LINE__ << ": " << #x << " = " << x << endl;
#else
#define BUG(...)
#endif
// DEBUGGING_UTILITIES_END

const double epsilon = 1e-8;
const int infinite    = 2000000000; // 2 * 10^9
const long long infiniteLL = 2000000000000000000LL; // 2 * 10^18
const long long modulo = 1000000007;

struct SegmentTree {
    int N;
    vector<int> tree;

    void initialize(int _N) {
        N = _N;
        tree.resize(4 * N, 0);
    }

    void merge(int& u, int& left, int& right) {
        u = max(left, right);
    }

    void update(int node, int b, int e, int idx, int val) {
        if (b > idx || e < idx) {
            return;
        } else if (b == e) {
            tree[node] = val;
        } else {
            int mid = (b + e) / 2, left = 2 * node, right = 2 * node + 1;
            update(left, b, mid, idx, val);
            update(right, mid + 1, e, idx, val);
            merge(tree[node], tree[left], tree[right]);
        }
    }

    int query(int node, int b, int e, int low, int high) {
        if (b > high || e < low) {
            return 0;
        } else if (b >= low && e <= high) {
            return tree[node];
        } else {
            int mid = (b + e) / 2, left = 2 * node, right = 2 * node + 1;
            return max(query(left, b, mid, low, high), query(right, mid + 1, e, low, high));
        }
    }

    int query(int low, int high) {
        return query(1, 0, N - 1, low, high);
    }

    void update(int idx, int val) {
        update(1, 0, N - 1, idx, val);
    }
};

struct Compressor {
    map<int, int> compressed;
    int counter;

    Compressor() {
        counter = 0;
    }

    vector<int> compress(vector<int> _a) {
        vector<int> a = _a;
        sort(a.begin(), a.end());

        for (int i = 0; i < size(a); ++i) {
            if (compressed.find(a[i]) == compressed.end()) {
                compressed[a[i]] = counter++;
            }
        }

        vector<int> result;

        for (int i = 0; i < size(_a); ++i) {
            result.push_back(compressed[_a[i]]);
        }

        return result;
    }
};

struct Task {
    int N;
    vector<int> a;
    vector< vector<int> > adj;
    Compressor compressor;
    vector<int> answer;
    vector< stack<int> > value;
    SegmentTree tree;

    void readInput() {
        N = readInt();
        a.resize(N);

        for (int i = 0; i < N; ++i) {
            a[i] = readInt();
        }

        a = compressor.compress(a);

        adj.resize(N);

        for (int i = 0; i < N - 1; ++i) {
            int u = readInt() - 1, v = readInt() - 1;
            adj[u].push_back(v);
            adj[v].push_back(u);
        }

        answer.resize(N);
        value.resize(N);

        for (int i = 0; i < N; ++i) {
            value[i].push(0);
        }

        tree.initialize(N);
    }

    void dfs(int u, int p) {
        int x = tree.query(0, a[u] - 1);
        value[u].push(x + 1);
        tree.update(a[u], value[u].top());
        answer[u] = x + 1;

        for (int v : adj[u]) {
            if (v != p) {
                dfs(v, u);
            }
        }

        value[u].pop();
        tree.update(a[u], value[u].top());
    }

    void solve() {
        dfs(0, -1);

        for (int i = 0; i < N; ++i) {
            cout << answer[i] << endl;
        }
    }

    void perform() {
        readInput();
        solve();
    }
};

int main()
{
    Task task;
    task.perform();

    return 0;
}

// Powered by PhoenixAI


Compiler output:
a.cc: In function 'std::vector<std::__cxx11::basic_string<char> > split(std::string, std::string)':
a.cc:47:29: error: call of overloaded 'size(std::string&)' is ambiguous
   47 |     for (int i = 0; i < size(s); ++i) {
      |                         ~~~~^~~
a.cc:40:31: note: candidate: 'int size(const T&) [with T = std::__cxx11::basic_string<char>]'
   40 | template <class T> inline int size(const T& c) {return (int) c.size();}
      |                               ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:262:5: note: candidate: 'constexpr decltype (__cont.size()) std::size(const _Container&) [with _Container = __cxx11::basic_string<char>; decltype (__cont.size()) = long unsigned int]'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc: In member function 'std::vector<int> Compressor::compress(std::vector<int>)':
a.cc:180:33: error: call of overloaded 'size(std::vector<int>&)' is ambiguous
  180 |         for (int i = 0; i < size(a); ++i) {
      |                             ~~~~^~~
a.cc:40:31: note: candidate: 'int size(const T&) [with T = std::vector<int>]'
   40 | template <class T> inline int size(const T& c) {return (int) c.size();}
      |                               ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note: candidate: 'constexpr decltype (__cont.size()) std::size(const _Container&) [with _Container = vector<int>; decltype (__cont.size()) = long unsigned int]'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:188:33: error: call of overloaded 'size(std::vector<int>&)' is ambiguous
  188 |         for (int i = 0; i < size(_a); ++i) {
      |                             ~~~~^~~~
a.cc:40:31: note: candidate: 'int size(const T&) [with T = std::vector<int>]'
   40 | template <class T> inline int size(const T& c) {return (int) c.size();}
      |                               ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note: candidate: 'constexpr decltype (__cont.size()) std::size(const _Container&) [with _Container = vector<int>; decltype (__cont.size()) = long unsigned int]'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~