#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<(x.F)<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
const int N=1e5+99;
int n,t,col[N],sz[N],up[N],X[N],Y[N],h[N],par[N];
ll fenwik[N];
vector<int> g[N];
vector<pair<int,int> > res,v[N];
void add(int x, int val){ for(x++;x<N;x+=x&-x) fenwik[x]+=val; }
ll get(int x) { ll res=0; for (x++;x>0;x-=x&-x) res+=fenwik[x]; return res; }
void dfs1(int u){
int x=-1;
sz[u]=1;
f(i,0,g[u].size()){
int v=g[u][i];
dfs1(v);
sz[u]+=sz[v];
if(x==-1 || sz[v]>sz[g[u][x]]){
x=i;
}
}
if(x!=-1){
swap(g[u][0],g[u][x]);
}
}
void dfs2(int u){
int t=1;
for(auto v : g[u]){
h[v]=h[u]+1;
if(t){
up[v]=up[u];
}
else{
up[v]=v;
}
dfs2(v);
t=0;
}
v[up[u]].pb({col[u],1});
}
void del(int u,int x,int c){
vector<pair<int,int> > vec;
int t=x;
while(t>0){
int p=min(t,v[u].back().S);
v[u].back().S-=p;
t-=p;
vec.pb({v[u].back().F,p});
if(v[u].back().S>0 || t==0) break;
v[u].pop_back();
}
v[u].pb({c,x});
reverse(all(vec));
for(auto p : vec){
res.pb(p);
}
}
void query(int u,int c){
while(u>0){
del(up[u],h[u]-h[up[u]]+1,c);
u=par[up[u]];
}
}
void get_ans(){
ll ans=0;
res[0].S--;
for(auto p : res){
add(p.F,p.S);
ans+=get(p.F-1)*p.S;
}
for(auto p : res){
add(p.F,-p.S);
}
res.clear();
cout<<ans<<endl;
}
int main(){
vector<int> vec;
cin>>n;
f(i,1,n+1){
cin>>col[i];
vec.pb(col[i]);
}
sort(all(vec));
f(i,1,n+1){
col[i]=lower_bound(all(vec),col[i])-vec.begin()+1;
}
f(i,1,n){
cin>>X[i]>>Y[i];
par[Y[i]]=X[i];
g[X[i]].pb(Y[i]);
}
up[1]=1;
dfs1(1);
dfs2(1);
f(i,1,n){
query(Y[i],col[Y[i]]);
get_ans();
}
}
Compilation message
construction.cpp: In function 'void dfs1(int)':
construction.cpp:8:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | #define f(i,a,b) for(int i=a;i<b;i++)
......
35 | f(i,0,g[u].size()){
| ~~~~~~~~~~~~~~~
construction.cpp:35:2: note: in expansion of macro 'f'
35 | f(i,0,g[u].size()){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4952 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Correct |
6 ms |
5068 KB |
Output is correct |
9 |
Correct |
4 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
5036 KB |
Output is correct |
11 |
Correct |
5 ms |
5068 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
4 ms |
5068 KB |
Output is correct |
14 |
Correct |
4 ms |
5068 KB |
Output is correct |
15 |
Correct |
3 ms |
5068 KB |
Output is correct |
16 |
Correct |
4 ms |
5068 KB |
Output is correct |
17 |
Correct |
5 ms |
5068 KB |
Output is correct |
18 |
Correct |
6 ms |
5068 KB |
Output is correct |
19 |
Correct |
4 ms |
5068 KB |
Output is correct |
20 |
Correct |
4 ms |
5008 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
3 ms |
5068 KB |
Output is correct |
23 |
Correct |
4 ms |
5068 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
3 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
4 ms |
5068 KB |
Output is correct |
29 |
Correct |
3 ms |
5068 KB |
Output is correct |
30 |
Correct |
4 ms |
4980 KB |
Output is correct |
31 |
Correct |
4 ms |
5068 KB |
Output is correct |
32 |
Correct |
4 ms |
5116 KB |
Output is correct |
33 |
Correct |
4 ms |
5108 KB |
Output is correct |
34 |
Correct |
4 ms |
5068 KB |
Output is correct |
35 |
Correct |
4 ms |
5068 KB |
Output is correct |
36 |
Correct |
4 ms |
5068 KB |
Output is correct |
37 |
Correct |
4 ms |
5068 KB |
Output is correct |
38 |
Correct |
4 ms |
5068 KB |
Output is correct |
39 |
Correct |
4 ms |
5068 KB |
Output is correct |
40 |
Correct |
4 ms |
5068 KB |
Output is correct |
41 |
Correct |
4 ms |
5068 KB |
Output is correct |
42 |
Correct |
4 ms |
5068 KB |
Output is correct |
43 |
Correct |
4 ms |
5068 KB |
Output is correct |
44 |
Correct |
3 ms |
5068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4952 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Correct |
6 ms |
5068 KB |
Output is correct |
9 |
Correct |
4 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
5036 KB |
Output is correct |
11 |
Correct |
5 ms |
5068 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
4 ms |
5068 KB |
Output is correct |
14 |
Correct |
4 ms |
5068 KB |
Output is correct |
15 |
Correct |
3 ms |
5068 KB |
Output is correct |
16 |
Correct |
4 ms |
5068 KB |
Output is correct |
17 |
Correct |
5 ms |
5068 KB |
Output is correct |
18 |
Correct |
6 ms |
5068 KB |
Output is correct |
19 |
Correct |
4 ms |
5068 KB |
Output is correct |
20 |
Correct |
4 ms |
5008 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
3 ms |
5068 KB |
Output is correct |
23 |
Correct |
4 ms |
5068 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
3 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
4 ms |
5068 KB |
Output is correct |
29 |
Correct |
3 ms |
5068 KB |
Output is correct |
30 |
Correct |
4 ms |
4980 KB |
Output is correct |
31 |
Correct |
4 ms |
5068 KB |
Output is correct |
32 |
Correct |
4 ms |
5116 KB |
Output is correct |
33 |
Correct |
4 ms |
5108 KB |
Output is correct |
34 |
Correct |
4 ms |
5068 KB |
Output is correct |
35 |
Correct |
4 ms |
5068 KB |
Output is correct |
36 |
Correct |
4 ms |
5068 KB |
Output is correct |
37 |
Correct |
4 ms |
5068 KB |
Output is correct |
38 |
Correct |
4 ms |
5068 KB |
Output is correct |
39 |
Correct |
4 ms |
5068 KB |
Output is correct |
40 |
Correct |
4 ms |
5068 KB |
Output is correct |
41 |
Correct |
4 ms |
5068 KB |
Output is correct |
42 |
Correct |
4 ms |
5068 KB |
Output is correct |
43 |
Correct |
4 ms |
5068 KB |
Output is correct |
44 |
Correct |
3 ms |
5068 KB |
Output is correct |
45 |
Correct |
6 ms |
5068 KB |
Output is correct |
46 |
Correct |
21 ms |
5324 KB |
Output is correct |
47 |
Correct |
15 ms |
5324 KB |
Output is correct |
48 |
Correct |
15 ms |
5324 KB |
Output is correct |
49 |
Correct |
13 ms |
5580 KB |
Output is correct |
50 |
Correct |
12 ms |
5580 KB |
Output is correct |
51 |
Correct |
13 ms |
5564 KB |
Output is correct |
52 |
Correct |
12 ms |
5452 KB |
Output is correct |
53 |
Correct |
14 ms |
5488 KB |
Output is correct |
54 |
Correct |
13 ms |
5484 KB |
Output is correct |
55 |
Correct |
13 ms |
5528 KB |
Output is correct |
56 |
Correct |
14 ms |
5472 KB |
Output is correct |
57 |
Correct |
15 ms |
5352 KB |
Output is correct |
58 |
Correct |
17 ms |
5392 KB |
Output is correct |
59 |
Correct |
16 ms |
5360 KB |
Output is correct |
60 |
Correct |
16 ms |
5388 KB |
Output is correct |
61 |
Correct |
12 ms |
5400 KB |
Output is correct |
62 |
Correct |
12 ms |
5432 KB |
Output is correct |
63 |
Correct |
12 ms |
5452 KB |
Output is correct |
64 |
Correct |
13 ms |
5376 KB |
Output is correct |
65 |
Correct |
14 ms |
5324 KB |
Output is correct |
66 |
Correct |
16 ms |
5388 KB |
Output is correct |
67 |
Correct |
19 ms |
5380 KB |
Output is correct |
68 |
Correct |
13 ms |
5640 KB |
Output is correct |
69 |
Correct |
16 ms |
5452 KB |
Output is correct |
70 |
Correct |
12 ms |
5452 KB |
Output is correct |
71 |
Correct |
12 ms |
5452 KB |
Output is correct |
72 |
Correct |
15 ms |
5372 KB |
Output is correct |
73 |
Correct |
14 ms |
5284 KB |
Output is correct |
74 |
Correct |
12 ms |
5452 KB |
Output is correct |
75 |
Correct |
12 ms |
5416 KB |
Output is correct |
76 |
Correct |
12 ms |
5400 KB |
Output is correct |
77 |
Correct |
13 ms |
5452 KB |
Output is correct |
78 |
Correct |
12 ms |
5416 KB |
Output is correct |
79 |
Correct |
13 ms |
5408 KB |
Output is correct |
80 |
Correct |
13 ms |
5452 KB |
Output is correct |
81 |
Correct |
13 ms |
5324 KB |
Output is correct |
82 |
Correct |
14 ms |
5404 KB |
Output is correct |
83 |
Correct |
13 ms |
5452 KB |
Output is correct |
84 |
Correct |
17 ms |
5440 KB |
Output is correct |
85 |
Correct |
14 ms |
5432 KB |
Output is correct |
86 |
Correct |
14 ms |
5500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4952 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
4 ms |
5068 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Correct |
6 ms |
5068 KB |
Output is correct |
9 |
Correct |
4 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
5036 KB |
Output is correct |
11 |
Correct |
5 ms |
5068 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
4 ms |
5068 KB |
Output is correct |
14 |
Correct |
4 ms |
5068 KB |
Output is correct |
15 |
Correct |
3 ms |
5068 KB |
Output is correct |
16 |
Correct |
4 ms |
5068 KB |
Output is correct |
17 |
Correct |
5 ms |
5068 KB |
Output is correct |
18 |
Correct |
6 ms |
5068 KB |
Output is correct |
19 |
Correct |
4 ms |
5068 KB |
Output is correct |
20 |
Correct |
4 ms |
5008 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
3 ms |
5068 KB |
Output is correct |
23 |
Correct |
4 ms |
5068 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
3 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
4 ms |
5068 KB |
Output is correct |
29 |
Correct |
3 ms |
5068 KB |
Output is correct |
30 |
Correct |
4 ms |
4980 KB |
Output is correct |
31 |
Correct |
4 ms |
5068 KB |
Output is correct |
32 |
Correct |
4 ms |
5116 KB |
Output is correct |
33 |
Correct |
4 ms |
5108 KB |
Output is correct |
34 |
Correct |
4 ms |
5068 KB |
Output is correct |
35 |
Correct |
4 ms |
5068 KB |
Output is correct |
36 |
Correct |
4 ms |
5068 KB |
Output is correct |
37 |
Correct |
4 ms |
5068 KB |
Output is correct |
38 |
Correct |
4 ms |
5068 KB |
Output is correct |
39 |
Correct |
4 ms |
5068 KB |
Output is correct |
40 |
Correct |
4 ms |
5068 KB |
Output is correct |
41 |
Correct |
4 ms |
5068 KB |
Output is correct |
42 |
Correct |
4 ms |
5068 KB |
Output is correct |
43 |
Correct |
4 ms |
5068 KB |
Output is correct |
44 |
Correct |
3 ms |
5068 KB |
Output is correct |
45 |
Correct |
6 ms |
5068 KB |
Output is correct |
46 |
Correct |
21 ms |
5324 KB |
Output is correct |
47 |
Correct |
15 ms |
5324 KB |
Output is correct |
48 |
Correct |
15 ms |
5324 KB |
Output is correct |
49 |
Correct |
13 ms |
5580 KB |
Output is correct |
50 |
Correct |
12 ms |
5580 KB |
Output is correct |
51 |
Correct |
13 ms |
5564 KB |
Output is correct |
52 |
Correct |
12 ms |
5452 KB |
Output is correct |
53 |
Correct |
14 ms |
5488 KB |
Output is correct |
54 |
Correct |
13 ms |
5484 KB |
Output is correct |
55 |
Correct |
13 ms |
5528 KB |
Output is correct |
56 |
Correct |
14 ms |
5472 KB |
Output is correct |
57 |
Correct |
15 ms |
5352 KB |
Output is correct |
58 |
Correct |
17 ms |
5392 KB |
Output is correct |
59 |
Correct |
16 ms |
5360 KB |
Output is correct |
60 |
Correct |
16 ms |
5388 KB |
Output is correct |
61 |
Correct |
12 ms |
5400 KB |
Output is correct |
62 |
Correct |
12 ms |
5432 KB |
Output is correct |
63 |
Correct |
12 ms |
5452 KB |
Output is correct |
64 |
Correct |
13 ms |
5376 KB |
Output is correct |
65 |
Correct |
14 ms |
5324 KB |
Output is correct |
66 |
Correct |
16 ms |
5388 KB |
Output is correct |
67 |
Correct |
19 ms |
5380 KB |
Output is correct |
68 |
Correct |
13 ms |
5640 KB |
Output is correct |
69 |
Correct |
16 ms |
5452 KB |
Output is correct |
70 |
Correct |
12 ms |
5452 KB |
Output is correct |
71 |
Correct |
12 ms |
5452 KB |
Output is correct |
72 |
Correct |
15 ms |
5372 KB |
Output is correct |
73 |
Correct |
14 ms |
5284 KB |
Output is correct |
74 |
Correct |
12 ms |
5452 KB |
Output is correct |
75 |
Correct |
12 ms |
5416 KB |
Output is correct |
76 |
Correct |
12 ms |
5400 KB |
Output is correct |
77 |
Correct |
13 ms |
5452 KB |
Output is correct |
78 |
Correct |
12 ms |
5416 KB |
Output is correct |
79 |
Correct |
13 ms |
5408 KB |
Output is correct |
80 |
Correct |
13 ms |
5452 KB |
Output is correct |
81 |
Correct |
13 ms |
5324 KB |
Output is correct |
82 |
Correct |
14 ms |
5404 KB |
Output is correct |
83 |
Correct |
13 ms |
5452 KB |
Output is correct |
84 |
Correct |
17 ms |
5440 KB |
Output is correct |
85 |
Correct |
14 ms |
5432 KB |
Output is correct |
86 |
Correct |
14 ms |
5500 KB |
Output is correct |
87 |
Correct |
38 ms |
5848 KB |
Output is correct |
88 |
Correct |
104 ms |
7564 KB |
Output is correct |
89 |
Correct |
405 ms |
13328 KB |
Output is correct |
90 |
Correct |
418 ms |
13524 KB |
Output is correct |
91 |
Correct |
381 ms |
13400 KB |
Output is correct |
92 |
Correct |
289 ms |
19332 KB |
Output is correct |
93 |
Correct |
315 ms |
19456 KB |
Output is correct |
94 |
Correct |
275 ms |
19376 KB |
Output is correct |
95 |
Correct |
268 ms |
16832 KB |
Output is correct |
96 |
Correct |
284 ms |
16828 KB |
Output is correct |
97 |
Correct |
296 ms |
16900 KB |
Output is correct |
98 |
Correct |
276 ms |
16980 KB |
Output is correct |
99 |
Correct |
284 ms |
15880 KB |
Output is correct |
100 |
Correct |
448 ms |
13512 KB |
Output is correct |
101 |
Correct |
464 ms |
13388 KB |
Output is correct |
102 |
Correct |
460 ms |
13464 KB |
Output is correct |
103 |
Correct |
466 ms |
13452 KB |
Output is correct |
104 |
Correct |
315 ms |
15848 KB |
Output is correct |
105 |
Correct |
310 ms |
15888 KB |
Output is correct |
106 |
Correct |
296 ms |
15892 KB |
Output is correct |
107 |
Correct |
318 ms |
12628 KB |
Output is correct |
108 |
Correct |
376 ms |
13236 KB |
Output is correct |
109 |
Correct |
357 ms |
13456 KB |
Output is correct |
110 |
Correct |
253 ms |
19400 KB |
Output is correct |
111 |
Correct |
262 ms |
16928 KB |
Output is correct |
112 |
Correct |
274 ms |
16808 KB |
Output is correct |
113 |
Correct |
255 ms |
15932 KB |
Output is correct |
114 |
Correct |
423 ms |
13412 KB |
Output is correct |
115 |
Correct |
452 ms |
13532 KB |
Output is correct |
116 |
Correct |
268 ms |
15836 KB |
Output is correct |
117 |
Correct |
287 ms |
15000 KB |
Output is correct |
118 |
Correct |
276 ms |
14832 KB |
Output is correct |
119 |
Correct |
281 ms |
14608 KB |
Output is correct |
120 |
Correct |
268 ms |
14876 KB |
Output is correct |
121 |
Correct |
281 ms |
15016 KB |
Output is correct |
122 |
Correct |
266 ms |
14528 KB |
Output is correct |
123 |
Correct |
286 ms |
15008 KB |
Output is correct |
124 |
Correct |
286 ms |
14836 KB |
Output is correct |
125 |
Correct |
295 ms |
14596 KB |
Output is correct |
126 |
Correct |
304 ms |
14988 KB |
Output is correct |
127 |
Correct |
283 ms |
14816 KB |
Output is correct |
128 |
Correct |
300 ms |
14628 KB |
Output is correct |