#include<stdio.h>
#include<vector>
using namespace std;
int par[121212][20], num[121212], S[121212], E[121212], cnt, tn, depth[121212], A[121212], B[121212], C[121212], CSD[121212];
vector<int>edge[121212];
struct xy { int x, y; }tree[313131], temp[121212];
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>>=1; e>>=1;
}
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 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;
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, 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, i, j; scanf("%d", &n);
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:22: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:47:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int n, i, j; scanf("%d", &n);
~~~~~^~~~~~~~~~
construction.cpp:50: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:53: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);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3192 KB |
Output is correct |
2 |
Correct |
3 ms |
3304 KB |
Output is correct |
3 |
Correct |
5 ms |
3304 KB |
Output is correct |
4 |
Correct |
4 ms |
3304 KB |
Output is correct |
5 |
Correct |
5 ms |
3412 KB |
Output is correct |
6 |
Correct |
4 ms |
3420 KB |
Output is correct |
7 |
Correct |
7 ms |
3624 KB |
Output is correct |
8 |
Correct |
4 ms |
3624 KB |
Output is correct |
9 |
Correct |
4 ms |
3624 KB |
Output is correct |
10 |
Correct |
7 ms |
3624 KB |
Output is correct |
11 |
Correct |
4 ms |
3624 KB |
Output is correct |
12 |
Correct |
5 ms |
3624 KB |
Output is correct |
13 |
Correct |
4 ms |
3624 KB |
Output is correct |
14 |
Correct |
4 ms |
3624 KB |
Output is correct |
15 |
Correct |
5 ms |
3680 KB |
Output is correct |
16 |
Correct |
5 ms |
3680 KB |
Output is correct |
17 |
Correct |
6 ms |
3680 KB |
Output is correct |
18 |
Correct |
5 ms |
3680 KB |
Output is correct |
19 |
Correct |
4 ms |
3680 KB |
Output is correct |
20 |
Correct |
5 ms |
3680 KB |
Output is correct |
21 |
Correct |
4 ms |
3680 KB |
Output is correct |
22 |
Correct |
4 ms |
3680 KB |
Output is correct |
23 |
Correct |
5 ms |
3680 KB |
Output is correct |
24 |
Correct |
6 ms |
3680 KB |
Output is correct |
25 |
Correct |
7 ms |
3680 KB |
Output is correct |
26 |
Correct |
5 ms |
3680 KB |
Output is correct |
27 |
Correct |
5 ms |
3680 KB |
Output is correct |
28 |
Correct |
6 ms |
3680 KB |
Output is correct |
29 |
Correct |
5 ms |
3680 KB |
Output is correct |
30 |
Correct |
6 ms |
3680 KB |
Output is correct |
31 |
Correct |
7 ms |
3680 KB |
Output is correct |
32 |
Correct |
5 ms |
3680 KB |
Output is correct |
33 |
Correct |
5 ms |
3680 KB |
Output is correct |
34 |
Correct |
6 ms |
3680 KB |
Output is correct |
35 |
Correct |
4 ms |
3680 KB |
Output is correct |
36 |
Correct |
6 ms |
3680 KB |
Output is correct |
37 |
Correct |
5 ms |
3680 KB |
Output is correct |
38 |
Correct |
5 ms |
3680 KB |
Output is correct |
39 |
Correct |
4 ms |
3680 KB |
Output is correct |
40 |
Correct |
6 ms |
3680 KB |
Output is correct |
41 |
Correct |
6 ms |
3696 KB |
Output is correct |
42 |
Correct |
6 ms |
3696 KB |
Output is correct |
43 |
Correct |
5 ms |
3696 KB |
Output is correct |
44 |
Correct |
6 ms |
3696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3192 KB |
Output is correct |
2 |
Correct |
3 ms |
3304 KB |
Output is correct |
3 |
Correct |
5 ms |
3304 KB |
Output is correct |
4 |
Correct |
4 ms |
3304 KB |
Output is correct |
5 |
Correct |
5 ms |
3412 KB |
Output is correct |
6 |
Correct |
4 ms |
3420 KB |
Output is correct |
7 |
Correct |
7 ms |
3624 KB |
Output is correct |
8 |
Correct |
4 ms |
3624 KB |
Output is correct |
9 |
Correct |
4 ms |
3624 KB |
Output is correct |
10 |
Correct |
7 ms |
3624 KB |
Output is correct |
11 |
Correct |
4 ms |
3624 KB |
Output is correct |
12 |
Correct |
5 ms |
3624 KB |
Output is correct |
13 |
Correct |
4 ms |
3624 KB |
Output is correct |
14 |
Correct |
4 ms |
3624 KB |
Output is correct |
15 |
Correct |
5 ms |
3680 KB |
Output is correct |
16 |
Correct |
5 ms |
3680 KB |
Output is correct |
17 |
Correct |
6 ms |
3680 KB |
Output is correct |
18 |
Correct |
5 ms |
3680 KB |
Output is correct |
19 |
Correct |
4 ms |
3680 KB |
Output is correct |
20 |
Correct |
5 ms |
3680 KB |
Output is correct |
21 |
Correct |
4 ms |
3680 KB |
Output is correct |
22 |
Correct |
4 ms |
3680 KB |
Output is correct |
23 |
Correct |
5 ms |
3680 KB |
Output is correct |
24 |
Correct |
6 ms |
3680 KB |
Output is correct |
25 |
Correct |
7 ms |
3680 KB |
Output is correct |
26 |
Correct |
5 ms |
3680 KB |
Output is correct |
27 |
Correct |
5 ms |
3680 KB |
Output is correct |
28 |
Correct |
6 ms |
3680 KB |
Output is correct |
29 |
Correct |
5 ms |
3680 KB |
Output is correct |
30 |
Correct |
6 ms |
3680 KB |
Output is correct |
31 |
Correct |
7 ms |
3680 KB |
Output is correct |
32 |
Correct |
5 ms |
3680 KB |
Output is correct |
33 |
Correct |
5 ms |
3680 KB |
Output is correct |
34 |
Correct |
6 ms |
3680 KB |
Output is correct |
35 |
Correct |
4 ms |
3680 KB |
Output is correct |
36 |
Correct |
6 ms |
3680 KB |
Output is correct |
37 |
Correct |
5 ms |
3680 KB |
Output is correct |
38 |
Correct |
5 ms |
3680 KB |
Output is correct |
39 |
Correct |
4 ms |
3680 KB |
Output is correct |
40 |
Correct |
6 ms |
3680 KB |
Output is correct |
41 |
Correct |
6 ms |
3696 KB |
Output is correct |
42 |
Correct |
6 ms |
3696 KB |
Output is correct |
43 |
Correct |
5 ms |
3696 KB |
Output is correct |
44 |
Correct |
6 ms |
3696 KB |
Output is correct |
45 |
Correct |
8 ms |
3696 KB |
Output is correct |
46 |
Correct |
10 ms |
4128 KB |
Output is correct |
47 |
Correct |
12 ms |
4128 KB |
Output is correct |
48 |
Correct |
15 ms |
4128 KB |
Output is correct |
49 |
Correct |
11 ms |
4380 KB |
Output is correct |
50 |
Correct |
10 ms |
4400 KB |
Output is correct |
51 |
Correct |
14 ms |
4400 KB |
Output is correct |
52 |
Correct |
8 ms |
4400 KB |
Output is correct |
53 |
Correct |
9 ms |
4400 KB |
Output is correct |
54 |
Correct |
13 ms |
4400 KB |
Output is correct |
55 |
Correct |
10 ms |
4400 KB |
Output is correct |
56 |
Correct |
12 ms |
4400 KB |
Output is correct |
57 |
Correct |
15 ms |
4400 KB |
Output is correct |
58 |
Correct |
20 ms |
4400 KB |
Output is correct |
59 |
Correct |
18 ms |
4400 KB |
Output is correct |
60 |
Correct |
19 ms |
4400 KB |
Output is correct |
61 |
Correct |
14 ms |
4400 KB |
Output is correct |
62 |
Correct |
11 ms |
4400 KB |
Output is correct |
63 |
Correct |
11 ms |
4400 KB |
Output is correct |
64 |
Correct |
13 ms |
4400 KB |
Output is correct |
65 |
Correct |
10 ms |
4400 KB |
Output is correct |
66 |
Correct |
10 ms |
4400 KB |
Output is correct |
67 |
Correct |
10 ms |
4400 KB |
Output is correct |
68 |
Correct |
8 ms |
4412 KB |
Output is correct |
69 |
Correct |
16 ms |
4412 KB |
Output is correct |
70 |
Correct |
10 ms |
4412 KB |
Output is correct |
71 |
Correct |
8 ms |
4412 KB |
Output is correct |
72 |
Correct |
17 ms |
4412 KB |
Output is correct |
73 |
Correct |
17 ms |
4412 KB |
Output is correct |
74 |
Correct |
12 ms |
4412 KB |
Output is correct |
75 |
Correct |
14 ms |
4412 KB |
Output is correct |
76 |
Correct |
11 ms |
4412 KB |
Output is correct |
77 |
Correct |
12 ms |
4412 KB |
Output is correct |
78 |
Correct |
11 ms |
4412 KB |
Output is correct |
79 |
Correct |
9 ms |
4412 KB |
Output is correct |
80 |
Correct |
9 ms |
4412 KB |
Output is correct |
81 |
Correct |
12 ms |
4412 KB |
Output is correct |
82 |
Correct |
11 ms |
4412 KB |
Output is correct |
83 |
Correct |
11 ms |
4412 KB |
Output is correct |
84 |
Correct |
11 ms |
4412 KB |
Output is correct |
85 |
Correct |
12 ms |
4412 KB |
Output is correct |
86 |
Correct |
11 ms |
4412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3192 KB |
Output is correct |
2 |
Correct |
3 ms |
3304 KB |
Output is correct |
3 |
Correct |
5 ms |
3304 KB |
Output is correct |
4 |
Correct |
4 ms |
3304 KB |
Output is correct |
5 |
Correct |
5 ms |
3412 KB |
Output is correct |
6 |
Correct |
4 ms |
3420 KB |
Output is correct |
7 |
Correct |
7 ms |
3624 KB |
Output is correct |
8 |
Correct |
4 ms |
3624 KB |
Output is correct |
9 |
Correct |
4 ms |
3624 KB |
Output is correct |
10 |
Correct |
7 ms |
3624 KB |
Output is correct |
11 |
Correct |
4 ms |
3624 KB |
Output is correct |
12 |
Correct |
5 ms |
3624 KB |
Output is correct |
13 |
Correct |
4 ms |
3624 KB |
Output is correct |
14 |
Correct |
4 ms |
3624 KB |
Output is correct |
15 |
Correct |
5 ms |
3680 KB |
Output is correct |
16 |
Correct |
5 ms |
3680 KB |
Output is correct |
17 |
Correct |
6 ms |
3680 KB |
Output is correct |
18 |
Correct |
5 ms |
3680 KB |
Output is correct |
19 |
Correct |
4 ms |
3680 KB |
Output is correct |
20 |
Correct |
5 ms |
3680 KB |
Output is correct |
21 |
Correct |
4 ms |
3680 KB |
Output is correct |
22 |
Correct |
4 ms |
3680 KB |
Output is correct |
23 |
Correct |
5 ms |
3680 KB |
Output is correct |
24 |
Correct |
6 ms |
3680 KB |
Output is correct |
25 |
Correct |
7 ms |
3680 KB |
Output is correct |
26 |
Correct |
5 ms |
3680 KB |
Output is correct |
27 |
Correct |
5 ms |
3680 KB |
Output is correct |
28 |
Correct |
6 ms |
3680 KB |
Output is correct |
29 |
Correct |
5 ms |
3680 KB |
Output is correct |
30 |
Correct |
6 ms |
3680 KB |
Output is correct |
31 |
Correct |
7 ms |
3680 KB |
Output is correct |
32 |
Correct |
5 ms |
3680 KB |
Output is correct |
33 |
Correct |
5 ms |
3680 KB |
Output is correct |
34 |
Correct |
6 ms |
3680 KB |
Output is correct |
35 |
Correct |
4 ms |
3680 KB |
Output is correct |
36 |
Correct |
6 ms |
3680 KB |
Output is correct |
37 |
Correct |
5 ms |
3680 KB |
Output is correct |
38 |
Correct |
5 ms |
3680 KB |
Output is correct |
39 |
Correct |
4 ms |
3680 KB |
Output is correct |
40 |
Correct |
6 ms |
3680 KB |
Output is correct |
41 |
Correct |
6 ms |
3696 KB |
Output is correct |
42 |
Correct |
6 ms |
3696 KB |
Output is correct |
43 |
Correct |
5 ms |
3696 KB |
Output is correct |
44 |
Correct |
6 ms |
3696 KB |
Output is correct |
45 |
Correct |
8 ms |
3696 KB |
Output is correct |
46 |
Correct |
10 ms |
4128 KB |
Output is correct |
47 |
Correct |
12 ms |
4128 KB |
Output is correct |
48 |
Correct |
15 ms |
4128 KB |
Output is correct |
49 |
Correct |
11 ms |
4380 KB |
Output is correct |
50 |
Correct |
10 ms |
4400 KB |
Output is correct |
51 |
Correct |
14 ms |
4400 KB |
Output is correct |
52 |
Correct |
8 ms |
4400 KB |
Output is correct |
53 |
Correct |
9 ms |
4400 KB |
Output is correct |
54 |
Correct |
13 ms |
4400 KB |
Output is correct |
55 |
Correct |
10 ms |
4400 KB |
Output is correct |
56 |
Correct |
12 ms |
4400 KB |
Output is correct |
57 |
Correct |
15 ms |
4400 KB |
Output is correct |
58 |
Correct |
20 ms |
4400 KB |
Output is correct |
59 |
Correct |
18 ms |
4400 KB |
Output is correct |
60 |
Correct |
19 ms |
4400 KB |
Output is correct |
61 |
Correct |
14 ms |
4400 KB |
Output is correct |
62 |
Correct |
11 ms |
4400 KB |
Output is correct |
63 |
Correct |
11 ms |
4400 KB |
Output is correct |
64 |
Correct |
13 ms |
4400 KB |
Output is correct |
65 |
Correct |
10 ms |
4400 KB |
Output is correct |
66 |
Correct |
10 ms |
4400 KB |
Output is correct |
67 |
Correct |
10 ms |
4400 KB |
Output is correct |
68 |
Correct |
8 ms |
4412 KB |
Output is correct |
69 |
Correct |
16 ms |
4412 KB |
Output is correct |
70 |
Correct |
10 ms |
4412 KB |
Output is correct |
71 |
Correct |
8 ms |
4412 KB |
Output is correct |
72 |
Correct |
17 ms |
4412 KB |
Output is correct |
73 |
Correct |
17 ms |
4412 KB |
Output is correct |
74 |
Correct |
12 ms |
4412 KB |
Output is correct |
75 |
Correct |
14 ms |
4412 KB |
Output is correct |
76 |
Correct |
11 ms |
4412 KB |
Output is correct |
77 |
Correct |
12 ms |
4412 KB |
Output is correct |
78 |
Correct |
11 ms |
4412 KB |
Output is correct |
79 |
Correct |
9 ms |
4412 KB |
Output is correct |
80 |
Correct |
9 ms |
4412 KB |
Output is correct |
81 |
Correct |
12 ms |
4412 KB |
Output is correct |
82 |
Correct |
11 ms |
4412 KB |
Output is correct |
83 |
Correct |
11 ms |
4412 KB |
Output is correct |
84 |
Correct |
11 ms |
4412 KB |
Output is correct |
85 |
Correct |
12 ms |
4412 KB |
Output is correct |
86 |
Correct |
11 ms |
4412 KB |
Output is correct |
87 |
Correct |
30 ms |
5180 KB |
Output is correct |
88 |
Correct |
92 ms |
8508 KB |
Output is correct |
89 |
Correct |
409 ms |
20156 KB |
Output is correct |
90 |
Correct |
384 ms |
20156 KB |
Output is correct |
91 |
Correct |
374 ms |
20156 KB |
Output is correct |
92 |
Correct |
211 ms |
26172 KB |
Output is correct |
93 |
Correct |
214 ms |
26172 KB |
Output is correct |
94 |
Correct |
251 ms |
26172 KB |
Output is correct |
95 |
Correct |
257 ms |
26172 KB |
Output is correct |
96 |
Correct |
315 ms |
26172 KB |
Output is correct |
97 |
Correct |
255 ms |
26172 KB |
Output is correct |
98 |
Correct |
285 ms |
26172 KB |
Output is correct |
99 |
Correct |
246 ms |
26172 KB |
Output is correct |
100 |
Correct |
742 ms |
26172 KB |
Output is correct |
101 |
Correct |
656 ms |
26172 KB |
Output is correct |
102 |
Correct |
625 ms |
26172 KB |
Output is correct |
103 |
Correct |
660 ms |
26172 KB |
Output is correct |
104 |
Correct |
311 ms |
26172 KB |
Output is correct |
105 |
Correct |
369 ms |
26172 KB |
Output is correct |
106 |
Correct |
335 ms |
26172 KB |
Output is correct |
107 |
Correct |
349 ms |
26172 KB |
Output is correct |
108 |
Correct |
351 ms |
26172 KB |
Output is correct |
109 |
Correct |
448 ms |
26172 KB |
Output is correct |
110 |
Correct |
215 ms |
26172 KB |
Output is correct |
111 |
Correct |
225 ms |
26172 KB |
Output is correct |
112 |
Correct |
224 ms |
26172 KB |
Output is correct |
113 |
Correct |
308 ms |
26172 KB |
Output is correct |
114 |
Correct |
650 ms |
26172 KB |
Output is correct |
115 |
Correct |
737 ms |
26172 KB |
Output is correct |
116 |
Correct |
306 ms |
26172 KB |
Output is correct |
117 |
Correct |
231 ms |
26172 KB |
Output is correct |
118 |
Correct |
268 ms |
26172 KB |
Output is correct |
119 |
Correct |
204 ms |
26172 KB |
Output is correct |
120 |
Correct |
215 ms |
26172 KB |
Output is correct |
121 |
Correct |
190 ms |
26172 KB |
Output is correct |
122 |
Correct |
191 ms |
26172 KB |
Output is correct |
123 |
Correct |
285 ms |
26172 KB |
Output is correct |
124 |
Correct |
340 ms |
26172 KB |
Output is correct |
125 |
Correct |
320 ms |
26172 KB |
Output is correct |
126 |
Correct |
303 ms |
26172 KB |
Output is correct |
127 |
Correct |
301 ms |
26172 KB |
Output is correct |
128 |
Correct |
298 ms |
26172 KB |
Output is correct |