#include <bits/stdc++.h>
using namespace std;
#define el "\n"
#define FOR(i,a,b) for(int i = (a), _b = (b); i <= _b; i++)
#define FORD(i,a,b) for(int i = (a), _b = (b); i >= _b; i--)
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define lg(x) __lg(x)
#define alla(a,n) a+1,a+n+1
#define ll long long
#define pii pair<int, int>
template <class T> bool maxi(T &x, T y) { if(x < y) { x = y ; return true ;} return false;}
template <class T> bool mini(T &x, T y) { if(x > y) { x = y ; return true ;} return false;}
const int N = 1e5 + 2;
int n, a[N];
vector<int> adj[N];
pair<int, int> E[N];
int sz[N], par[N];
void dfs(int u, int p)
{
sz[u] = 1;
for(int v : adj[u]) if(v != p) {
par[v] = u;
dfs(v, u);
sz[u] += sz[v];
}
}
int head[N], chainID[N], euler[N], pos[N], timer = 0, mtc = 1;
void hld(int u, int p)
{
if(!head[mtc]) {
head[mtc] = u;
}
chainID[u] = mtc;
euler[++timer] = u;
pos[u] = timer;
int ma = 0;
for(int v : adj[u]) if(v != p) {
if(!ma || sz[v] > sz[ma]) ma = v;
}
if(ma) hld(ma, u);
for(int v : adj[u]) if(v != p && v != ma) {
mtc++;
hld(v, u);
}
}
int st[4 * N], bit[N];
void upd(int x, int v)
{
for(; x <= n; x += x & -x) bit[x] += v;
}
int get(int x)
{
int ans = 0;
for(; x >= 1; x -= x & -x) ans += bit[x];
return ans;
}
void down(int id)
{
if(st[id] == -1) return;
st[id << 1] = st[id << 1 | 1] = st[id];
}
void upd(int id, int l, int r, int u, int v, int val)
{
if(r < u || v < l) return;
if(u <= l && r <= v) {
st[id] = val;
return;
}
int mid = (r + l) >> 1;
down(id);
upd(id << 1, l, mid, u, v, val);
upd(id << 1 | 1, mid + 1, r, u, v, val);
if(st[id << 1] == st[id << 1 | 1]) st[id] = st[id << 1];
else st[id] = -1;
}
stack<pair<int, int>> memo;
int get(int id, int l, int r, int u, int v)
{
if(r < u || v < l) return 0;
if(u <= l && r <= v && st[id] != -1) {
int res = (r - l + 1) * get(st[id] - 1);
upd(st[id], r - l + 1);
memo.push({st[id], r - l + 1});
return res;
}
int mid = (r + l) >> 1;
down(id);
return get(id << 1 | 1, mid + 1, r, u, v) + get(id << 1, l, mid, u, v);
}
int calc(int u)
{
upd(1, 1, n, pos[u], pos[u], a[u]);
int val = a[u];
u = par[u];
int ans = 0;
while(u > 0) {
ans += get(1, 1, n, pos[head[chainID[u]]], pos[u]);
upd(1, 1, n, pos[head[chainID[u]]], pos[u], val);
u = par[head[chainID[u]]];
}
while(!memo.empty()) {
int pos = memo.top().first;
int val = memo.top().second;
memo.pop();
upd(pos, -val);
}
return ans;
}
void solve()
{
cin >> n;
vector<int> V;
FOR(i, 1, n) cin >> a[i], V.push_back(a[i]);
sort(all(V));
V.resize(unique(all(V)) - V.begin());
FOR(i, 1, n) {
a[i] = lower_bound(all(V), a[i]) - V.begin() + 1;
}
FOR(i, 1, n - 1) {
int x, y; cin >> x >> y;
E[i] = {x, y};
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1, 1);
hld(1, 1);
memset(st, -1, sizeof st);
calc(1);
FOR(i, 1, n - 1) {
cout << calc(E[i].second) << el;
}
}
main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define __Azul__ "JOI18_construction"
if(fopen(__Azul__".inp", "r")) {
freopen(__Azul__".inp", "r", stdin);
freopen(__Azul__".out", "w", stdout);
}
solve();
cerr << "\nTime" << 0.001 * clock() << "s "; return 0;
}
Compilation message (stderr)
construction.cpp:157:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
157 | main()
| ^~~~
construction.cpp: In function 'int main()':
construction.cpp:163:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
163 | freopen(__Azul__".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
construction.cpp:164:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
164 | freopen(__Azul__".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |