#include<bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define pb push_back
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T>
using ord = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;
const ll N=1e6,mod=1e9 +7;
ll a,b,d,n,m,l,k,q;
ll t[N];
ll p[N];
ll x,y;
ll c;
ll cal(ll y){
ll res=0,ind=0;
ord<pair<ll,ll>>x;
x.insert({t[y],ind++});
t[y]=c;
while(y!=p[y]){
y=p[y];
res+=x.order_of_key(make_pair(t[y],0));
x.insert({t[y],ind++});
t[y]=c;
}
return res;
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
ll T=1;
// cin>>T;
while(T--){
cin>>n;
for(ll i=0;i<n;i++){
cin>>t[i];
p[i]=-1;
}
p[0]=0;
for(ll i=0;i<n-1;i++){
// ll res=0;
cin>>x>>y;
x--;y--;
p[y]=x;
c=t[y];
cout<<cal(x)<<"\n";
}
}
return 0;
}
/*
4 10
3 6 2 9
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |