#include<stdio.h>
#include<vector>
using namespace std;
int par[121212][20];
int num[121212], S[121212], E[121212], cnt, tn, depth[121212];;
vector<int>edge[121212];
struct xy { int x, y; }tree[313131];
xy max(xy a, xy b) { if (a.x > b.x)return a; return b; }
void insert_g(int w, xy g) { for (int i = w + tn; i > 0; i /= 2)tree[i] = max(tree[i], g); }
int search_g(int s, int e) {
s += tn; e += tn; xy res = { -1,0 };
while (s <= e) {
if ((s & 1) == 1)res = max(res, tree[s++]);
if ((e & 1) == 0)res = max(res, tree[e--]);
s /= 2; e /= 2;
}
return res.y;
}
void dfs(int w, int bef, int dep) {
par[w][0] = bef; depth[w] = dep;
for (int i = 0; par[w][i] != -1; i++)par[w][i + 1] = par[par[w][i]][i];
num[w] = S[w] = ++cnt;
for (int i = 0; i < edge[w].size(); i++)if (edge[w][i] != bef) {
dfs(edge[w][i], w, dep + 1);
}
E[w] = cnt;
}
int get_C(int x) { return search_g(S[x], E[x]); }
int A[121212], B[121212];
int C[121212], CSD[121212];
int go(int a, int b) { for (int i = 0; i < 20; i++)if ((1 << i)&b)a = par[a][i]; return a; }
vector<xy>L;
xy temp[121212];
long long f(int s, int e) {
if (s == e)return 0;
int m = (s + e) / 2, i;
long long res = f(s, m) + f(m + 1, e);
int p1 = s, p2 = m + 1;
int jw = m, S = 0;
for (i = s; i <= m; i++) {
while (jw + 1 <= e&&L[i].x < L[jw + 1].x)S += L[++jw].y;
res += (long long)L[i].y * S;
}
for (i = s; i <= e; i++) {
if (p1 == m + 1 || (p2 != e + 1 && L[p1].x < L[p2].x)) temp[i] = L[p2++];
else temp[i] = L[p1++];
}
for (i = s; i <= e; i++) L[i] = temp[i];
return res;
}
int main() {
int n; scanf("%d", &n);
int i, j;
for (tn = 1; tn < n + 1; tn *= 2);
for (i = 0; i < tn * 2; i++)tree[i].x = -1;
for (i = 1; i <= n; i++)scanf("%d", &C[i]);
for (i = 1; i <= n; i++)for (j = 0; j < 20; j++)par[i][j] = -1;
for (i = 1; i <= n - 1; i++) {
int s, e; scanf("%d%d", &s, &e);
A[i] = s; B[i] = e;
edge[s].push_back(e); edge[e].push_back(s);
}
dfs(1, -1, 0);
CSD[1] = 0; insert_g(1, { 0,1 });
for (i = 1; i <= n - 1; i++) {
L.clear();
int e = B[i], now = par[e][0];
while (now != -1) {
int nowC = get_C(now);
int cs = go(now, depth[now] - CSD[nowC]);
L.push_back({ C[nowC],depth[now] - CSD[nowC] + 1 });
CSD[nowC] = depth[now] + 1;
now = par[cs][0];
}
insert_g(num[e], { i, e });
printf("%lld\n", f(0, L.size() - 1));
}
return 0;
}
Compilation message
construction.cpp: In function 'void dfs(int, int, int)':
construction.cpp:23:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < edge[w].size(); i++)if (edge[w][i] != bef) {
~~^~~~~~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:52:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int n; scanf("%d", &n);
~~~~~^~~~~~~~~~
construction.cpp:56:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (i = 1; i <= n; i++)scanf("%d", &C[i]);
~~~~~^~~~~~~~~~~~~
construction.cpp:59:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int s, e; scanf("%d%d", &s, &e);
~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
3320 KB |
Output is correct |
2 |
Correct |
4 ms |
3320 KB |
Output is correct |
3 |
Correct |
4 ms |
3380 KB |
Output is correct |
4 |
Correct |
5 ms |
3424 KB |
Output is correct |
5 |
Correct |
5 ms |
3476 KB |
Output is correct |
6 |
Correct |
6 ms |
3524 KB |
Output is correct |
7 |
Correct |
6 ms |
3544 KB |
Output is correct |
8 |
Correct |
5 ms |
3552 KB |
Output is correct |
9 |
Correct |
4 ms |
3552 KB |
Output is correct |
10 |
Correct |
5 ms |
3576 KB |
Output is correct |
11 |
Correct |
6 ms |
3576 KB |
Output is correct |
12 |
Correct |
5 ms |
3576 KB |
Output is correct |
13 |
Correct |
5 ms |
3660 KB |
Output is correct |
14 |
Correct |
5 ms |
3660 KB |
Output is correct |
15 |
Correct |
7 ms |
3660 KB |
Output is correct |
16 |
Correct |
6 ms |
3660 KB |
Output is correct |
17 |
Correct |
6 ms |
3660 KB |
Output is correct |
18 |
Correct |
7 ms |
3660 KB |
Output is correct |
19 |
Correct |
6 ms |
3660 KB |
Output is correct |
20 |
Correct |
6 ms |
3660 KB |
Output is correct |
21 |
Correct |
7 ms |
3704 KB |
Output is correct |
22 |
Correct |
5 ms |
3704 KB |
Output is correct |
23 |
Correct |
5 ms |
3704 KB |
Output is correct |
24 |
Correct |
6 ms |
3704 KB |
Output is correct |
25 |
Correct |
7 ms |
3704 KB |
Output is correct |
26 |
Correct |
5 ms |
3704 KB |
Output is correct |
27 |
Correct |
6 ms |
3704 KB |
Output is correct |
28 |
Correct |
6 ms |
3704 KB |
Output is correct |
29 |
Correct |
5 ms |
3704 KB |
Output is correct |
30 |
Correct |
7 ms |
3704 KB |
Output is correct |
31 |
Correct |
6 ms |
3704 KB |
Output is correct |
32 |
Correct |
5 ms |
3704 KB |
Output is correct |
33 |
Correct |
6 ms |
3704 KB |
Output is correct |
34 |
Correct |
5 ms |
3704 KB |
Output is correct |
35 |
Correct |
5 ms |
3704 KB |
Output is correct |
36 |
Correct |
5 ms |
3704 KB |
Output is correct |
37 |
Correct |
6 ms |
3704 KB |
Output is correct |
38 |
Correct |
5 ms |
3704 KB |
Output is correct |
39 |
Correct |
5 ms |
3704 KB |
Output is correct |
40 |
Correct |
5 ms |
3704 KB |
Output is correct |
41 |
Correct |
6 ms |
3704 KB |
Output is correct |
42 |
Correct |
7 ms |
3704 KB |
Output is correct |
43 |
Correct |
7 ms |
3704 KB |
Output is correct |
44 |
Correct |
7 ms |
3704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
3320 KB |
Output is correct |
2 |
Correct |
4 ms |
3320 KB |
Output is correct |
3 |
Correct |
4 ms |
3380 KB |
Output is correct |
4 |
Correct |
5 ms |
3424 KB |
Output is correct |
5 |
Correct |
5 ms |
3476 KB |
Output is correct |
6 |
Correct |
6 ms |
3524 KB |
Output is correct |
7 |
Correct |
6 ms |
3544 KB |
Output is correct |
8 |
Correct |
5 ms |
3552 KB |
Output is correct |
9 |
Correct |
4 ms |
3552 KB |
Output is correct |
10 |
Correct |
5 ms |
3576 KB |
Output is correct |
11 |
Correct |
6 ms |
3576 KB |
Output is correct |
12 |
Correct |
5 ms |
3576 KB |
Output is correct |
13 |
Correct |
5 ms |
3660 KB |
Output is correct |
14 |
Correct |
5 ms |
3660 KB |
Output is correct |
15 |
Correct |
7 ms |
3660 KB |
Output is correct |
16 |
Correct |
6 ms |
3660 KB |
Output is correct |
17 |
Correct |
6 ms |
3660 KB |
Output is correct |
18 |
Correct |
7 ms |
3660 KB |
Output is correct |
19 |
Correct |
6 ms |
3660 KB |
Output is correct |
20 |
Correct |
6 ms |
3660 KB |
Output is correct |
21 |
Correct |
7 ms |
3704 KB |
Output is correct |
22 |
Correct |
5 ms |
3704 KB |
Output is correct |
23 |
Correct |
5 ms |
3704 KB |
Output is correct |
24 |
Correct |
6 ms |
3704 KB |
Output is correct |
25 |
Correct |
7 ms |
3704 KB |
Output is correct |
26 |
Correct |
5 ms |
3704 KB |
Output is correct |
27 |
Correct |
6 ms |
3704 KB |
Output is correct |
28 |
Correct |
6 ms |
3704 KB |
Output is correct |
29 |
Correct |
5 ms |
3704 KB |
Output is correct |
30 |
Correct |
7 ms |
3704 KB |
Output is correct |
31 |
Correct |
6 ms |
3704 KB |
Output is correct |
32 |
Correct |
5 ms |
3704 KB |
Output is correct |
33 |
Correct |
6 ms |
3704 KB |
Output is correct |
34 |
Correct |
5 ms |
3704 KB |
Output is correct |
35 |
Correct |
5 ms |
3704 KB |
Output is correct |
36 |
Correct |
5 ms |
3704 KB |
Output is correct |
37 |
Correct |
6 ms |
3704 KB |
Output is correct |
38 |
Correct |
5 ms |
3704 KB |
Output is correct |
39 |
Correct |
5 ms |
3704 KB |
Output is correct |
40 |
Correct |
5 ms |
3704 KB |
Output is correct |
41 |
Correct |
6 ms |
3704 KB |
Output is correct |
42 |
Correct |
7 ms |
3704 KB |
Output is correct |
43 |
Correct |
7 ms |
3704 KB |
Output is correct |
44 |
Correct |
7 ms |
3704 KB |
Output is correct |
45 |
Correct |
7 ms |
3704 KB |
Output is correct |
46 |
Correct |
13 ms |
4104 KB |
Output is correct |
47 |
Correct |
14 ms |
4104 KB |
Output is correct |
48 |
Correct |
17 ms |
4120 KB |
Output is correct |
49 |
Correct |
15 ms |
4380 KB |
Output is correct |
50 |
Correct |
12 ms |
4460 KB |
Output is correct |
51 |
Correct |
12 ms |
4480 KB |
Output is correct |
52 |
Correct |
11 ms |
4480 KB |
Output is correct |
53 |
Correct |
11 ms |
4496 KB |
Output is correct |
54 |
Correct |
13 ms |
4548 KB |
Output is correct |
55 |
Correct |
15 ms |
4576 KB |
Output is correct |
56 |
Correct |
15 ms |
4624 KB |
Output is correct |
57 |
Correct |
18 ms |
4624 KB |
Output is correct |
58 |
Correct |
20 ms |
4624 KB |
Output is correct |
59 |
Correct |
18 ms |
4624 KB |
Output is correct |
60 |
Correct |
17 ms |
4624 KB |
Output is correct |
61 |
Correct |
12 ms |
4652 KB |
Output is correct |
62 |
Correct |
18 ms |
4652 KB |
Output is correct |
63 |
Correct |
12 ms |
4652 KB |
Output is correct |
64 |
Correct |
25 ms |
4652 KB |
Output is correct |
65 |
Correct |
13 ms |
4652 KB |
Output is correct |
66 |
Correct |
14 ms |
4652 KB |
Output is correct |
67 |
Correct |
14 ms |
4652 KB |
Output is correct |
68 |
Correct |
14 ms |
4712 KB |
Output is correct |
69 |
Correct |
9 ms |
4712 KB |
Output is correct |
70 |
Correct |
10 ms |
4732 KB |
Output is correct |
71 |
Correct |
9 ms |
4760 KB |
Output is correct |
72 |
Correct |
14 ms |
4760 KB |
Output is correct |
73 |
Correct |
14 ms |
4760 KB |
Output is correct |
74 |
Correct |
9 ms |
4760 KB |
Output is correct |
75 |
Correct |
14 ms |
4760 KB |
Output is correct |
76 |
Correct |
8 ms |
4760 KB |
Output is correct |
77 |
Correct |
15 ms |
4760 KB |
Output is correct |
78 |
Correct |
10 ms |
4760 KB |
Output is correct |
79 |
Correct |
9 ms |
4760 KB |
Output is correct |
80 |
Correct |
9 ms |
4760 KB |
Output is correct |
81 |
Correct |
10 ms |
4760 KB |
Output is correct |
82 |
Correct |
10 ms |
4760 KB |
Output is correct |
83 |
Correct |
11 ms |
4760 KB |
Output is correct |
84 |
Correct |
15 ms |
4760 KB |
Output is correct |
85 |
Correct |
10 ms |
4760 KB |
Output is correct |
86 |
Correct |
9 ms |
4760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
3320 KB |
Output is correct |
2 |
Correct |
4 ms |
3320 KB |
Output is correct |
3 |
Correct |
4 ms |
3380 KB |
Output is correct |
4 |
Correct |
5 ms |
3424 KB |
Output is correct |
5 |
Correct |
5 ms |
3476 KB |
Output is correct |
6 |
Correct |
6 ms |
3524 KB |
Output is correct |
7 |
Correct |
6 ms |
3544 KB |
Output is correct |
8 |
Correct |
5 ms |
3552 KB |
Output is correct |
9 |
Correct |
4 ms |
3552 KB |
Output is correct |
10 |
Correct |
5 ms |
3576 KB |
Output is correct |
11 |
Correct |
6 ms |
3576 KB |
Output is correct |
12 |
Correct |
5 ms |
3576 KB |
Output is correct |
13 |
Correct |
5 ms |
3660 KB |
Output is correct |
14 |
Correct |
5 ms |
3660 KB |
Output is correct |
15 |
Correct |
7 ms |
3660 KB |
Output is correct |
16 |
Correct |
6 ms |
3660 KB |
Output is correct |
17 |
Correct |
6 ms |
3660 KB |
Output is correct |
18 |
Correct |
7 ms |
3660 KB |
Output is correct |
19 |
Correct |
6 ms |
3660 KB |
Output is correct |
20 |
Correct |
6 ms |
3660 KB |
Output is correct |
21 |
Correct |
7 ms |
3704 KB |
Output is correct |
22 |
Correct |
5 ms |
3704 KB |
Output is correct |
23 |
Correct |
5 ms |
3704 KB |
Output is correct |
24 |
Correct |
6 ms |
3704 KB |
Output is correct |
25 |
Correct |
7 ms |
3704 KB |
Output is correct |
26 |
Correct |
5 ms |
3704 KB |
Output is correct |
27 |
Correct |
6 ms |
3704 KB |
Output is correct |
28 |
Correct |
6 ms |
3704 KB |
Output is correct |
29 |
Correct |
5 ms |
3704 KB |
Output is correct |
30 |
Correct |
7 ms |
3704 KB |
Output is correct |
31 |
Correct |
6 ms |
3704 KB |
Output is correct |
32 |
Correct |
5 ms |
3704 KB |
Output is correct |
33 |
Correct |
6 ms |
3704 KB |
Output is correct |
34 |
Correct |
5 ms |
3704 KB |
Output is correct |
35 |
Correct |
5 ms |
3704 KB |
Output is correct |
36 |
Correct |
5 ms |
3704 KB |
Output is correct |
37 |
Correct |
6 ms |
3704 KB |
Output is correct |
38 |
Correct |
5 ms |
3704 KB |
Output is correct |
39 |
Correct |
5 ms |
3704 KB |
Output is correct |
40 |
Correct |
5 ms |
3704 KB |
Output is correct |
41 |
Correct |
6 ms |
3704 KB |
Output is correct |
42 |
Correct |
7 ms |
3704 KB |
Output is correct |
43 |
Correct |
7 ms |
3704 KB |
Output is correct |
44 |
Correct |
7 ms |
3704 KB |
Output is correct |
45 |
Correct |
7 ms |
3704 KB |
Output is correct |
46 |
Correct |
13 ms |
4104 KB |
Output is correct |
47 |
Correct |
14 ms |
4104 KB |
Output is correct |
48 |
Correct |
17 ms |
4120 KB |
Output is correct |
49 |
Correct |
15 ms |
4380 KB |
Output is correct |
50 |
Correct |
12 ms |
4460 KB |
Output is correct |
51 |
Correct |
12 ms |
4480 KB |
Output is correct |
52 |
Correct |
11 ms |
4480 KB |
Output is correct |
53 |
Correct |
11 ms |
4496 KB |
Output is correct |
54 |
Correct |
13 ms |
4548 KB |
Output is correct |
55 |
Correct |
15 ms |
4576 KB |
Output is correct |
56 |
Correct |
15 ms |
4624 KB |
Output is correct |
57 |
Correct |
18 ms |
4624 KB |
Output is correct |
58 |
Correct |
20 ms |
4624 KB |
Output is correct |
59 |
Correct |
18 ms |
4624 KB |
Output is correct |
60 |
Correct |
17 ms |
4624 KB |
Output is correct |
61 |
Correct |
12 ms |
4652 KB |
Output is correct |
62 |
Correct |
18 ms |
4652 KB |
Output is correct |
63 |
Correct |
12 ms |
4652 KB |
Output is correct |
64 |
Correct |
25 ms |
4652 KB |
Output is correct |
65 |
Correct |
13 ms |
4652 KB |
Output is correct |
66 |
Correct |
14 ms |
4652 KB |
Output is correct |
67 |
Correct |
14 ms |
4652 KB |
Output is correct |
68 |
Correct |
14 ms |
4712 KB |
Output is correct |
69 |
Correct |
9 ms |
4712 KB |
Output is correct |
70 |
Correct |
10 ms |
4732 KB |
Output is correct |
71 |
Correct |
9 ms |
4760 KB |
Output is correct |
72 |
Correct |
14 ms |
4760 KB |
Output is correct |
73 |
Correct |
14 ms |
4760 KB |
Output is correct |
74 |
Correct |
9 ms |
4760 KB |
Output is correct |
75 |
Correct |
14 ms |
4760 KB |
Output is correct |
76 |
Correct |
8 ms |
4760 KB |
Output is correct |
77 |
Correct |
15 ms |
4760 KB |
Output is correct |
78 |
Correct |
10 ms |
4760 KB |
Output is correct |
79 |
Correct |
9 ms |
4760 KB |
Output is correct |
80 |
Correct |
9 ms |
4760 KB |
Output is correct |
81 |
Correct |
10 ms |
4760 KB |
Output is correct |
82 |
Correct |
10 ms |
4760 KB |
Output is correct |
83 |
Correct |
11 ms |
4760 KB |
Output is correct |
84 |
Correct |
15 ms |
4760 KB |
Output is correct |
85 |
Correct |
10 ms |
4760 KB |
Output is correct |
86 |
Correct |
9 ms |
4760 KB |
Output is correct |
87 |
Correct |
28 ms |
5548 KB |
Output is correct |
88 |
Correct |
109 ms |
8748 KB |
Output is correct |
89 |
Correct |
374 ms |
20564 KB |
Output is correct |
90 |
Correct |
353 ms |
22572 KB |
Output is correct |
91 |
Correct |
404 ms |
24848 KB |
Output is correct |
92 |
Correct |
213 ms |
32940 KB |
Output is correct |
93 |
Correct |
269 ms |
35052 KB |
Output is correct |
94 |
Correct |
252 ms |
37164 KB |
Output is correct |
95 |
Correct |
228 ms |
37164 KB |
Output is correct |
96 |
Correct |
219 ms |
38640 KB |
Output is correct |
97 |
Correct |
267 ms |
40916 KB |
Output is correct |
98 |
Correct |
217 ms |
42936 KB |
Output is correct |
99 |
Correct |
270 ms |
44352 KB |
Output is correct |
100 |
Correct |
744 ms |
44352 KB |
Output is correct |
101 |
Correct |
604 ms |
45132 KB |
Output is correct |
102 |
Correct |
650 ms |
47308 KB |
Output is correct |
103 |
Correct |
711 ms |
49336 KB |
Output is correct |
104 |
Correct |
332 ms |
54564 KB |
Output is correct |
105 |
Correct |
273 ms |
56688 KB |
Output is correct |
106 |
Correct |
376 ms |
58720 KB |
Output is correct |
107 |
Correct |
386 ms |
58720 KB |
Output is correct |
108 |
Correct |
416 ms |
58720 KB |
Output is correct |
109 |
Correct |
416 ms |
60188 KB |
Output is correct |
110 |
Correct |
246 ms |
67736 KB |
Output is correct |
111 |
Correct |
314 ms |
67736 KB |
Output is correct |
112 |
Correct |
306 ms |
68832 KB |
Output is correct |
113 |
Correct |
312 ms |
69416 KB |
Output is correct |
114 |
Correct |
766 ms |
69416 KB |
Output is correct |
115 |
Correct |
678 ms |
69748 KB |
Output is correct |
116 |
Correct |
337 ms |
74188 KB |
Output is correct |
117 |
Correct |
268 ms |
74360 KB |
Output is correct |
118 |
Correct |
227 ms |
75760 KB |
Output is correct |
119 |
Correct |
217 ms |
77348 KB |
Output is correct |
120 |
Correct |
209 ms |
79508 KB |
Output is correct |
121 |
Correct |
218 ms |
80760 KB |
Output is correct |
122 |
Correct |
201 ms |
81836 KB |
Output is correct |
123 |
Correct |
365 ms |
84580 KB |
Output is correct |
124 |
Correct |
312 ms |
85912 KB |
Output is correct |
125 |
Correct |
356 ms |
87520 KB |
Output is correct |
126 |
Correct |
286 ms |
89904 KB |
Output is correct |
127 |
Correct |
244 ms |
90872 KB |
Output is correct |
128 |
Correct |
433 ms |
91964 KB |
Output is correct |