#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(a) begin(a),end(a)
const int N = (int)1e5+2;
using ll = long long;
using ar = array<int,2>;
vector<int> adj[N];
deque<ar> ch[N], edge;
int n, c[N], h[N], sz[N], p[N], dep[N], fen[N];
void upd(int x, int v){ for(; x<N; x+=x&-x) fen[x]+=v; }
int sum(int x){ int s=0; for(; x; x-=x&-x) s+=fen[x]; return s; }
int dfsSz(int s){
for(auto &u : adj[s]){
int x = adj[s][0];
dep[u] = dep[s]+1, sz[s]+=dfsSz(u);
if(sz[x]<sz[u]) swap(adj[s][0],u);
}
return (sz[s]+=1);
}
void Hld(int s, int head){
h[s] = head;
for(auto u : adj[s])
Hld(u,u==adj[s][0]?head:u);
}
ll calc(int a){
vector<ar> v, tv; v.clear(); ll ans = 0;
while(a!=-1){
int H = h[a], siz = dep[a]-dep[h[a]]+1; tv.clear();
for(auto [x,cnt] : ch[H]){
tv.pb({x,min(cnt,siz)});
siz-=min(cnt,siz); if(!siz) break;
}
a = p[H]; for(int i=(int)tv.size()-1;i>=0;i--) v.pb(tv[i]);
}
for(int i = 1; i <= n; i++) fen[i]=0;
for(auto [x,y] : v) ans+=(ll)y*sum(x-1), upd(x,y);
return ans;
}
void add(int b, int v){
while(b!=-1){
int H = h[b], siz = dep[b]-dep[h[b]]+1; int tsz = siz;
while(!ch[H].empty() and siz){
int cnt = ch[H].front()[1];
if(cnt<=siz) ch[H].pop_front(), siz-=cnt;
else ch[H].front()[1]-=siz, siz=0;
}
ch[H].push_front({v,tsz}); b = p[H];
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie();
cin >> n; vector<int> v; int a, b; p[1]=-1;
for(int i = 1; i <= n; i++) cin >> c[i], v.pb(c[i]);
sort(all(v)), v.erase(unique(all(v)),end(v));
for(int i = 1; i <= n; i++) c[i] = lower_bound(all(v),c[i])-begin(v)+1;
for(int i = 1; i < n; i++) cin >> a >> b, adj[a].pb(b), p[b]=a, edge.pb({a,b});
ch[1].pb({c[1],1}); dfsSz(1); Hld(1,1);
for(auto [a,b] : edge) cout << calc(a) << "\n", add(b,c[b]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
69916 KB |
Output is correct |
2 |
Correct |
48 ms |
69904 KB |
Output is correct |
3 |
Correct |
50 ms |
69964 KB |
Output is correct |
4 |
Correct |
47 ms |
70092 KB |
Output is correct |
5 |
Correct |
50 ms |
70088 KB |
Output is correct |
6 |
Correct |
43 ms |
70116 KB |
Output is correct |
7 |
Correct |
48 ms |
70184 KB |
Output is correct |
8 |
Correct |
45 ms |
70096 KB |
Output is correct |
9 |
Correct |
57 ms |
70040 KB |
Output is correct |
10 |
Correct |
45 ms |
70000 KB |
Output is correct |
11 |
Correct |
48 ms |
70200 KB |
Output is correct |
12 |
Correct |
50 ms |
70168 KB |
Output is correct |
13 |
Correct |
47 ms |
70136 KB |
Output is correct |
14 |
Correct |
47 ms |
70196 KB |
Output is correct |
15 |
Correct |
49 ms |
70152 KB |
Output is correct |
16 |
Correct |
48 ms |
70120 KB |
Output is correct |
17 |
Correct |
48 ms |
70056 KB |
Output is correct |
18 |
Correct |
48 ms |
70160 KB |
Output is correct |
19 |
Correct |
51 ms |
70188 KB |
Output is correct |
20 |
Correct |
56 ms |
70140 KB |
Output is correct |
21 |
Correct |
50 ms |
70188 KB |
Output is correct |
22 |
Correct |
49 ms |
70280 KB |
Output is correct |
23 |
Correct |
48 ms |
70180 KB |
Output is correct |
24 |
Correct |
50 ms |
70184 KB |
Output is correct |
25 |
Correct |
53 ms |
70180 KB |
Output is correct |
26 |
Correct |
51 ms |
70036 KB |
Output is correct |
27 |
Correct |
50 ms |
70092 KB |
Output is correct |
28 |
Correct |
53 ms |
70104 KB |
Output is correct |
29 |
Correct |
50 ms |
70116 KB |
Output is correct |
30 |
Correct |
50 ms |
70092 KB |
Output is correct |
31 |
Correct |
52 ms |
70184 KB |
Output is correct |
32 |
Correct |
53 ms |
70160 KB |
Output is correct |
33 |
Correct |
49 ms |
70160 KB |
Output is correct |
34 |
Correct |
48 ms |
70100 KB |
Output is correct |
35 |
Correct |
49 ms |
70076 KB |
Output is correct |
36 |
Correct |
51 ms |
70100 KB |
Output is correct |
37 |
Correct |
49 ms |
70100 KB |
Output is correct |
38 |
Correct |
49 ms |
70080 KB |
Output is correct |
39 |
Correct |
49 ms |
70184 KB |
Output is correct |
40 |
Correct |
51 ms |
70100 KB |
Output is correct |
41 |
Correct |
54 ms |
70144 KB |
Output is correct |
42 |
Correct |
51 ms |
70184 KB |
Output is correct |
43 |
Correct |
48 ms |
70168 KB |
Output is correct |
44 |
Correct |
49 ms |
70192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
69916 KB |
Output is correct |
2 |
Correct |
48 ms |
69904 KB |
Output is correct |
3 |
Correct |
50 ms |
69964 KB |
Output is correct |
4 |
Correct |
47 ms |
70092 KB |
Output is correct |
5 |
Correct |
50 ms |
70088 KB |
Output is correct |
6 |
Correct |
43 ms |
70116 KB |
Output is correct |
7 |
Correct |
48 ms |
70184 KB |
Output is correct |
8 |
Correct |
45 ms |
70096 KB |
Output is correct |
9 |
Correct |
57 ms |
70040 KB |
Output is correct |
10 |
Correct |
45 ms |
70000 KB |
Output is correct |
11 |
Correct |
48 ms |
70200 KB |
Output is correct |
12 |
Correct |
50 ms |
70168 KB |
Output is correct |
13 |
Correct |
47 ms |
70136 KB |
Output is correct |
14 |
Correct |
47 ms |
70196 KB |
Output is correct |
15 |
Correct |
49 ms |
70152 KB |
Output is correct |
16 |
Correct |
48 ms |
70120 KB |
Output is correct |
17 |
Correct |
48 ms |
70056 KB |
Output is correct |
18 |
Correct |
48 ms |
70160 KB |
Output is correct |
19 |
Correct |
51 ms |
70188 KB |
Output is correct |
20 |
Correct |
56 ms |
70140 KB |
Output is correct |
21 |
Correct |
50 ms |
70188 KB |
Output is correct |
22 |
Correct |
49 ms |
70280 KB |
Output is correct |
23 |
Correct |
48 ms |
70180 KB |
Output is correct |
24 |
Correct |
50 ms |
70184 KB |
Output is correct |
25 |
Correct |
53 ms |
70180 KB |
Output is correct |
26 |
Correct |
51 ms |
70036 KB |
Output is correct |
27 |
Correct |
50 ms |
70092 KB |
Output is correct |
28 |
Correct |
53 ms |
70104 KB |
Output is correct |
29 |
Correct |
50 ms |
70116 KB |
Output is correct |
30 |
Correct |
50 ms |
70092 KB |
Output is correct |
31 |
Correct |
52 ms |
70184 KB |
Output is correct |
32 |
Correct |
53 ms |
70160 KB |
Output is correct |
33 |
Correct |
49 ms |
70160 KB |
Output is correct |
34 |
Correct |
48 ms |
70100 KB |
Output is correct |
35 |
Correct |
49 ms |
70076 KB |
Output is correct |
36 |
Correct |
51 ms |
70100 KB |
Output is correct |
37 |
Correct |
49 ms |
70100 KB |
Output is correct |
38 |
Correct |
49 ms |
70080 KB |
Output is correct |
39 |
Correct |
49 ms |
70184 KB |
Output is correct |
40 |
Correct |
51 ms |
70100 KB |
Output is correct |
41 |
Correct |
54 ms |
70144 KB |
Output is correct |
42 |
Correct |
51 ms |
70184 KB |
Output is correct |
43 |
Correct |
48 ms |
70168 KB |
Output is correct |
44 |
Correct |
49 ms |
70192 KB |
Output is correct |
45 |
Correct |
49 ms |
70292 KB |
Output is correct |
46 |
Correct |
58 ms |
71244 KB |
Output is correct |
47 |
Correct |
63 ms |
71240 KB |
Output is correct |
48 |
Correct |
55 ms |
71224 KB |
Output is correct |
49 |
Correct |
47 ms |
70592 KB |
Output is correct |
50 |
Correct |
48 ms |
70568 KB |
Output is correct |
51 |
Correct |
50 ms |
70672 KB |
Output is correct |
52 |
Correct |
53 ms |
71468 KB |
Output is correct |
53 |
Correct |
54 ms |
71472 KB |
Output is correct |
54 |
Correct |
54 ms |
71380 KB |
Output is correct |
55 |
Correct |
54 ms |
71500 KB |
Output is correct |
56 |
Correct |
54 ms |
71372 KB |
Output is correct |
57 |
Correct |
56 ms |
71156 KB |
Output is correct |
58 |
Correct |
59 ms |
71236 KB |
Output is correct |
59 |
Correct |
63 ms |
71116 KB |
Output is correct |
60 |
Correct |
67 ms |
71208 KB |
Output is correct |
61 |
Correct |
53 ms |
71388 KB |
Output is correct |
62 |
Correct |
53 ms |
71392 KB |
Output is correct |
63 |
Correct |
59 ms |
71432 KB |
Output is correct |
64 |
Correct |
65 ms |
71204 KB |
Output is correct |
65 |
Correct |
69 ms |
71196 KB |
Output is correct |
66 |
Correct |
53 ms |
71240 KB |
Output is correct |
67 |
Correct |
53 ms |
71244 KB |
Output is correct |
68 |
Correct |
59 ms |
70588 KB |
Output is correct |
69 |
Correct |
54 ms |
71472 KB |
Output is correct |
70 |
Correct |
52 ms |
71472 KB |
Output is correct |
71 |
Correct |
52 ms |
71380 KB |
Output is correct |
72 |
Correct |
57 ms |
71168 KB |
Output is correct |
73 |
Correct |
57 ms |
71196 KB |
Output is correct |
74 |
Correct |
55 ms |
71292 KB |
Output is correct |
75 |
Correct |
68 ms |
71316 KB |
Output is correct |
76 |
Correct |
55 ms |
71276 KB |
Output is correct |
77 |
Correct |
53 ms |
71236 KB |
Output is correct |
78 |
Correct |
54 ms |
71336 KB |
Output is correct |
79 |
Correct |
53 ms |
71308 KB |
Output is correct |
80 |
Correct |
54 ms |
71308 KB |
Output is correct |
81 |
Correct |
56 ms |
71312 KB |
Output is correct |
82 |
Correct |
55 ms |
71236 KB |
Output is correct |
83 |
Correct |
55 ms |
71308 KB |
Output is correct |
84 |
Correct |
55 ms |
71256 KB |
Output is correct |
85 |
Correct |
62 ms |
71308 KB |
Output is correct |
86 |
Correct |
57 ms |
71316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
69916 KB |
Output is correct |
2 |
Correct |
48 ms |
69904 KB |
Output is correct |
3 |
Correct |
50 ms |
69964 KB |
Output is correct |
4 |
Correct |
47 ms |
70092 KB |
Output is correct |
5 |
Correct |
50 ms |
70088 KB |
Output is correct |
6 |
Correct |
43 ms |
70116 KB |
Output is correct |
7 |
Correct |
48 ms |
70184 KB |
Output is correct |
8 |
Correct |
45 ms |
70096 KB |
Output is correct |
9 |
Correct |
57 ms |
70040 KB |
Output is correct |
10 |
Correct |
45 ms |
70000 KB |
Output is correct |
11 |
Correct |
48 ms |
70200 KB |
Output is correct |
12 |
Correct |
50 ms |
70168 KB |
Output is correct |
13 |
Correct |
47 ms |
70136 KB |
Output is correct |
14 |
Correct |
47 ms |
70196 KB |
Output is correct |
15 |
Correct |
49 ms |
70152 KB |
Output is correct |
16 |
Correct |
48 ms |
70120 KB |
Output is correct |
17 |
Correct |
48 ms |
70056 KB |
Output is correct |
18 |
Correct |
48 ms |
70160 KB |
Output is correct |
19 |
Correct |
51 ms |
70188 KB |
Output is correct |
20 |
Correct |
56 ms |
70140 KB |
Output is correct |
21 |
Correct |
50 ms |
70188 KB |
Output is correct |
22 |
Correct |
49 ms |
70280 KB |
Output is correct |
23 |
Correct |
48 ms |
70180 KB |
Output is correct |
24 |
Correct |
50 ms |
70184 KB |
Output is correct |
25 |
Correct |
53 ms |
70180 KB |
Output is correct |
26 |
Correct |
51 ms |
70036 KB |
Output is correct |
27 |
Correct |
50 ms |
70092 KB |
Output is correct |
28 |
Correct |
53 ms |
70104 KB |
Output is correct |
29 |
Correct |
50 ms |
70116 KB |
Output is correct |
30 |
Correct |
50 ms |
70092 KB |
Output is correct |
31 |
Correct |
52 ms |
70184 KB |
Output is correct |
32 |
Correct |
53 ms |
70160 KB |
Output is correct |
33 |
Correct |
49 ms |
70160 KB |
Output is correct |
34 |
Correct |
48 ms |
70100 KB |
Output is correct |
35 |
Correct |
49 ms |
70076 KB |
Output is correct |
36 |
Correct |
51 ms |
70100 KB |
Output is correct |
37 |
Correct |
49 ms |
70100 KB |
Output is correct |
38 |
Correct |
49 ms |
70080 KB |
Output is correct |
39 |
Correct |
49 ms |
70184 KB |
Output is correct |
40 |
Correct |
51 ms |
70100 KB |
Output is correct |
41 |
Correct |
54 ms |
70144 KB |
Output is correct |
42 |
Correct |
51 ms |
70184 KB |
Output is correct |
43 |
Correct |
48 ms |
70168 KB |
Output is correct |
44 |
Correct |
49 ms |
70192 KB |
Output is correct |
45 |
Correct |
49 ms |
70292 KB |
Output is correct |
46 |
Correct |
58 ms |
71244 KB |
Output is correct |
47 |
Correct |
63 ms |
71240 KB |
Output is correct |
48 |
Correct |
55 ms |
71224 KB |
Output is correct |
49 |
Correct |
47 ms |
70592 KB |
Output is correct |
50 |
Correct |
48 ms |
70568 KB |
Output is correct |
51 |
Correct |
50 ms |
70672 KB |
Output is correct |
52 |
Correct |
53 ms |
71468 KB |
Output is correct |
53 |
Correct |
54 ms |
71472 KB |
Output is correct |
54 |
Correct |
54 ms |
71380 KB |
Output is correct |
55 |
Correct |
54 ms |
71500 KB |
Output is correct |
56 |
Correct |
54 ms |
71372 KB |
Output is correct |
57 |
Correct |
56 ms |
71156 KB |
Output is correct |
58 |
Correct |
59 ms |
71236 KB |
Output is correct |
59 |
Correct |
63 ms |
71116 KB |
Output is correct |
60 |
Correct |
67 ms |
71208 KB |
Output is correct |
61 |
Correct |
53 ms |
71388 KB |
Output is correct |
62 |
Correct |
53 ms |
71392 KB |
Output is correct |
63 |
Correct |
59 ms |
71432 KB |
Output is correct |
64 |
Correct |
65 ms |
71204 KB |
Output is correct |
65 |
Correct |
69 ms |
71196 KB |
Output is correct |
66 |
Correct |
53 ms |
71240 KB |
Output is correct |
67 |
Correct |
53 ms |
71244 KB |
Output is correct |
68 |
Correct |
59 ms |
70588 KB |
Output is correct |
69 |
Correct |
54 ms |
71472 KB |
Output is correct |
70 |
Correct |
52 ms |
71472 KB |
Output is correct |
71 |
Correct |
52 ms |
71380 KB |
Output is correct |
72 |
Correct |
57 ms |
71168 KB |
Output is correct |
73 |
Correct |
57 ms |
71196 KB |
Output is correct |
74 |
Correct |
55 ms |
71292 KB |
Output is correct |
75 |
Correct |
68 ms |
71316 KB |
Output is correct |
76 |
Correct |
55 ms |
71276 KB |
Output is correct |
77 |
Correct |
53 ms |
71236 KB |
Output is correct |
78 |
Correct |
54 ms |
71336 KB |
Output is correct |
79 |
Correct |
53 ms |
71308 KB |
Output is correct |
80 |
Correct |
54 ms |
71308 KB |
Output is correct |
81 |
Correct |
56 ms |
71312 KB |
Output is correct |
82 |
Correct |
55 ms |
71236 KB |
Output is correct |
83 |
Correct |
55 ms |
71308 KB |
Output is correct |
84 |
Correct |
55 ms |
71256 KB |
Output is correct |
85 |
Correct |
62 ms |
71308 KB |
Output is correct |
86 |
Correct |
57 ms |
71316 KB |
Output is correct |
87 |
Correct |
76 ms |
73108 KB |
Output is correct |
88 |
Correct |
181 ms |
79384 KB |
Output is correct |
89 |
Correct |
1261 ms |
101244 KB |
Output is correct |
90 |
Correct |
1217 ms |
101404 KB |
Output is correct |
91 |
Correct |
1191 ms |
101192 KB |
Output is correct |
92 |
Correct |
1055 ms |
84844 KB |
Output is correct |
93 |
Correct |
1002 ms |
84860 KB |
Output is correct |
94 |
Correct |
1016 ms |
84664 KB |
Output is correct |
95 |
Correct |
1100 ms |
106788 KB |
Output is correct |
96 |
Correct |
1032 ms |
106736 KB |
Output is correct |
97 |
Correct |
1154 ms |
106652 KB |
Output is correct |
98 |
Correct |
1030 ms |
106564 KB |
Output is correct |
99 |
Correct |
1231 ms |
105364 KB |
Output is correct |
100 |
Correct |
1223 ms |
93920 KB |
Output is correct |
101 |
Correct |
1181 ms |
93808 KB |
Output is correct |
102 |
Correct |
1193 ms |
93648 KB |
Output is correct |
103 |
Correct |
1337 ms |
93824 KB |
Output is correct |
104 |
Correct |
1073 ms |
105112 KB |
Output is correct |
105 |
Correct |
1048 ms |
105120 KB |
Output is correct |
106 |
Correct |
1052 ms |
105212 KB |
Output is correct |
107 |
Correct |
1181 ms |
101288 KB |
Output is correct |
108 |
Correct |
1151 ms |
101384 KB |
Output is correct |
109 |
Correct |
1193 ms |
101388 KB |
Output is correct |
110 |
Correct |
967 ms |
84704 KB |
Output is correct |
111 |
Correct |
1036 ms |
106624 KB |
Output is correct |
112 |
Correct |
987 ms |
106620 KB |
Output is correct |
113 |
Correct |
1012 ms |
105108 KB |
Output is correct |
114 |
Correct |
1221 ms |
93856 KB |
Output is correct |
115 |
Correct |
1157 ms |
93612 KB |
Output is correct |
116 |
Correct |
1035 ms |
105104 KB |
Output is correct |
117 |
Correct |
1020 ms |
103208 KB |
Output is correct |
118 |
Correct |
1071 ms |
102620 KB |
Output is correct |
119 |
Correct |
1123 ms |
102252 KB |
Output is correct |
120 |
Correct |
1068 ms |
103368 KB |
Output is correct |
121 |
Correct |
1135 ms |
102556 KB |
Output is correct |
122 |
Correct |
1027 ms |
102336 KB |
Output is correct |
123 |
Correct |
1137 ms |
103192 KB |
Output is correct |
124 |
Correct |
1160 ms |
102672 KB |
Output is correct |
125 |
Correct |
1061 ms |
102156 KB |
Output is correct |
126 |
Correct |
1139 ms |
103248 KB |
Output is correct |
127 |
Correct |
1050 ms |
102660 KB |
Output is correct |
128 |
Correct |
1038 ms |
102276 KB |
Output is correct |