#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define fi first
#define se second
#define pb push_back
#define endl "\n"
template<typename T>
using ord_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int main() {
//the booster of input and output
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// freopen("In.in", "r", stdin);
// freopen("Out.out", "w", stdout);
//the body
ll n;
cin >> n;
vector<ll> arr(n);
for (ll i = 0; i < n; ++i) {
cin >> arr[i];
}
vector<vector<ll>> pth(n);
pth[0].pb(0);
for (ll i = 0; i < n - 1; ++i) {
ll a, b;
cin>> a >> b;
a--,b--;
pth[b] = pth[a];
ll ans = 0,cnt=0 ,m = pth[b].size();
ord_set<pair<ll,ll>> s;
for (ll j = 0; j < m; ++j) {
ll tmp = s.order_of_key({arr[pth[b][j]] + 1,0});
ans += max(0ll, j-tmp);
s.insert({arr[pth[b][j]], cnt++});
}
for (ll j = 0; j < m; ++j) {
arr[pth[b][j]] = arr[b];
}
pth[b].pb(b);
cout << ans << endl;
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |