This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int ll
#define MAX 1000001
//#define INF INT_MAX
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ins insert
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define lb(a,b) lower_bound(all(a),b)
#define ub(a,b) upper_bound(all(a),b)
#define sortv(a) sort(all(a))
#define outputar(a,b){\
for(int i=0;i<b;i++){\
cout << a[i] << " ";\
}\
cout << endl;\
}
#define outputvec(a){\
for(auto x:a){\
cout << (int)x << " ";\
}\
cout << endl;\
}
#define reset(a,n,v){\
for(int i=0;i<n;i++){\
a[i]=v;\
}\
}
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef tuple<ll,ll,ll> tll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef double db;
typedef long double ldb;
inline void USACO(string filename){
freopen((filename+".in").c_str(),"r",stdin);
freopen((filename+".out").c_str(),"w",stdout);
}
int n,q,t=1,m,k,x,y,z,x2,y2,z2,a[MAX],b[MAX],d[MAX];
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//string s[MAX],str[MAX];
//int e[1001][1001];
string s1,s2,s3;
const int mod = 998244353;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
ll sum[MAX],cnt=1;
void update(ll x,ll v){
while(x<=cnt){
sum[x]+=v;
x+=x&-x;
}
}
ll query(ll x){
ll res=0;
while(x>=1){
res+=sum[x];
x-=(x&-x);
}
return res;
}
vector<vector<int>> g;
int pre[MAX];
void dfs(int v,int prev){
pre[v]=prev;
for(auto x:g[v]){
if(x==prev){
continue;
}
dfs(x,v);
}
}
void solve(){
cin >> n;
g.clear();
g.resize(n+1);
map<int,int> c;
set<int> e;
for(int i=1;i<=n;i++){
cin >> d[i];
e.ins(d[i]);
}
for(auto x:e){
c[x]=cnt;
cnt++;
}
for(int i=0;i<n-1;i++){
cin >> a[i] >> b[i];
g[a[i]].pb(b[i]);
g[b[i]].pb(a[i]);
}
dfs(1,-1);
for(int i=0;i<n-1;i++){
vector<int> f;
int h=a[i];
while(h!=-1){
f.pb(d[h]);
h=pre[h];
}
reverse(all(f));
int res=0;
for(int i=0;i<=cnt;i++){
sum[i]=0;
}
for(auto x:f){
res+=query(cnt)-query(c[x]);
update(c[x],1);
}
cout << res << "\n";
h=a[i];
while(h!=-1){
d[h]=d[b[i]];
h=pre[h];
}
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("input13.txt","r",stdin);
//cin >> t;
ll cnt1=1;
while(t--){
solve();
cnt1++;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |