#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define pf push_front
#define sz(a) (int)a.size()
#define all(a) begin(a),end(a)
const int mxN = (int)1e5+2;
using ar = array<int,2>;
int n, tim=1;
vector<int> adj[mxN];
deque<ar> edge, ch[mxN];
int c[mxN], h[mxN], sub[mxN], p[mxN], st[mxN], en[mxN], dep[mxN], fen[mxN];
void upd(int x, int v){ for(; x<mxN; 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, int par){
sub[s] = 1; dep[s] = (par==-1?0:dep[par]+1); p[s] = par;
for(auto &u : adj[s]){
if(u==par) continue;
sub[s]+=dfsSz(u,s);
int x = adj[s][0];
if(x==par or sub[x]<sub[u]) swap(adj[s][0],u);
}
return sub[s];
}
void dfsHld(int s, int par, int head){
h[s] = head; st[s]=tim++;
for(auto u : adj[s]){
if(u==par) continue;
dfsHld(u,s,u==adj[s][0]?head:u);
}
en[s] = tim;
}
int calc(int a){
vector<ar> v; v.clear(); int ans = 0;
while(a!=-1){
int H = h[a], siz = dep[a]-dep[h[a]]+1;
vector<ar> tv; tv.clear();
for(auto [x,cnt] : ch[H])
if(siz) tv.pb({x,min(cnt,siz)}), siz-=min(cnt,siz);
reverse(all(tv)); for(auto u : tv) v.pb(u);
a = p[H];
}
for(int i = 1; i <= n; i++) fen[i]=0;
for(auto [x,y] : v) ans+=y*sum(x-1), upd(x,y);
return ans;
}
void addEdge(int b){
int v = c[b];
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].pf({v,tsz}); b = p[H];
}
}
int32_t main() {
ios_base::sync_with_stdio(false); cin.tie();
cin >> n; vector<int> v;
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++){
int a, b; cin >> a >> b;
adj[a].pb(b), adj[b].pb(a), edge.pb({a,b});
}
ch[1].pb({c[1],1}); dfsSz(1,-1); dfsHld(1,-1,1);
for(auto [a,b] : edge) cout << calc(a) << "\n", addEdge(b);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
70040 KB |
Output is correct |
2 |
Correct |
44 ms |
70052 KB |
Output is correct |
3 |
Correct |
52 ms |
70052 KB |
Output is correct |
4 |
Correct |
48 ms |
70164 KB |
Output is correct |
5 |
Correct |
47 ms |
70208 KB |
Output is correct |
6 |
Correct |
47 ms |
70136 KB |
Output is correct |
7 |
Correct |
50 ms |
70220 KB |
Output is correct |
8 |
Correct |
50 ms |
70092 KB |
Output is correct |
9 |
Correct |
51 ms |
70092 KB |
Output is correct |
10 |
Correct |
46 ms |
70064 KB |
Output is correct |
11 |
Correct |
49 ms |
70300 KB |
Output is correct |
12 |
Correct |
48 ms |
70192 KB |
Output is correct |
13 |
Correct |
47 ms |
70276 KB |
Output is correct |
14 |
Correct |
50 ms |
70124 KB |
Output is correct |
15 |
Correct |
50 ms |
70184 KB |
Output is correct |
16 |
Correct |
49 ms |
70228 KB |
Output is correct |
17 |
Correct |
69 ms |
70236 KB |
Output is correct |
18 |
Correct |
48 ms |
70228 KB |
Output is correct |
19 |
Correct |
47 ms |
70228 KB |
Output is correct |
20 |
Correct |
50 ms |
70288 KB |
Output is correct |
21 |
Correct |
49 ms |
70232 KB |
Output is correct |
22 |
Correct |
51 ms |
70228 KB |
Output is correct |
23 |
Correct |
48 ms |
70240 KB |
Output is correct |
24 |
Correct |
51 ms |
70228 KB |
Output is correct |
25 |
Correct |
49 ms |
70152 KB |
Output is correct |
26 |
Correct |
46 ms |
70100 KB |
Output is correct |
27 |
Correct |
48 ms |
70256 KB |
Output is correct |
28 |
Correct |
50 ms |
70164 KB |
Output is correct |
29 |
Correct |
50 ms |
70244 KB |
Output is correct |
30 |
Correct |
54 ms |
70220 KB |
Output is correct |
31 |
Correct |
49 ms |
70112 KB |
Output is correct |
32 |
Correct |
49 ms |
70128 KB |
Output is correct |
33 |
Correct |
48 ms |
70116 KB |
Output is correct |
34 |
Correct |
50 ms |
70164 KB |
Output is correct |
35 |
Correct |
48 ms |
70176 KB |
Output is correct |
36 |
Correct |
50 ms |
70140 KB |
Output is correct |
37 |
Correct |
52 ms |
70240 KB |
Output is correct |
38 |
Correct |
52 ms |
70236 KB |
Output is correct |
39 |
Correct |
51 ms |
70240 KB |
Output is correct |
40 |
Correct |
57 ms |
70116 KB |
Output is correct |
41 |
Correct |
49 ms |
70376 KB |
Output is correct |
42 |
Correct |
49 ms |
70216 KB |
Output is correct |
43 |
Correct |
55 ms |
70180 KB |
Output is correct |
44 |
Correct |
48 ms |
70196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
70040 KB |
Output is correct |
2 |
Correct |
44 ms |
70052 KB |
Output is correct |
3 |
Correct |
52 ms |
70052 KB |
Output is correct |
4 |
Correct |
48 ms |
70164 KB |
Output is correct |
5 |
Correct |
47 ms |
70208 KB |
Output is correct |
6 |
Correct |
47 ms |
70136 KB |
Output is correct |
7 |
Correct |
50 ms |
70220 KB |
Output is correct |
8 |
Correct |
50 ms |
70092 KB |
Output is correct |
9 |
Correct |
51 ms |
70092 KB |
Output is correct |
10 |
Correct |
46 ms |
70064 KB |
Output is correct |
11 |
Correct |
49 ms |
70300 KB |
Output is correct |
12 |
Correct |
48 ms |
70192 KB |
Output is correct |
13 |
Correct |
47 ms |
70276 KB |
Output is correct |
14 |
Correct |
50 ms |
70124 KB |
Output is correct |
15 |
Correct |
50 ms |
70184 KB |
Output is correct |
16 |
Correct |
49 ms |
70228 KB |
Output is correct |
17 |
Correct |
69 ms |
70236 KB |
Output is correct |
18 |
Correct |
48 ms |
70228 KB |
Output is correct |
19 |
Correct |
47 ms |
70228 KB |
Output is correct |
20 |
Correct |
50 ms |
70288 KB |
Output is correct |
21 |
Correct |
49 ms |
70232 KB |
Output is correct |
22 |
Correct |
51 ms |
70228 KB |
Output is correct |
23 |
Correct |
48 ms |
70240 KB |
Output is correct |
24 |
Correct |
51 ms |
70228 KB |
Output is correct |
25 |
Correct |
49 ms |
70152 KB |
Output is correct |
26 |
Correct |
46 ms |
70100 KB |
Output is correct |
27 |
Correct |
48 ms |
70256 KB |
Output is correct |
28 |
Correct |
50 ms |
70164 KB |
Output is correct |
29 |
Correct |
50 ms |
70244 KB |
Output is correct |
30 |
Correct |
54 ms |
70220 KB |
Output is correct |
31 |
Correct |
49 ms |
70112 KB |
Output is correct |
32 |
Correct |
49 ms |
70128 KB |
Output is correct |
33 |
Correct |
48 ms |
70116 KB |
Output is correct |
34 |
Correct |
50 ms |
70164 KB |
Output is correct |
35 |
Correct |
48 ms |
70176 KB |
Output is correct |
36 |
Correct |
50 ms |
70140 KB |
Output is correct |
37 |
Correct |
52 ms |
70240 KB |
Output is correct |
38 |
Correct |
52 ms |
70236 KB |
Output is correct |
39 |
Correct |
51 ms |
70240 KB |
Output is correct |
40 |
Correct |
57 ms |
70116 KB |
Output is correct |
41 |
Correct |
49 ms |
70376 KB |
Output is correct |
42 |
Correct |
49 ms |
70216 KB |
Output is correct |
43 |
Correct |
55 ms |
70180 KB |
Output is correct |
44 |
Correct |
48 ms |
70196 KB |
Output is correct |
45 |
Correct |
53 ms |
70324 KB |
Output is correct |
46 |
Correct |
60 ms |
71500 KB |
Output is correct |
47 |
Correct |
59 ms |
71452 KB |
Output is correct |
48 |
Correct |
59 ms |
71460 KB |
Output is correct |
49 |
Correct |
53 ms |
70764 KB |
Output is correct |
50 |
Correct |
53 ms |
70688 KB |
Output is correct |
51 |
Correct |
50 ms |
70680 KB |
Output is correct |
52 |
Correct |
59 ms |
71796 KB |
Output is correct |
53 |
Correct |
61 ms |
71828 KB |
Output is correct |
54 |
Correct |
60 ms |
71764 KB |
Output is correct |
55 |
Correct |
59 ms |
71756 KB |
Output is correct |
56 |
Correct |
57 ms |
71816 KB |
Output is correct |
57 |
Correct |
58 ms |
71480 KB |
Output is correct |
58 |
Correct |
66 ms |
71500 KB |
Output is correct |
59 |
Correct |
64 ms |
71500 KB |
Output is correct |
60 |
Correct |
61 ms |
71436 KB |
Output is correct |
61 |
Correct |
55 ms |
71696 KB |
Output is correct |
62 |
Correct |
57 ms |
71680 KB |
Output is correct |
63 |
Correct |
59 ms |
71704 KB |
Output is correct |
64 |
Correct |
55 ms |
71496 KB |
Output is correct |
65 |
Correct |
57 ms |
71540 KB |
Output is correct |
66 |
Correct |
60 ms |
71544 KB |
Output is correct |
67 |
Correct |
57 ms |
71484 KB |
Output is correct |
68 |
Correct |
49 ms |
70684 KB |
Output is correct |
69 |
Correct |
60 ms |
71804 KB |
Output is correct |
70 |
Correct |
60 ms |
71884 KB |
Output is correct |
71 |
Correct |
55 ms |
71712 KB |
Output is correct |
72 |
Correct |
57 ms |
71444 KB |
Output is correct |
73 |
Correct |
59 ms |
71464 KB |
Output is correct |
74 |
Correct |
66 ms |
71672 KB |
Output is correct |
75 |
Correct |
55 ms |
71672 KB |
Output is correct |
76 |
Correct |
56 ms |
71600 KB |
Output is correct |
77 |
Correct |
54 ms |
71600 KB |
Output is correct |
78 |
Correct |
57 ms |
71616 KB |
Output is correct |
79 |
Correct |
56 ms |
71508 KB |
Output is correct |
80 |
Correct |
57 ms |
71572 KB |
Output is correct |
81 |
Correct |
57 ms |
71536 KB |
Output is correct |
82 |
Correct |
60 ms |
71520 KB |
Output is correct |
83 |
Correct |
56 ms |
71528 KB |
Output is correct |
84 |
Correct |
56 ms |
71644 KB |
Output is correct |
85 |
Correct |
65 ms |
71628 KB |
Output is correct |
86 |
Correct |
53 ms |
71504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
70040 KB |
Output is correct |
2 |
Correct |
44 ms |
70052 KB |
Output is correct |
3 |
Correct |
52 ms |
70052 KB |
Output is correct |
4 |
Correct |
48 ms |
70164 KB |
Output is correct |
5 |
Correct |
47 ms |
70208 KB |
Output is correct |
6 |
Correct |
47 ms |
70136 KB |
Output is correct |
7 |
Correct |
50 ms |
70220 KB |
Output is correct |
8 |
Correct |
50 ms |
70092 KB |
Output is correct |
9 |
Correct |
51 ms |
70092 KB |
Output is correct |
10 |
Correct |
46 ms |
70064 KB |
Output is correct |
11 |
Correct |
49 ms |
70300 KB |
Output is correct |
12 |
Correct |
48 ms |
70192 KB |
Output is correct |
13 |
Correct |
47 ms |
70276 KB |
Output is correct |
14 |
Correct |
50 ms |
70124 KB |
Output is correct |
15 |
Correct |
50 ms |
70184 KB |
Output is correct |
16 |
Correct |
49 ms |
70228 KB |
Output is correct |
17 |
Correct |
69 ms |
70236 KB |
Output is correct |
18 |
Correct |
48 ms |
70228 KB |
Output is correct |
19 |
Correct |
47 ms |
70228 KB |
Output is correct |
20 |
Correct |
50 ms |
70288 KB |
Output is correct |
21 |
Correct |
49 ms |
70232 KB |
Output is correct |
22 |
Correct |
51 ms |
70228 KB |
Output is correct |
23 |
Correct |
48 ms |
70240 KB |
Output is correct |
24 |
Correct |
51 ms |
70228 KB |
Output is correct |
25 |
Correct |
49 ms |
70152 KB |
Output is correct |
26 |
Correct |
46 ms |
70100 KB |
Output is correct |
27 |
Correct |
48 ms |
70256 KB |
Output is correct |
28 |
Correct |
50 ms |
70164 KB |
Output is correct |
29 |
Correct |
50 ms |
70244 KB |
Output is correct |
30 |
Correct |
54 ms |
70220 KB |
Output is correct |
31 |
Correct |
49 ms |
70112 KB |
Output is correct |
32 |
Correct |
49 ms |
70128 KB |
Output is correct |
33 |
Correct |
48 ms |
70116 KB |
Output is correct |
34 |
Correct |
50 ms |
70164 KB |
Output is correct |
35 |
Correct |
48 ms |
70176 KB |
Output is correct |
36 |
Correct |
50 ms |
70140 KB |
Output is correct |
37 |
Correct |
52 ms |
70240 KB |
Output is correct |
38 |
Correct |
52 ms |
70236 KB |
Output is correct |
39 |
Correct |
51 ms |
70240 KB |
Output is correct |
40 |
Correct |
57 ms |
70116 KB |
Output is correct |
41 |
Correct |
49 ms |
70376 KB |
Output is correct |
42 |
Correct |
49 ms |
70216 KB |
Output is correct |
43 |
Correct |
55 ms |
70180 KB |
Output is correct |
44 |
Correct |
48 ms |
70196 KB |
Output is correct |
45 |
Correct |
53 ms |
70324 KB |
Output is correct |
46 |
Correct |
60 ms |
71500 KB |
Output is correct |
47 |
Correct |
59 ms |
71452 KB |
Output is correct |
48 |
Correct |
59 ms |
71460 KB |
Output is correct |
49 |
Correct |
53 ms |
70764 KB |
Output is correct |
50 |
Correct |
53 ms |
70688 KB |
Output is correct |
51 |
Correct |
50 ms |
70680 KB |
Output is correct |
52 |
Correct |
59 ms |
71796 KB |
Output is correct |
53 |
Correct |
61 ms |
71828 KB |
Output is correct |
54 |
Correct |
60 ms |
71764 KB |
Output is correct |
55 |
Correct |
59 ms |
71756 KB |
Output is correct |
56 |
Correct |
57 ms |
71816 KB |
Output is correct |
57 |
Correct |
58 ms |
71480 KB |
Output is correct |
58 |
Correct |
66 ms |
71500 KB |
Output is correct |
59 |
Correct |
64 ms |
71500 KB |
Output is correct |
60 |
Correct |
61 ms |
71436 KB |
Output is correct |
61 |
Correct |
55 ms |
71696 KB |
Output is correct |
62 |
Correct |
57 ms |
71680 KB |
Output is correct |
63 |
Correct |
59 ms |
71704 KB |
Output is correct |
64 |
Correct |
55 ms |
71496 KB |
Output is correct |
65 |
Correct |
57 ms |
71540 KB |
Output is correct |
66 |
Correct |
60 ms |
71544 KB |
Output is correct |
67 |
Correct |
57 ms |
71484 KB |
Output is correct |
68 |
Correct |
49 ms |
70684 KB |
Output is correct |
69 |
Correct |
60 ms |
71804 KB |
Output is correct |
70 |
Correct |
60 ms |
71884 KB |
Output is correct |
71 |
Correct |
55 ms |
71712 KB |
Output is correct |
72 |
Correct |
57 ms |
71444 KB |
Output is correct |
73 |
Correct |
59 ms |
71464 KB |
Output is correct |
74 |
Correct |
66 ms |
71672 KB |
Output is correct |
75 |
Correct |
55 ms |
71672 KB |
Output is correct |
76 |
Correct |
56 ms |
71600 KB |
Output is correct |
77 |
Correct |
54 ms |
71600 KB |
Output is correct |
78 |
Correct |
57 ms |
71616 KB |
Output is correct |
79 |
Correct |
56 ms |
71508 KB |
Output is correct |
80 |
Correct |
57 ms |
71572 KB |
Output is correct |
81 |
Correct |
57 ms |
71536 KB |
Output is correct |
82 |
Correct |
60 ms |
71520 KB |
Output is correct |
83 |
Correct |
56 ms |
71528 KB |
Output is correct |
84 |
Correct |
56 ms |
71644 KB |
Output is correct |
85 |
Correct |
65 ms |
71628 KB |
Output is correct |
86 |
Correct |
53 ms |
71504 KB |
Output is correct |
87 |
Correct |
83 ms |
73824 KB |
Output is correct |
88 |
Correct |
267 ms |
81580 KB |
Output is correct |
89 |
Execution timed out |
2080 ms |
104308 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |