#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
int N;
int A[101010], B[101010], C[101010];
int L[101010], R[101010], P[101010], H[101010], num;
int Max[404040], BIT[101010], D[101010];
vector<int> adj[101010], comp;
void updMax(int id, int s, int e, int t, int v){
if (e < t || t < s) return;
if (s == e){
Max[id] = v;
return;
}
int mid = (s+e)/2;
updMax(id*2, s, mid, t, v);
updMax(id*2+1, mid+1, e, t, v);
Max[id] = max(Max[id*2], Max[id*2+1]);
}
int RMQ(int id, int s, int e, int ts, int te){
if (e < ts || te < s) return 0;
if (ts <= s && e <= te) return Max[id];
int mid = (s+e)/2;
return max(RMQ(id*2, s, mid, ts, te), RMQ(id*2+1, mid+1, e, ts, te));
}
void updBIT(int t, int v){
while (t<=N){
BIT[t] += v;
t += t & -t;
}
}
int read(int t){
int ret = 0;
while (t){
ret += BIT[t];
t -= t & -t;
}
return ret;
}
void dfs(int u, int p){
L[u] = ++num, P[u] = p;
for (int v : adj[u]){
if (v == p) continue;
D[v] = D[u] + 1;
dfs(v, u);
}
R[u] = num;
}
int main(){
scanf("%d", &N);
for (int i=1; i<=N; i++){
scanf("%d", &C[i]);
comp.push_back(C[i]);
}
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
for (int i=1; i<=N; i++) C[i] = lower_bound(comp.begin(), comp.end(), C[i])-comp.begin()+1;
for (int i=2; i<=N; i++){
scanf("%d %d", &A[i], &B[i]);
adj[A[i]].push_back(B[i]);
adj[B[i]].push_back(A[i]);
}
D[1] = 1, B[1] = 1;
dfs(1, 0);
updMax(1, 1, N, L[1], 1);
for (int i=2; i<=N; i++){
int u = A[i];
LL ans = 0;
vector<pii> E;
while (u != 0){
int v = B[RMQ(1, 1, N, L[u], R[u])];
ans += (LL)(D[u]-D[H[v]]) * read(C[v]-1);
updBIT(C[v], D[u]-D[H[v]]);
E.push_back(pii(C[v], D[u]-D[H[v]]));
int t = u;
u = H[v], H[v] = t;
}
for (pii t : E) updBIT(t.first, -t.second);
printf("%lld\n", ans);
updMax(1, 1, N, L[B[i]], i);
}
return 0;
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:58:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
construction.cpp:60:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &C[i]);
~~~~~^~~~~~~~~~~~~
construction.cpp:67:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &A[i], &B[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2808 KB |
Output is correct |
3 |
Correct |
4 ms |
2808 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
5 ms |
2808 KB |
Output is correct |
6 |
Correct |
5 ms |
2808 KB |
Output is correct |
7 |
Correct |
5 ms |
2808 KB |
Output is correct |
8 |
Correct |
4 ms |
2808 KB |
Output is correct |
9 |
Correct |
4 ms |
2808 KB |
Output is correct |
10 |
Correct |
4 ms |
2808 KB |
Output is correct |
11 |
Correct |
5 ms |
2808 KB |
Output is correct |
12 |
Correct |
5 ms |
2808 KB |
Output is correct |
13 |
Correct |
5 ms |
2736 KB |
Output is correct |
14 |
Correct |
4 ms |
2808 KB |
Output is correct |
15 |
Correct |
5 ms |
2808 KB |
Output is correct |
16 |
Correct |
5 ms |
2812 KB |
Output is correct |
17 |
Correct |
5 ms |
2808 KB |
Output is correct |
18 |
Correct |
5 ms |
2808 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
5 ms |
2808 KB |
Output is correct |
21 |
Correct |
5 ms |
2808 KB |
Output is correct |
22 |
Correct |
5 ms |
2808 KB |
Output is correct |
23 |
Correct |
5 ms |
2808 KB |
Output is correct |
24 |
Correct |
5 ms |
2808 KB |
Output is correct |
25 |
Correct |
5 ms |
2808 KB |
Output is correct |
26 |
Correct |
4 ms |
2808 KB |
Output is correct |
27 |
Correct |
4 ms |
2808 KB |
Output is correct |
28 |
Correct |
5 ms |
2808 KB |
Output is correct |
29 |
Correct |
5 ms |
2808 KB |
Output is correct |
30 |
Correct |
5 ms |
2808 KB |
Output is correct |
31 |
Correct |
5 ms |
2808 KB |
Output is correct |
32 |
Correct |
5 ms |
2808 KB |
Output is correct |
33 |
Correct |
6 ms |
2808 KB |
Output is correct |
34 |
Correct |
5 ms |
2780 KB |
Output is correct |
35 |
Correct |
5 ms |
2812 KB |
Output is correct |
36 |
Correct |
6 ms |
2812 KB |
Output is correct |
37 |
Correct |
5 ms |
2808 KB |
Output is correct |
38 |
Correct |
5 ms |
2808 KB |
Output is correct |
39 |
Correct |
4 ms |
2808 KB |
Output is correct |
40 |
Correct |
5 ms |
2808 KB |
Output is correct |
41 |
Correct |
5 ms |
2808 KB |
Output is correct |
42 |
Correct |
13 ms |
2808 KB |
Output is correct |
43 |
Correct |
6 ms |
2808 KB |
Output is correct |
44 |
Correct |
5 ms |
2808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2808 KB |
Output is correct |
3 |
Correct |
4 ms |
2808 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
5 ms |
2808 KB |
Output is correct |
6 |
Correct |
5 ms |
2808 KB |
Output is correct |
7 |
Correct |
5 ms |
2808 KB |
Output is correct |
8 |
Correct |
4 ms |
2808 KB |
Output is correct |
9 |
Correct |
4 ms |
2808 KB |
Output is correct |
10 |
Correct |
4 ms |
2808 KB |
Output is correct |
11 |
Correct |
5 ms |
2808 KB |
Output is correct |
12 |
Correct |
5 ms |
2808 KB |
Output is correct |
13 |
Correct |
5 ms |
2736 KB |
Output is correct |
14 |
Correct |
4 ms |
2808 KB |
Output is correct |
15 |
Correct |
5 ms |
2808 KB |
Output is correct |
16 |
Correct |
5 ms |
2812 KB |
Output is correct |
17 |
Correct |
5 ms |
2808 KB |
Output is correct |
18 |
Correct |
5 ms |
2808 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
5 ms |
2808 KB |
Output is correct |
21 |
Correct |
5 ms |
2808 KB |
Output is correct |
22 |
Correct |
5 ms |
2808 KB |
Output is correct |
23 |
Correct |
5 ms |
2808 KB |
Output is correct |
24 |
Correct |
5 ms |
2808 KB |
Output is correct |
25 |
Correct |
5 ms |
2808 KB |
Output is correct |
26 |
Correct |
4 ms |
2808 KB |
Output is correct |
27 |
Correct |
4 ms |
2808 KB |
Output is correct |
28 |
Correct |
5 ms |
2808 KB |
Output is correct |
29 |
Correct |
5 ms |
2808 KB |
Output is correct |
30 |
Correct |
5 ms |
2808 KB |
Output is correct |
31 |
Correct |
5 ms |
2808 KB |
Output is correct |
32 |
Correct |
5 ms |
2808 KB |
Output is correct |
33 |
Correct |
6 ms |
2808 KB |
Output is correct |
34 |
Correct |
5 ms |
2780 KB |
Output is correct |
35 |
Correct |
5 ms |
2812 KB |
Output is correct |
36 |
Correct |
6 ms |
2812 KB |
Output is correct |
37 |
Correct |
5 ms |
2808 KB |
Output is correct |
38 |
Correct |
5 ms |
2808 KB |
Output is correct |
39 |
Correct |
4 ms |
2808 KB |
Output is correct |
40 |
Correct |
5 ms |
2808 KB |
Output is correct |
41 |
Correct |
5 ms |
2808 KB |
Output is correct |
42 |
Correct |
13 ms |
2808 KB |
Output is correct |
43 |
Correct |
6 ms |
2808 KB |
Output is correct |
44 |
Correct |
5 ms |
2808 KB |
Output is correct |
45 |
Correct |
7 ms |
2808 KB |
Output is correct |
46 |
Correct |
13 ms |
3064 KB |
Output is correct |
47 |
Correct |
14 ms |
3064 KB |
Output is correct |
48 |
Correct |
13 ms |
3064 KB |
Output is correct |
49 |
Correct |
9 ms |
3320 KB |
Output is correct |
50 |
Correct |
10 ms |
3192 KB |
Output is correct |
51 |
Correct |
9 ms |
3192 KB |
Output is correct |
52 |
Correct |
10 ms |
3192 KB |
Output is correct |
53 |
Correct |
10 ms |
3192 KB |
Output is correct |
54 |
Correct |
10 ms |
3192 KB |
Output is correct |
55 |
Correct |
10 ms |
3192 KB |
Output is correct |
56 |
Correct |
10 ms |
3192 KB |
Output is correct |
57 |
Correct |
16 ms |
3064 KB |
Output is correct |
58 |
Correct |
17 ms |
3064 KB |
Output is correct |
59 |
Correct |
17 ms |
3064 KB |
Output is correct |
60 |
Correct |
17 ms |
3064 KB |
Output is correct |
61 |
Correct |
11 ms |
3192 KB |
Output is correct |
62 |
Correct |
10 ms |
3192 KB |
Output is correct |
63 |
Correct |
10 ms |
3192 KB |
Output is correct |
64 |
Correct |
12 ms |
3064 KB |
Output is correct |
65 |
Correct |
12 ms |
3064 KB |
Output is correct |
66 |
Correct |
12 ms |
3064 KB |
Output is correct |
67 |
Correct |
12 ms |
3036 KB |
Output is correct |
68 |
Correct |
10 ms |
3192 KB |
Output is correct |
69 |
Correct |
10 ms |
3192 KB |
Output is correct |
70 |
Correct |
9 ms |
3196 KB |
Output is correct |
71 |
Correct |
9 ms |
3188 KB |
Output is correct |
72 |
Correct |
17 ms |
3064 KB |
Output is correct |
73 |
Correct |
17 ms |
3064 KB |
Output is correct |
74 |
Correct |
9 ms |
3064 KB |
Output is correct |
75 |
Correct |
10 ms |
3064 KB |
Output is correct |
76 |
Correct |
10 ms |
3064 KB |
Output is correct |
77 |
Correct |
10 ms |
3064 KB |
Output is correct |
78 |
Correct |
9 ms |
3068 KB |
Output is correct |
79 |
Correct |
9 ms |
3128 KB |
Output is correct |
80 |
Correct |
9 ms |
3064 KB |
Output is correct |
81 |
Correct |
10 ms |
3064 KB |
Output is correct |
82 |
Correct |
10 ms |
3064 KB |
Output is correct |
83 |
Correct |
10 ms |
3064 KB |
Output is correct |
84 |
Correct |
9 ms |
3064 KB |
Output is correct |
85 |
Correct |
9 ms |
3064 KB |
Output is correct |
86 |
Correct |
10 ms |
3064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2808 KB |
Output is correct |
3 |
Correct |
4 ms |
2808 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
5 ms |
2808 KB |
Output is correct |
6 |
Correct |
5 ms |
2808 KB |
Output is correct |
7 |
Correct |
5 ms |
2808 KB |
Output is correct |
8 |
Correct |
4 ms |
2808 KB |
Output is correct |
9 |
Correct |
4 ms |
2808 KB |
Output is correct |
10 |
Correct |
4 ms |
2808 KB |
Output is correct |
11 |
Correct |
5 ms |
2808 KB |
Output is correct |
12 |
Correct |
5 ms |
2808 KB |
Output is correct |
13 |
Correct |
5 ms |
2736 KB |
Output is correct |
14 |
Correct |
4 ms |
2808 KB |
Output is correct |
15 |
Correct |
5 ms |
2808 KB |
Output is correct |
16 |
Correct |
5 ms |
2812 KB |
Output is correct |
17 |
Correct |
5 ms |
2808 KB |
Output is correct |
18 |
Correct |
5 ms |
2808 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
5 ms |
2808 KB |
Output is correct |
21 |
Correct |
5 ms |
2808 KB |
Output is correct |
22 |
Correct |
5 ms |
2808 KB |
Output is correct |
23 |
Correct |
5 ms |
2808 KB |
Output is correct |
24 |
Correct |
5 ms |
2808 KB |
Output is correct |
25 |
Correct |
5 ms |
2808 KB |
Output is correct |
26 |
Correct |
4 ms |
2808 KB |
Output is correct |
27 |
Correct |
4 ms |
2808 KB |
Output is correct |
28 |
Correct |
5 ms |
2808 KB |
Output is correct |
29 |
Correct |
5 ms |
2808 KB |
Output is correct |
30 |
Correct |
5 ms |
2808 KB |
Output is correct |
31 |
Correct |
5 ms |
2808 KB |
Output is correct |
32 |
Correct |
5 ms |
2808 KB |
Output is correct |
33 |
Correct |
6 ms |
2808 KB |
Output is correct |
34 |
Correct |
5 ms |
2780 KB |
Output is correct |
35 |
Correct |
5 ms |
2812 KB |
Output is correct |
36 |
Correct |
6 ms |
2812 KB |
Output is correct |
37 |
Correct |
5 ms |
2808 KB |
Output is correct |
38 |
Correct |
5 ms |
2808 KB |
Output is correct |
39 |
Correct |
4 ms |
2808 KB |
Output is correct |
40 |
Correct |
5 ms |
2808 KB |
Output is correct |
41 |
Correct |
5 ms |
2808 KB |
Output is correct |
42 |
Correct |
13 ms |
2808 KB |
Output is correct |
43 |
Correct |
6 ms |
2808 KB |
Output is correct |
44 |
Correct |
5 ms |
2808 KB |
Output is correct |
45 |
Correct |
7 ms |
2808 KB |
Output is correct |
46 |
Correct |
13 ms |
3064 KB |
Output is correct |
47 |
Correct |
14 ms |
3064 KB |
Output is correct |
48 |
Correct |
13 ms |
3064 KB |
Output is correct |
49 |
Correct |
9 ms |
3320 KB |
Output is correct |
50 |
Correct |
10 ms |
3192 KB |
Output is correct |
51 |
Correct |
9 ms |
3192 KB |
Output is correct |
52 |
Correct |
10 ms |
3192 KB |
Output is correct |
53 |
Correct |
10 ms |
3192 KB |
Output is correct |
54 |
Correct |
10 ms |
3192 KB |
Output is correct |
55 |
Correct |
10 ms |
3192 KB |
Output is correct |
56 |
Correct |
10 ms |
3192 KB |
Output is correct |
57 |
Correct |
16 ms |
3064 KB |
Output is correct |
58 |
Correct |
17 ms |
3064 KB |
Output is correct |
59 |
Correct |
17 ms |
3064 KB |
Output is correct |
60 |
Correct |
17 ms |
3064 KB |
Output is correct |
61 |
Correct |
11 ms |
3192 KB |
Output is correct |
62 |
Correct |
10 ms |
3192 KB |
Output is correct |
63 |
Correct |
10 ms |
3192 KB |
Output is correct |
64 |
Correct |
12 ms |
3064 KB |
Output is correct |
65 |
Correct |
12 ms |
3064 KB |
Output is correct |
66 |
Correct |
12 ms |
3064 KB |
Output is correct |
67 |
Correct |
12 ms |
3036 KB |
Output is correct |
68 |
Correct |
10 ms |
3192 KB |
Output is correct |
69 |
Correct |
10 ms |
3192 KB |
Output is correct |
70 |
Correct |
9 ms |
3196 KB |
Output is correct |
71 |
Correct |
9 ms |
3188 KB |
Output is correct |
72 |
Correct |
17 ms |
3064 KB |
Output is correct |
73 |
Correct |
17 ms |
3064 KB |
Output is correct |
74 |
Correct |
9 ms |
3064 KB |
Output is correct |
75 |
Correct |
10 ms |
3064 KB |
Output is correct |
76 |
Correct |
10 ms |
3064 KB |
Output is correct |
77 |
Correct |
10 ms |
3064 KB |
Output is correct |
78 |
Correct |
9 ms |
3068 KB |
Output is correct |
79 |
Correct |
9 ms |
3128 KB |
Output is correct |
80 |
Correct |
9 ms |
3064 KB |
Output is correct |
81 |
Correct |
10 ms |
3064 KB |
Output is correct |
82 |
Correct |
10 ms |
3064 KB |
Output is correct |
83 |
Correct |
10 ms |
3064 KB |
Output is correct |
84 |
Correct |
9 ms |
3064 KB |
Output is correct |
85 |
Correct |
9 ms |
3064 KB |
Output is correct |
86 |
Correct |
10 ms |
3064 KB |
Output is correct |
87 |
Correct |
29 ms |
3576 KB |
Output is correct |
88 |
Correct |
94 ms |
5240 KB |
Output is correct |
89 |
Correct |
441 ms |
11224 KB |
Output is correct |
90 |
Correct |
424 ms |
11272 KB |
Output is correct |
91 |
Correct |
443 ms |
11452 KB |
Output is correct |
92 |
Correct |
184 ms |
15728 KB |
Output is correct |
93 |
Correct |
194 ms |
15748 KB |
Output is correct |
94 |
Correct |
180 ms |
15728 KB |
Output is correct |
95 |
Correct |
209 ms |
14200 KB |
Output is correct |
96 |
Correct |
218 ms |
14308 KB |
Output is correct |
97 |
Correct |
222 ms |
14164 KB |
Output is correct |
98 |
Correct |
215 ms |
14236 KB |
Output is correct |
99 |
Correct |
219 ms |
13680 KB |
Output is correct |
100 |
Correct |
735 ms |
11332 KB |
Output is correct |
101 |
Correct |
769 ms |
11920 KB |
Output is correct |
102 |
Correct |
758 ms |
11708 KB |
Output is correct |
103 |
Correct |
752 ms |
11748 KB |
Output is correct |
104 |
Correct |
235 ms |
13996 KB |
Output is correct |
105 |
Correct |
235 ms |
14064 KB |
Output is correct |
106 |
Correct |
252 ms |
13932 KB |
Output is correct |
107 |
Correct |
388 ms |
11368 KB |
Output is correct |
108 |
Correct |
466 ms |
11372 KB |
Output is correct |
109 |
Correct |
432 ms |
11576 KB |
Output is correct |
110 |
Correct |
219 ms |
15852 KB |
Output is correct |
111 |
Correct |
216 ms |
14672 KB |
Output is correct |
112 |
Correct |
208 ms |
14208 KB |
Output is correct |
113 |
Correct |
195 ms |
13676 KB |
Output is correct |
114 |
Correct |
698 ms |
11916 KB |
Output is correct |
115 |
Correct |
703 ms |
11320 KB |
Output is correct |
116 |
Correct |
226 ms |
13800 KB |
Output is correct |
117 |
Correct |
213 ms |
12796 KB |
Output is correct |
118 |
Correct |
202 ms |
12372 KB |
Output is correct |
119 |
Correct |
202 ms |
12192 KB |
Output is correct |
120 |
Correct |
186 ms |
12316 KB |
Output is correct |
121 |
Correct |
212 ms |
12116 KB |
Output is correct |
122 |
Correct |
194 ms |
11968 KB |
Output is correct |
123 |
Correct |
234 ms |
12784 KB |
Output is correct |
124 |
Correct |
259 ms |
12600 KB |
Output is correct |
125 |
Correct |
231 ms |
12272 KB |
Output is correct |
126 |
Correct |
261 ms |
12612 KB |
Output is correct |
127 |
Correct |
236 ms |
12184 KB |
Output is correct |
128 |
Correct |
218 ms |
11960 KB |
Output is correct |