#include<bits/stdc++.h>
#define MAXN 200007
using namespace std;
const int inf=1e9+7;
int n,c[MAXN],a[MAXN],b[MAXN],sz[MAXN],heavy[MAXN],parent[MAXN];
int pos[MAXN],head[MAXN],tt,lazy[4*MAXN],dep[MAXN];
pair<int,int> tree[4*MAXN],curr;
vector<int> v[MAXN];
vector< pair<int,int> > w;
void dfs(int x,int p,int d){
parent[x]=p; dep[x]=d;
sz[x]=1;
for(int i=0;i<v[x].size();i++){
if(v[x][i]==p)continue;
dfs(v[x][i],x,d+1);
if(sz[v[x][i]]>sz[heavy[x]])heavy[x]=v[x][i];
sz[x]+=sz[v[x][i]];
}
}
void decompose(int x,int p,int h){
tt++; pos[x]=tt; head[x]=h;
if(heavy[x]!=0){
decompose(heavy[x],x,h);
}
for(int i=0;i<v[x].size();i++){
if(v[x][i]==p or v[x][i]==heavy[x])continue;
decompose(v[x][i],x,v[x][i]);
}
}
void psh(int v){
if(lazy[v]==0)return;
tree[2*v].first=tree[2*v].second=lazy[v];
tree[2*v+1].first=tree[2*v+1].second=lazy[v];
lazy[2*v]=lazy[2*v+1]=lazy[v];
lazy[v]=0;
}
pair<int,int> combine(pair<int,int> fr,pair<int,int> sc){
return {min(fr.first,sc.first),max(fr.second,sc.second)};
}
void update(int v,int l,int r,int ll,int rr,int val){
if(ll>rr)return;
if(l==ll and r==rr){
tree[v].first=tree[v].second=val;
lazy[v]=val;
}else{
psh(v);
int tt=(l+r)/2;
update(2*v,l,tt,ll,min(tt,rr),val);
update(2*v+1,tt+1,r,max(tt+1,ll),rr,val);
tree[v]=combine(tree[2*v],tree[2*v+1]);
}
}
pair<int,int> info(int v,int l,int r,int ll,int rr){
if(ll>rr)return{inf,0};
if(l==ll and r==rr){
return tree[v];
}else{
psh(v);
int tt=(l+r)/2;
return combine( info(2*v,l,tt,ll,min(tt,rr)) , info(2*v+1,tt+1,r,max(tt+1,ll),rr) );
}
}
void upgrade(int x,int val){
while(x!=0){
update(1,1,n,pos[head[x]],pos[x],val);
x=parent[head[x]];
}
}
void add_segments(int l,int r){
while(r>=l){
int ll=l-1,rr=r,mid;
while(ll+1<rr){
mid=(ll+rr)/2;
curr=info(1,1,n,mid,r);
if(curr.first==curr.second){
rr=mid;
}else{
ll=mid;
}
}
curr=info(1,1,n,rr,r);
w.push_back({curr.first,r-rr+1});
r=rr-1;
}
}
long long query(int x){
long long ans=0;
w.clear();
while(x!=0){
add_segments(pos[head[x]],pos[x]);
x=parent[head[x]];
}
for(int i=0;i<w.size();i++){
for(int f=i+1;f<w.size();f++){
if(w[i].first<w[f].first)ans+=(long long) w[i].second*w[f].second;
}
}
return ans;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>c[i];
}
for(int i=1;i<=n-1;i++){
cin>>a[i]>>b[i];
v[a[i]].push_back(b[i]);
v[b[i]].push_back(a[i]);
}
dfs(1,0,1);
decompose(1,0,1);
for(int i=1;i<=n;i++){
update(1,1,n,pos[i],pos[i],c[i]);
}
for(int i=1;i<=n-1;i++){
cout<<query(a[i])<<"\n";
upgrade(b[i],c[b[i]]);
}
return 0;
}
Compilation message
construction.cpp: In function 'void dfs(int, int, int)':
construction.cpp:19:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
construction.cpp: In function 'void decompose(int, int, int)':
construction.cpp:35:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
construction.cpp: In function 'long long int query(int)':
construction.cpp:121:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
121 | for(int i=0;i<w.size();i++){
| ~^~~~~~~~~
construction.cpp:122:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
122 | for(int f=i+1;f<w.size();f++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14984 KB |
Output is correct |
3 |
Correct |
2 ms |
14940 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
14940 KB |
Output is correct |
6 |
Correct |
3 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
3 ms |
14940 KB |
Output is correct |
9 |
Correct |
3 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14996 KB |
Output is correct |
11 |
Correct |
4 ms |
14940 KB |
Output is correct |
12 |
Correct |
4 ms |
14940 KB |
Output is correct |
13 |
Correct |
5 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14936 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14940 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
3 ms |
14936 KB |
Output is correct |
19 |
Correct |
3 ms |
14940 KB |
Output is correct |
20 |
Correct |
3 ms |
14940 KB |
Output is correct |
21 |
Correct |
3 ms |
14940 KB |
Output is correct |
22 |
Correct |
4 ms |
14952 KB |
Output is correct |
23 |
Correct |
3 ms |
14940 KB |
Output is correct |
24 |
Correct |
3 ms |
14940 KB |
Output is correct |
25 |
Correct |
4 ms |
14940 KB |
Output is correct |
26 |
Correct |
3 ms |
14936 KB |
Output is correct |
27 |
Correct |
3 ms |
14940 KB |
Output is correct |
28 |
Correct |
3 ms |
14940 KB |
Output is correct |
29 |
Correct |
3 ms |
14936 KB |
Output is correct |
30 |
Correct |
3 ms |
14940 KB |
Output is correct |
31 |
Correct |
3 ms |
14936 KB |
Output is correct |
32 |
Correct |
3 ms |
14940 KB |
Output is correct |
33 |
Correct |
3 ms |
14940 KB |
Output is correct |
34 |
Correct |
3 ms |
14956 KB |
Output is correct |
35 |
Correct |
4 ms |
15196 KB |
Output is correct |
36 |
Correct |
3 ms |
14940 KB |
Output is correct |
37 |
Correct |
4 ms |
14936 KB |
Output is correct |
38 |
Correct |
3 ms |
14940 KB |
Output is correct |
39 |
Correct |
3 ms |
14940 KB |
Output is correct |
40 |
Correct |
3 ms |
14940 KB |
Output is correct |
41 |
Correct |
3 ms |
14940 KB |
Output is correct |
42 |
Correct |
3 ms |
14940 KB |
Output is correct |
43 |
Correct |
4 ms |
14940 KB |
Output is correct |
44 |
Correct |
4 ms |
14944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14984 KB |
Output is correct |
3 |
Correct |
2 ms |
14940 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
14940 KB |
Output is correct |
6 |
Correct |
3 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
3 ms |
14940 KB |
Output is correct |
9 |
Correct |
3 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14996 KB |
Output is correct |
11 |
Correct |
4 ms |
14940 KB |
Output is correct |
12 |
Correct |
4 ms |
14940 KB |
Output is correct |
13 |
Correct |
5 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14936 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14940 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
3 ms |
14936 KB |
Output is correct |
19 |
Correct |
3 ms |
14940 KB |
Output is correct |
20 |
Correct |
3 ms |
14940 KB |
Output is correct |
21 |
Correct |
3 ms |
14940 KB |
Output is correct |
22 |
Correct |
4 ms |
14952 KB |
Output is correct |
23 |
Correct |
3 ms |
14940 KB |
Output is correct |
24 |
Correct |
3 ms |
14940 KB |
Output is correct |
25 |
Correct |
4 ms |
14940 KB |
Output is correct |
26 |
Correct |
3 ms |
14936 KB |
Output is correct |
27 |
Correct |
3 ms |
14940 KB |
Output is correct |
28 |
Correct |
3 ms |
14940 KB |
Output is correct |
29 |
Correct |
3 ms |
14936 KB |
Output is correct |
30 |
Correct |
3 ms |
14940 KB |
Output is correct |
31 |
Correct |
3 ms |
14936 KB |
Output is correct |
32 |
Correct |
3 ms |
14940 KB |
Output is correct |
33 |
Correct |
3 ms |
14940 KB |
Output is correct |
34 |
Correct |
3 ms |
14956 KB |
Output is correct |
35 |
Correct |
4 ms |
15196 KB |
Output is correct |
36 |
Correct |
3 ms |
14940 KB |
Output is correct |
37 |
Correct |
4 ms |
14936 KB |
Output is correct |
38 |
Correct |
3 ms |
14940 KB |
Output is correct |
39 |
Correct |
3 ms |
14940 KB |
Output is correct |
40 |
Correct |
3 ms |
14940 KB |
Output is correct |
41 |
Correct |
3 ms |
14940 KB |
Output is correct |
42 |
Correct |
3 ms |
14940 KB |
Output is correct |
43 |
Correct |
4 ms |
14940 KB |
Output is correct |
44 |
Correct |
4 ms |
14944 KB |
Output is correct |
45 |
Correct |
4 ms |
14936 KB |
Output is correct |
46 |
Correct |
11 ms |
15212 KB |
Output is correct |
47 |
Correct |
13 ms |
15600 KB |
Output is correct |
48 |
Correct |
11 ms |
15196 KB |
Output is correct |
49 |
Correct |
13 ms |
15452 KB |
Output is correct |
50 |
Correct |
12 ms |
15448 KB |
Output is correct |
51 |
Correct |
12 ms |
15332 KB |
Output is correct |
52 |
Correct |
16 ms |
15340 KB |
Output is correct |
53 |
Correct |
19 ms |
15196 KB |
Output is correct |
54 |
Correct |
19 ms |
15196 KB |
Output is correct |
55 |
Correct |
19 ms |
15356 KB |
Output is correct |
56 |
Correct |
15 ms |
15192 KB |
Output is correct |
57 |
Correct |
15 ms |
14940 KB |
Output is correct |
58 |
Correct |
16 ms |
15192 KB |
Output is correct |
59 |
Correct |
16 ms |
15196 KB |
Output is correct |
60 |
Correct |
16 ms |
15192 KB |
Output is correct |
61 |
Correct |
16 ms |
15196 KB |
Output is correct |
62 |
Correct |
16 ms |
15192 KB |
Output is correct |
63 |
Correct |
16 ms |
15192 KB |
Output is correct |
64 |
Correct |
10 ms |
14940 KB |
Output is correct |
65 |
Correct |
10 ms |
15196 KB |
Output is correct |
66 |
Correct |
11 ms |
15196 KB |
Output is correct |
67 |
Correct |
11 ms |
15188 KB |
Output is correct |
68 |
Correct |
12 ms |
15452 KB |
Output is correct |
69 |
Correct |
16 ms |
15196 KB |
Output is correct |
70 |
Correct |
19 ms |
15196 KB |
Output is correct |
71 |
Correct |
15 ms |
15328 KB |
Output is correct |
72 |
Correct |
15 ms |
15196 KB |
Output is correct |
73 |
Correct |
16 ms |
14940 KB |
Output is correct |
74 |
Correct |
16 ms |
15196 KB |
Output is correct |
75 |
Correct |
14 ms |
15448 KB |
Output is correct |
76 |
Correct |
14 ms |
15196 KB |
Output is correct |
77 |
Correct |
13 ms |
15004 KB |
Output is correct |
78 |
Correct |
14 ms |
15192 KB |
Output is correct |
79 |
Correct |
16 ms |
15196 KB |
Output is correct |
80 |
Correct |
13 ms |
15212 KB |
Output is correct |
81 |
Correct |
16 ms |
15196 KB |
Output is correct |
82 |
Correct |
16 ms |
15248 KB |
Output is correct |
83 |
Correct |
15 ms |
15196 KB |
Output is correct |
84 |
Correct |
15 ms |
15196 KB |
Output is correct |
85 |
Correct |
15 ms |
15196 KB |
Output is correct |
86 |
Correct |
14 ms |
15196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14940 KB |
Output is correct |
2 |
Correct |
2 ms |
14984 KB |
Output is correct |
3 |
Correct |
2 ms |
14940 KB |
Output is correct |
4 |
Correct |
3 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
14940 KB |
Output is correct |
6 |
Correct |
3 ms |
14940 KB |
Output is correct |
7 |
Correct |
3 ms |
14940 KB |
Output is correct |
8 |
Correct |
3 ms |
14940 KB |
Output is correct |
9 |
Correct |
3 ms |
14940 KB |
Output is correct |
10 |
Correct |
3 ms |
14996 KB |
Output is correct |
11 |
Correct |
4 ms |
14940 KB |
Output is correct |
12 |
Correct |
4 ms |
14940 KB |
Output is correct |
13 |
Correct |
5 ms |
14940 KB |
Output is correct |
14 |
Correct |
3 ms |
14936 KB |
Output is correct |
15 |
Correct |
4 ms |
14940 KB |
Output is correct |
16 |
Correct |
3 ms |
14940 KB |
Output is correct |
17 |
Correct |
3 ms |
14940 KB |
Output is correct |
18 |
Correct |
3 ms |
14936 KB |
Output is correct |
19 |
Correct |
3 ms |
14940 KB |
Output is correct |
20 |
Correct |
3 ms |
14940 KB |
Output is correct |
21 |
Correct |
3 ms |
14940 KB |
Output is correct |
22 |
Correct |
4 ms |
14952 KB |
Output is correct |
23 |
Correct |
3 ms |
14940 KB |
Output is correct |
24 |
Correct |
3 ms |
14940 KB |
Output is correct |
25 |
Correct |
4 ms |
14940 KB |
Output is correct |
26 |
Correct |
3 ms |
14936 KB |
Output is correct |
27 |
Correct |
3 ms |
14940 KB |
Output is correct |
28 |
Correct |
3 ms |
14940 KB |
Output is correct |
29 |
Correct |
3 ms |
14936 KB |
Output is correct |
30 |
Correct |
3 ms |
14940 KB |
Output is correct |
31 |
Correct |
3 ms |
14936 KB |
Output is correct |
32 |
Correct |
3 ms |
14940 KB |
Output is correct |
33 |
Correct |
3 ms |
14940 KB |
Output is correct |
34 |
Correct |
3 ms |
14956 KB |
Output is correct |
35 |
Correct |
4 ms |
15196 KB |
Output is correct |
36 |
Correct |
3 ms |
14940 KB |
Output is correct |
37 |
Correct |
4 ms |
14936 KB |
Output is correct |
38 |
Correct |
3 ms |
14940 KB |
Output is correct |
39 |
Correct |
3 ms |
14940 KB |
Output is correct |
40 |
Correct |
3 ms |
14940 KB |
Output is correct |
41 |
Correct |
3 ms |
14940 KB |
Output is correct |
42 |
Correct |
3 ms |
14940 KB |
Output is correct |
43 |
Correct |
4 ms |
14940 KB |
Output is correct |
44 |
Correct |
4 ms |
14944 KB |
Output is correct |
45 |
Correct |
4 ms |
14936 KB |
Output is correct |
46 |
Correct |
11 ms |
15212 KB |
Output is correct |
47 |
Correct |
13 ms |
15600 KB |
Output is correct |
48 |
Correct |
11 ms |
15196 KB |
Output is correct |
49 |
Correct |
13 ms |
15452 KB |
Output is correct |
50 |
Correct |
12 ms |
15448 KB |
Output is correct |
51 |
Correct |
12 ms |
15332 KB |
Output is correct |
52 |
Correct |
16 ms |
15340 KB |
Output is correct |
53 |
Correct |
19 ms |
15196 KB |
Output is correct |
54 |
Correct |
19 ms |
15196 KB |
Output is correct |
55 |
Correct |
19 ms |
15356 KB |
Output is correct |
56 |
Correct |
15 ms |
15192 KB |
Output is correct |
57 |
Correct |
15 ms |
14940 KB |
Output is correct |
58 |
Correct |
16 ms |
15192 KB |
Output is correct |
59 |
Correct |
16 ms |
15196 KB |
Output is correct |
60 |
Correct |
16 ms |
15192 KB |
Output is correct |
61 |
Correct |
16 ms |
15196 KB |
Output is correct |
62 |
Correct |
16 ms |
15192 KB |
Output is correct |
63 |
Correct |
16 ms |
15192 KB |
Output is correct |
64 |
Correct |
10 ms |
14940 KB |
Output is correct |
65 |
Correct |
10 ms |
15196 KB |
Output is correct |
66 |
Correct |
11 ms |
15196 KB |
Output is correct |
67 |
Correct |
11 ms |
15188 KB |
Output is correct |
68 |
Correct |
12 ms |
15452 KB |
Output is correct |
69 |
Correct |
16 ms |
15196 KB |
Output is correct |
70 |
Correct |
19 ms |
15196 KB |
Output is correct |
71 |
Correct |
15 ms |
15328 KB |
Output is correct |
72 |
Correct |
15 ms |
15196 KB |
Output is correct |
73 |
Correct |
16 ms |
14940 KB |
Output is correct |
74 |
Correct |
16 ms |
15196 KB |
Output is correct |
75 |
Correct |
14 ms |
15448 KB |
Output is correct |
76 |
Correct |
14 ms |
15196 KB |
Output is correct |
77 |
Correct |
13 ms |
15004 KB |
Output is correct |
78 |
Correct |
14 ms |
15192 KB |
Output is correct |
79 |
Correct |
16 ms |
15196 KB |
Output is correct |
80 |
Correct |
13 ms |
15212 KB |
Output is correct |
81 |
Correct |
16 ms |
15196 KB |
Output is correct |
82 |
Correct |
16 ms |
15248 KB |
Output is correct |
83 |
Correct |
15 ms |
15196 KB |
Output is correct |
84 |
Correct |
15 ms |
15196 KB |
Output is correct |
85 |
Correct |
15 ms |
15196 KB |
Output is correct |
86 |
Correct |
14 ms |
15196 KB |
Output is correct |
87 |
Correct |
28 ms |
15452 KB |
Output is correct |
88 |
Correct |
96 ms |
16868 KB |
Output is correct |
89 |
Correct |
392 ms |
24756 KB |
Output is correct |
90 |
Correct |
400 ms |
24656 KB |
Output is correct |
91 |
Correct |
392 ms |
24564 KB |
Output is correct |
92 |
Correct |
467 ms |
32416 KB |
Output is correct |
93 |
Correct |
469 ms |
32304 KB |
Output is correct |
94 |
Correct |
459 ms |
32336 KB |
Output is correct |
95 |
Correct |
1541 ms |
28736 KB |
Output is correct |
96 |
Execution timed out |
2021 ms |
29252 KB |
Time limit exceeded |
97 |
Halted |
0 ms |
0 KB |
- |