Language: C++

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 7;
const int inf = 1e9 + 7;

multiset<pair<int, int> > s;
multiset<pair<int, int> >::iterator it;
vector<int> g[maxn];
int ans[maxn], ar[maxn], n;

int mx[maxn << 2], mn[maxn << 2], sum[maxn << 2], ad[maxn << 2], se[maxn << 2];
int max_val, min_val, sum_val, v, x, y;

void init(int n){
    for(int i = 0; i <= (n << 2); i++)
        mx[i] = mn[i] = sum[i] = ad[i] = se[i] = 0;
}
void maintain(int l, int r, int o){
    if(se[o] >= 0){
        mx[o] = se[o] + ad[o];
        mn[o] = se[o] + ad[o];
        sum[o] = (se[o] + ad[o]) *(r - l + 1);
        return ;
    }
    mx[o] = 0; mn[o] = 0; sum[o] = 0;
    if(l<r){
        mx[o] = max(mx[o << 1], mx[o << 1 ^ 1]);
        mn[o] = min(mn[o << 1], mn[o << 1 ^ 1]);
        sum[o] = sum[o << 1] + sum[o << 1 ^ 1];
    }
    mx[o] += ad[o];
    mn[o] += ad[o];
    sum[o] += ad[o] * (r - l + 1);
}
void pushdown(int o){
    if(se[o] >= 0){
        se[o << 1] = se[o << 1 ^ 1] = se[o];
        ad[o << 1] = 0; 
        ad[o << 1 ^ 1] = 0;
        se[o] =  - 1;
    }
    if(ad[o]>0){
        ad[o << 1] += ad[o];
        ad[o << 1 ^ 1] += ad[o];
        ad[o] = 0;
    }
}

void add(int l, int r, int o){
    if(l >= x && r <= y){
        ad[o] += v;
    }
    else{
        pushdown(o);
        int mid = (l + r) >> 1;
        if(mid >= x)add(l, mid, o << 1);
        else maintain(l, mid, o << 1);
        if(mid<y)add(mid + 1, r, o << 1 ^ 1);
        else maintain(mid + 1, r, o << 1 ^ 1);
    }
    maintain(l, r, o);
}
void set_func(int l, int r, int o){
    if(l >= x && r <= y){
        se[o] = v;
        ad[o] = 0;
    }
    else{
        int mid = (l + r) >> 1;
        pushdown(o);
        if(mid >= x)set_func(l, mid, o << 1);
        else maintain(l, mid, o << 1);
        if(mid < y)set_func(mid + 1, r, o << 1 ^ 1);
        else maintain(mid + 1, r, o << 1 ^ 1);
    }
    maintain(l, r, o);
}

void que(int l, int r, int o, int add = 0){
    if(se[o] >= 0){
        int v_mid = se[o] + add + ad[o];
        min_val = min(min_val, v_mid);
        max_val = max(max_val, v_mid);
        sum_val += v_mid * (min(y, r) - max(l, x) + 1);
        return ;
    }
    else if(l >= x && r <= y){
        min_val = min(min_val, mn[o] + add);
        max_val = max(max_val, mx[o] + add);
        sum_val += sum[o] + add * (r - l + 1);
        return ;
    }
    add += ad[o];
    int mid = (l + r) >> 1;
    if(mid >= x)que(l, mid, o << 1, add);
    if(mid < y)que(mid + 1, r, o << 1 ^ 1, add);
}


map<int, int> mp;
void dfs(int u, int p = -1){
    /*
    cout << "u = " << u << "\n";
    for(it = s.begin(); it != s.end(); it ++){
        cout << it -> first << ' ' << it -> second << '\n';
    } cout << "\n";
    */
    int now_index = mp[ar[u]];
    x = y = now_index;
    que(1, n, 1);
    int pre_val = min_val;
    max_val = 0;
    x = 1, y = now_index - 1;
    que(1, n, 1);
    x = y = now_index;
    v = max_val + 1;
    ans[u] = max_val + 1;
    set_func(1, n, 1);
    for(int v : g[u]){
        if(v == p)continue;
        dfs(v, u);
    }
    x = y = now_index;
    v = pre_val;
    set_func(1, n, 1);
}
set<int> index;
int main() 
{
    cin >> n;
    for(int i = 1; i <= n; ++i)cin >> ar[i], index.insert(ar[i]);
    int ins = 0;
    for(int x : index){
        mp[x] = ++ins;
    }
    for(int i = 1; i < n; ++i){
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs(1);
    for(int i = 1; i <= n; i++)cout << ans[i] << '\n';

    return 0;
}


Compiler output:
a.cc:128:10: error: 'std::set<int> index' redeclared as different kind of entity
  128 | set<int> index;
      |          ^~~~~
In file included from /usr/include/string.h:462,
                 from /usr/include/c++/14/cstring:43,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:121,
                 from a.cc:1:
/usr/include/strings.h:50:20: note: previous declaration 'const char* index(const char*, int)'
   50 | extern const char *index (const char *__s, int __c)
      |                    ^~~~~
a.cc: In function 'int main()':
a.cc:132:52: error: overloaded function with no contextual type information
  132 |     for(int i = 1; i <= n; ++i)cin >> ar[i], index.insert(ar[i]);
      |                                                    ^~~~~~
a.cc:134:17: error: unable to deduce 'auto&&' from 'index'
  134 |     for(int x : index){
      |                 ^~~~~
a.cc:134:17: note:   couldn't deduce template parameter 'auto'