#include <bits/stdc++.h>
#define tt cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0)
#define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define db long double
#define sz(a) ((int)(a).size())
#define pb emplace_back
#define pf emplace_front
#define pob pop_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define ins emplace
#define mp make_pair
using namespace std;
const int MOD = 1e9+7, MAXN = 1e5+5;
const string NAME = "";
int n,a[MAXN],val[MAXN];
int sz[MAXN],par[MAXN],head[MAXN],pos[MAXN],depth[MAXN],curpos=0;
pair<int,int> edge[MAXN];
vector<int> adj[MAXN];
vector<pair<int,int>> v[MAXN];
void compress(){
vector<int> v;
for(int i = 1; i<=n; ++i)
v.pb(a[i]);
sort(v.begin(),v.end());
for(int i = 1; i<=n; ++i)
a[i]=lb(v.begin(),v.end(),a[i])-v.begin()+1;
}
void dfs(int u){
sz[u]=1;
for(int& v : adj[u])
if(v!=par[u]) depth[v]=depth[u]+1, par[v]=u, dfs(v), sz[u]+=sz[v];
}
void HLD(int u, int curhead){
head[u]=curhead, pos[u]=++curpos;
int heavy=0;
for(int& v : adj[u])
if(v!=par[u]&&sz[v]>sz[heavy]) heavy=v;
if(heavy!=0) HLD(heavy,curhead);
for(int& v : adj[u])
if(v!=par[u]&&v!=heavy) HLD(v,v);
}
void update(int x, int val){
while(x>0){
int Head=head[x];
while(!v[Head].empty()&&pos[v[Head].back().fi]<=pos[x]) v[Head].pob();
v[Head].pb(x,val), x=par[Head];
}
}
int bit[MAXN];
bool vis[MAXN];
void reset(int pos){
while(pos<=n) bit[pos]=0, pos+=pos&-pos;
}
void upd(int pos, int val){
while(pos<=n) bit[pos]+=val, pos+=pos&-pos;
}
int getsum(int pos){
int rs=0;
while(pos>0) rs+=bit[pos], pos-=pos&-pos;
return rs;
}
ll query(int x){
ll rs=0;
vector<int> tmp;
while(x>0){
int Head=head[x];
vector<pair<int,int>>& vec=v[Head];
if(!vec.empty()){
int j=sz(vec)-1;
for(int i = 0; i<sz(vec)-1; ++i)
if(pos[vec[i+1].fi]<=pos[x]){
j=i;
break;
}
for(; j<sz(vec); ++j){
int cnt=(j<sz(vec)-1 ? min(pos[vec[j].fi],pos[x])-pos[vec[j+1].fi] : min(pos[vec[j].fi],pos[x])-pos[Head]+1);
rs+=1ll*cnt*getsum(vec[j].se-1), upd(vec[j].se,cnt);
if(!vis[vec[j].se]) vis[vec[j].se]=1, tmp.pb(vec[j].se);
}
}
x=par[Head];
}
for(int& x : tmp)
reset(x), vis[x]=0;
return rs;
}
int main()
{
tt;
if(fopen((NAME + ".INP").c_str(), "r")) fo;
cin >> n;
for(int i = 1; i<=n; ++i)
cin >> a[i];
for(int i = 1; i<n; ++i){
cin >> edge[i].fi >> edge[i].se;
adj[edge[i].fi].pb(edge[i].se), adj[edge[i].se].pb(edge[i].fi);
}
compress();
dfs(1);
HLD(1,1);
for(int i = 1; i<n; ++i){
cout << query(edge[i].fi) << "\n";
update(edge[i].se,a[edge[i].se]);
}
}
컴파일 시 표준 에러 (stderr) 메시지
construction.cpp: In function 'int main()':
construction.cpp:3:19: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
construction.cpp:97:45: note: in expansion of macro 'fo'
97 | if(fopen((NAME + ".INP").c_str(), "r")) fo;
| ^~
construction.cpp:3:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
construction.cpp:97:45: note: in expansion of macro 'fo'
97 | if(fopen((NAME + ".INP").c_str(), "r")) fo;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |