#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)2e5+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() {
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 |
91 ms |
139596 KB |
Output is correct |
2 |
Correct |
92 ms |
139664 KB |
Output is correct |
3 |
Correct |
92 ms |
139708 KB |
Output is correct |
4 |
Correct |
87 ms |
139716 KB |
Output is correct |
5 |
Correct |
91 ms |
139908 KB |
Output is correct |
6 |
Correct |
91 ms |
139856 KB |
Output is correct |
7 |
Correct |
93 ms |
139852 KB |
Output is correct |
8 |
Correct |
102 ms |
139896 KB |
Output is correct |
9 |
Correct |
85 ms |
139804 KB |
Output is correct |
10 |
Correct |
84 ms |
139752 KB |
Output is correct |
11 |
Correct |
92 ms |
139856 KB |
Output is correct |
12 |
Correct |
111 ms |
139912 KB |
Output is correct |
13 |
Correct |
97 ms |
139976 KB |
Output is correct |
14 |
Correct |
93 ms |
139808 KB |
Output is correct |
15 |
Correct |
98 ms |
139992 KB |
Output is correct |
16 |
Correct |
94 ms |
139904 KB |
Output is correct |
17 |
Correct |
98 ms |
139788 KB |
Output is correct |
18 |
Correct |
126 ms |
139792 KB |
Output is correct |
19 |
Correct |
96 ms |
139812 KB |
Output is correct |
20 |
Correct |
103 ms |
139872 KB |
Output is correct |
21 |
Correct |
94 ms |
139848 KB |
Output is correct |
22 |
Correct |
93 ms |
139848 KB |
Output is correct |
23 |
Correct |
90 ms |
139836 KB |
Output is correct |
24 |
Correct |
92 ms |
139908 KB |
Output is correct |
25 |
Correct |
94 ms |
139888 KB |
Output is correct |
26 |
Correct |
91 ms |
139720 KB |
Output is correct |
27 |
Correct |
96 ms |
139928 KB |
Output is correct |
28 |
Correct |
98 ms |
139852 KB |
Output is correct |
29 |
Correct |
96 ms |
139924 KB |
Output is correct |
30 |
Correct |
94 ms |
139956 KB |
Output is correct |
31 |
Correct |
95 ms |
139884 KB |
Output is correct |
32 |
Correct |
95 ms |
139880 KB |
Output is correct |
33 |
Correct |
93 ms |
139880 KB |
Output is correct |
34 |
Correct |
92 ms |
139880 KB |
Output is correct |
35 |
Correct |
96 ms |
139884 KB |
Output is correct |
36 |
Correct |
94 ms |
139880 KB |
Output is correct |
37 |
Correct |
96 ms |
139912 KB |
Output is correct |
38 |
Correct |
95 ms |
139904 KB |
Output is correct |
39 |
Correct |
93 ms |
139864 KB |
Output is correct |
40 |
Correct |
96 ms |
139908 KB |
Output is correct |
41 |
Correct |
91 ms |
139980 KB |
Output is correct |
42 |
Correct |
111 ms |
139916 KB |
Output is correct |
43 |
Correct |
99 ms |
139848 KB |
Output is correct |
44 |
Correct |
96 ms |
139820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
139596 KB |
Output is correct |
2 |
Correct |
92 ms |
139664 KB |
Output is correct |
3 |
Correct |
92 ms |
139708 KB |
Output is correct |
4 |
Correct |
87 ms |
139716 KB |
Output is correct |
5 |
Correct |
91 ms |
139908 KB |
Output is correct |
6 |
Correct |
91 ms |
139856 KB |
Output is correct |
7 |
Correct |
93 ms |
139852 KB |
Output is correct |
8 |
Correct |
102 ms |
139896 KB |
Output is correct |
9 |
Correct |
85 ms |
139804 KB |
Output is correct |
10 |
Correct |
84 ms |
139752 KB |
Output is correct |
11 |
Correct |
92 ms |
139856 KB |
Output is correct |
12 |
Correct |
111 ms |
139912 KB |
Output is correct |
13 |
Correct |
97 ms |
139976 KB |
Output is correct |
14 |
Correct |
93 ms |
139808 KB |
Output is correct |
15 |
Correct |
98 ms |
139992 KB |
Output is correct |
16 |
Correct |
94 ms |
139904 KB |
Output is correct |
17 |
Correct |
98 ms |
139788 KB |
Output is correct |
18 |
Correct |
126 ms |
139792 KB |
Output is correct |
19 |
Correct |
96 ms |
139812 KB |
Output is correct |
20 |
Correct |
103 ms |
139872 KB |
Output is correct |
21 |
Correct |
94 ms |
139848 KB |
Output is correct |
22 |
Correct |
93 ms |
139848 KB |
Output is correct |
23 |
Correct |
90 ms |
139836 KB |
Output is correct |
24 |
Correct |
92 ms |
139908 KB |
Output is correct |
25 |
Correct |
94 ms |
139888 KB |
Output is correct |
26 |
Correct |
91 ms |
139720 KB |
Output is correct |
27 |
Correct |
96 ms |
139928 KB |
Output is correct |
28 |
Correct |
98 ms |
139852 KB |
Output is correct |
29 |
Correct |
96 ms |
139924 KB |
Output is correct |
30 |
Correct |
94 ms |
139956 KB |
Output is correct |
31 |
Correct |
95 ms |
139884 KB |
Output is correct |
32 |
Correct |
95 ms |
139880 KB |
Output is correct |
33 |
Correct |
93 ms |
139880 KB |
Output is correct |
34 |
Correct |
92 ms |
139880 KB |
Output is correct |
35 |
Correct |
96 ms |
139884 KB |
Output is correct |
36 |
Correct |
94 ms |
139880 KB |
Output is correct |
37 |
Correct |
96 ms |
139912 KB |
Output is correct |
38 |
Correct |
95 ms |
139904 KB |
Output is correct |
39 |
Correct |
93 ms |
139864 KB |
Output is correct |
40 |
Correct |
96 ms |
139908 KB |
Output is correct |
41 |
Correct |
91 ms |
139980 KB |
Output is correct |
42 |
Correct |
111 ms |
139916 KB |
Output is correct |
43 |
Correct |
99 ms |
139848 KB |
Output is correct |
44 |
Correct |
96 ms |
139820 KB |
Output is correct |
45 |
Correct |
98 ms |
139984 KB |
Output is correct |
46 |
Correct |
106 ms |
141268 KB |
Output is correct |
47 |
Correct |
108 ms |
141308 KB |
Output is correct |
48 |
Correct |
104 ms |
141220 KB |
Output is correct |
49 |
Correct |
98 ms |
140412 KB |
Output is correct |
50 |
Correct |
99 ms |
140464 KB |
Output is correct |
51 |
Correct |
95 ms |
140468 KB |
Output is correct |
52 |
Correct |
110 ms |
141516 KB |
Output is correct |
53 |
Correct |
125 ms |
141568 KB |
Output is correct |
54 |
Correct |
126 ms |
141468 KB |
Output is correct |
55 |
Correct |
106 ms |
141516 KB |
Output is correct |
56 |
Correct |
107 ms |
141444 KB |
Output is correct |
57 |
Correct |
119 ms |
141388 KB |
Output is correct |
58 |
Correct |
109 ms |
141304 KB |
Output is correct |
59 |
Correct |
109 ms |
141236 KB |
Output is correct |
60 |
Correct |
107 ms |
141296 KB |
Output is correct |
61 |
Correct |
100 ms |
141388 KB |
Output is correct |
62 |
Correct |
105 ms |
141448 KB |
Output is correct |
63 |
Correct |
105 ms |
141516 KB |
Output is correct |
64 |
Correct |
113 ms |
141308 KB |
Output is correct |
65 |
Correct |
108 ms |
141204 KB |
Output is correct |
66 |
Correct |
106 ms |
141264 KB |
Output is correct |
67 |
Correct |
129 ms |
141224 KB |
Output is correct |
68 |
Correct |
94 ms |
140468 KB |
Output is correct |
69 |
Correct |
103 ms |
141488 KB |
Output is correct |
70 |
Correct |
107 ms |
141532 KB |
Output is correct |
71 |
Correct |
103 ms |
141324 KB |
Output is correct |
72 |
Correct |
104 ms |
141276 KB |
Output is correct |
73 |
Correct |
109 ms |
141296 KB |
Output is correct |
74 |
Correct |
103 ms |
141352 KB |
Output is correct |
75 |
Correct |
110 ms |
141304 KB |
Output is correct |
76 |
Correct |
103 ms |
141220 KB |
Output is correct |
77 |
Correct |
105 ms |
141420 KB |
Output is correct |
78 |
Correct |
103 ms |
141284 KB |
Output is correct |
79 |
Correct |
102 ms |
141252 KB |
Output is correct |
80 |
Correct |
103 ms |
141296 KB |
Output is correct |
81 |
Correct |
106 ms |
141264 KB |
Output is correct |
82 |
Correct |
103 ms |
141260 KB |
Output is correct |
83 |
Correct |
104 ms |
141264 KB |
Output is correct |
84 |
Correct |
100 ms |
141312 KB |
Output is correct |
85 |
Correct |
100 ms |
141324 KB |
Output is correct |
86 |
Correct |
106 ms |
141292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
139596 KB |
Output is correct |
2 |
Correct |
92 ms |
139664 KB |
Output is correct |
3 |
Correct |
92 ms |
139708 KB |
Output is correct |
4 |
Correct |
87 ms |
139716 KB |
Output is correct |
5 |
Correct |
91 ms |
139908 KB |
Output is correct |
6 |
Correct |
91 ms |
139856 KB |
Output is correct |
7 |
Correct |
93 ms |
139852 KB |
Output is correct |
8 |
Correct |
102 ms |
139896 KB |
Output is correct |
9 |
Correct |
85 ms |
139804 KB |
Output is correct |
10 |
Correct |
84 ms |
139752 KB |
Output is correct |
11 |
Correct |
92 ms |
139856 KB |
Output is correct |
12 |
Correct |
111 ms |
139912 KB |
Output is correct |
13 |
Correct |
97 ms |
139976 KB |
Output is correct |
14 |
Correct |
93 ms |
139808 KB |
Output is correct |
15 |
Correct |
98 ms |
139992 KB |
Output is correct |
16 |
Correct |
94 ms |
139904 KB |
Output is correct |
17 |
Correct |
98 ms |
139788 KB |
Output is correct |
18 |
Correct |
126 ms |
139792 KB |
Output is correct |
19 |
Correct |
96 ms |
139812 KB |
Output is correct |
20 |
Correct |
103 ms |
139872 KB |
Output is correct |
21 |
Correct |
94 ms |
139848 KB |
Output is correct |
22 |
Correct |
93 ms |
139848 KB |
Output is correct |
23 |
Correct |
90 ms |
139836 KB |
Output is correct |
24 |
Correct |
92 ms |
139908 KB |
Output is correct |
25 |
Correct |
94 ms |
139888 KB |
Output is correct |
26 |
Correct |
91 ms |
139720 KB |
Output is correct |
27 |
Correct |
96 ms |
139928 KB |
Output is correct |
28 |
Correct |
98 ms |
139852 KB |
Output is correct |
29 |
Correct |
96 ms |
139924 KB |
Output is correct |
30 |
Correct |
94 ms |
139956 KB |
Output is correct |
31 |
Correct |
95 ms |
139884 KB |
Output is correct |
32 |
Correct |
95 ms |
139880 KB |
Output is correct |
33 |
Correct |
93 ms |
139880 KB |
Output is correct |
34 |
Correct |
92 ms |
139880 KB |
Output is correct |
35 |
Correct |
96 ms |
139884 KB |
Output is correct |
36 |
Correct |
94 ms |
139880 KB |
Output is correct |
37 |
Correct |
96 ms |
139912 KB |
Output is correct |
38 |
Correct |
95 ms |
139904 KB |
Output is correct |
39 |
Correct |
93 ms |
139864 KB |
Output is correct |
40 |
Correct |
96 ms |
139908 KB |
Output is correct |
41 |
Correct |
91 ms |
139980 KB |
Output is correct |
42 |
Correct |
111 ms |
139916 KB |
Output is correct |
43 |
Correct |
99 ms |
139848 KB |
Output is correct |
44 |
Correct |
96 ms |
139820 KB |
Output is correct |
45 |
Correct |
98 ms |
139984 KB |
Output is correct |
46 |
Correct |
106 ms |
141268 KB |
Output is correct |
47 |
Correct |
108 ms |
141308 KB |
Output is correct |
48 |
Correct |
104 ms |
141220 KB |
Output is correct |
49 |
Correct |
98 ms |
140412 KB |
Output is correct |
50 |
Correct |
99 ms |
140464 KB |
Output is correct |
51 |
Correct |
95 ms |
140468 KB |
Output is correct |
52 |
Correct |
110 ms |
141516 KB |
Output is correct |
53 |
Correct |
125 ms |
141568 KB |
Output is correct |
54 |
Correct |
126 ms |
141468 KB |
Output is correct |
55 |
Correct |
106 ms |
141516 KB |
Output is correct |
56 |
Correct |
107 ms |
141444 KB |
Output is correct |
57 |
Correct |
119 ms |
141388 KB |
Output is correct |
58 |
Correct |
109 ms |
141304 KB |
Output is correct |
59 |
Correct |
109 ms |
141236 KB |
Output is correct |
60 |
Correct |
107 ms |
141296 KB |
Output is correct |
61 |
Correct |
100 ms |
141388 KB |
Output is correct |
62 |
Correct |
105 ms |
141448 KB |
Output is correct |
63 |
Correct |
105 ms |
141516 KB |
Output is correct |
64 |
Correct |
113 ms |
141308 KB |
Output is correct |
65 |
Correct |
108 ms |
141204 KB |
Output is correct |
66 |
Correct |
106 ms |
141264 KB |
Output is correct |
67 |
Correct |
129 ms |
141224 KB |
Output is correct |
68 |
Correct |
94 ms |
140468 KB |
Output is correct |
69 |
Correct |
103 ms |
141488 KB |
Output is correct |
70 |
Correct |
107 ms |
141532 KB |
Output is correct |
71 |
Correct |
103 ms |
141324 KB |
Output is correct |
72 |
Correct |
104 ms |
141276 KB |
Output is correct |
73 |
Correct |
109 ms |
141296 KB |
Output is correct |
74 |
Correct |
103 ms |
141352 KB |
Output is correct |
75 |
Correct |
110 ms |
141304 KB |
Output is correct |
76 |
Correct |
103 ms |
141220 KB |
Output is correct |
77 |
Correct |
105 ms |
141420 KB |
Output is correct |
78 |
Correct |
103 ms |
141284 KB |
Output is correct |
79 |
Correct |
102 ms |
141252 KB |
Output is correct |
80 |
Correct |
103 ms |
141296 KB |
Output is correct |
81 |
Correct |
106 ms |
141264 KB |
Output is correct |
82 |
Correct |
103 ms |
141260 KB |
Output is correct |
83 |
Correct |
104 ms |
141264 KB |
Output is correct |
84 |
Correct |
100 ms |
141312 KB |
Output is correct |
85 |
Correct |
100 ms |
141324 KB |
Output is correct |
86 |
Correct |
106 ms |
141292 KB |
Output is correct |
87 |
Correct |
137 ms |
143720 KB |
Output is correct |
88 |
Correct |
330 ms |
151856 KB |
Output is correct |
89 |
Execution timed out |
2073 ms |
177200 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |