Language: C++

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize ("Ofast")
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
 
using namespace __gnu_pbds;
using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
 
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<long long,long long> ii; 	
typedef complex<long double> com;

const ll mod = 1e9+7;

ll n;
ll current = 1;
vector <vector<ll>> graph;
vector <ll> depth;
vector <ll> parent;
vector <ll> heavy;
vector <ll> head;
vector <ll> pos;
vector <ll> s;


void modify(ll p,ll v,ll id = 1, ll l = 0,ll r = current){
    if (r - l < 2){
        s[id] = v;
        return;
    }
    ll mid=(l+r)/2;
    if (p < mid) modify(p,v,id*2,l,mid);
    else modify(p,v,id*2+1,mid,r);
    s[id] = max(s[id*2],s[id*2+1]);
}
 
ll que(ll x,ll y,ll id = 1,ll l = 0,ll r = current){
    if (y <= l || x >= r) return 0;
    if (x <= l && r <= y){
        return s[id];
    }
    ll mid = (l+r)/2;
    return max(que(x,y,id*2,l,mid),que(x,y,id*2+1,mid,r));
}
 
ll dfs1(ll u = 1){
    ll s = 1;
    ll ms = 0;
    for (int z=0;z<graph[u].size();z++){
        if (graph[u][z] == parent[u]) continue;
        parent[graph[u][z]] = u;
        depth[graph[u][z]] = depth[u]+1;
        ll sv = dfs1(graph[u][z]);
        s += sv;
        if (sv > ms){
            ms = sv;
            heavy[u] = graph[u][z];
        }
    }
    return s;
}
 
void dfs2(ll u = 1,ll h = 1){
    head[u] = h;
    pos[u] = current++;
    if (heavy[u] != -1)dfs2(heavy[u],h);
    for (int z=0;z<graph[u].size();z++){
        if (graph[u][z] == parent[u]) continue;
        if (graph[u][z] == heavy[u]) continue;
        dfs2(graph[u][z],graph[u][z]);
    }
}
 


ll query(ll u,ll v){
    ll ans1 = -LLONG_MAX;
    while (head[u] != head[v]){
        if (depth[head[u]] > depth[head[v]]) swap(u,v);
        ans1 = max(ans1,que(pos[head[v]],pos[v]+1));
        v = parent[head[v]];
    }
    if (depth[u] > depth[v]) swap(u,v);
    ans1 = max(ans1,que(pos[u],pos[v]+1));
    return ans1;
}
 

void reset(){
    current = 1;
    graph.resize(n+1);
    depth.resize(n+1);
    parent.resize(n+1);
    heavy.resize(n+1,-1);
    head.resize(n+1);
    pos.resize(n+1);
    s.resize((n+1)*4,0);
}

vector <ll> dp;

struct stu{
    ll u,w;
    bool operator<(const stu& rhs) const{
        return w < rhs.w;
    }
};

vector <stu> arr;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
    cin >> n;
    reset();
    dp.resize(n+1);
    map <ll,vector<ll>> mapp;
    for (int z=1;z<=n;z++){
        ll temp;
        cin >> temp;
        mapp[temp].push_back(z);
    }
    for (int z=0;z<n-1;z++){
        ll u,v;
        cin >> u >> v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    dfs1();
    dfs2();
    for (auto it=mapp.begin();it != mapp.end();it++){
        pair <ll,vector<ll>> i = *it;
        for (int z=0;z<i.second.size();z++){
            dp[i.second[z]] = query(1,i.second[z])+1;
        }
        for (int z=0;z<i.second.size();z++){
            modify(pos[i.second[z]],dp[i.second[z]]);
        }
    }
    for (int z=1;z<=n;z++){
        cout << query(1,i.second[z]) << endl;
    }
} 

Compiler output:
a.cc: In function 'int main()':
a.cc:155:25: error: 'i' was not declared in this scope
  155 |         cout << query(1,i.second[z]) << endl;
      |                         ^