#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;
while (s <= e) {
if (s % 2 == 1)res = max(res, tree[s++]);
if (e % 2 == 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);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3192 KB |
Output is correct |
2 |
Correct |
5 ms |
3300 KB |
Output is correct |
3 |
Correct |
6 ms |
3376 KB |
Output is correct |
4 |
Correct |
10 ms |
3580 KB |
Output is correct |
5 |
Correct |
6 ms |
3580 KB |
Output is correct |
6 |
Correct |
7 ms |
3580 KB |
Output is correct |
7 |
Correct |
6 ms |
3580 KB |
Output is correct |
8 |
Correct |
5 ms |
3580 KB |
Output is correct |
9 |
Correct |
6 ms |
3580 KB |
Output is correct |
10 |
Correct |
5 ms |
3580 KB |
Output is correct |
11 |
Correct |
6 ms |
3580 KB |
Output is correct |
12 |
Correct |
6 ms |
3580 KB |
Output is correct |
13 |
Correct |
6 ms |
3580 KB |
Output is correct |
14 |
Correct |
6 ms |
3580 KB |
Output is correct |
15 |
Correct |
6 ms |
3580 KB |
Output is correct |
16 |
Correct |
6 ms |
3580 KB |
Output is correct |
17 |
Correct |
8 ms |
3664 KB |
Output is correct |
18 |
Correct |
8 ms |
3664 KB |
Output is correct |
19 |
Correct |
6 ms |
3664 KB |
Output is correct |
20 |
Correct |
5 ms |
3724 KB |
Output is correct |
21 |
Correct |
6 ms |
3724 KB |
Output is correct |
22 |
Correct |
6 ms |
3724 KB |
Output is correct |
23 |
Correct |
6 ms |
3724 KB |
Output is correct |
24 |
Correct |
7 ms |
3724 KB |
Output is correct |
25 |
Correct |
5 ms |
3724 KB |
Output is correct |
26 |
Correct |
7 ms |
3724 KB |
Output is correct |
27 |
Correct |
7 ms |
3724 KB |
Output is correct |
28 |
Correct |
7 ms |
3724 KB |
Output is correct |
29 |
Correct |
6 ms |
3724 KB |
Output is correct |
30 |
Correct |
7 ms |
3724 KB |
Output is correct |
31 |
Correct |
7 ms |
3724 KB |
Output is correct |
32 |
Correct |
6 ms |
3724 KB |
Output is correct |
33 |
Correct |
5 ms |
3724 KB |
Output is correct |
34 |
Correct |
5 ms |
3724 KB |
Output is correct |
35 |
Correct |
5 ms |
3724 KB |
Output is correct |
36 |
Correct |
5 ms |
3724 KB |
Output is correct |
37 |
Correct |
6 ms |
3724 KB |
Output is correct |
38 |
Correct |
6 ms |
3724 KB |
Output is correct |
39 |
Correct |
6 ms |
3724 KB |
Output is correct |
40 |
Correct |
6 ms |
3724 KB |
Output is correct |
41 |
Correct |
7 ms |
3724 KB |
Output is correct |
42 |
Correct |
7 ms |
3724 KB |
Output is correct |
43 |
Correct |
5 ms |
3724 KB |
Output is correct |
44 |
Correct |
6 ms |
3724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3192 KB |
Output is correct |
2 |
Correct |
5 ms |
3300 KB |
Output is correct |
3 |
Correct |
6 ms |
3376 KB |
Output is correct |
4 |
Correct |
10 ms |
3580 KB |
Output is correct |
5 |
Correct |
6 ms |
3580 KB |
Output is correct |
6 |
Correct |
7 ms |
3580 KB |
Output is correct |
7 |
Correct |
6 ms |
3580 KB |
Output is correct |
8 |
Correct |
5 ms |
3580 KB |
Output is correct |
9 |
Correct |
6 ms |
3580 KB |
Output is correct |
10 |
Correct |
5 ms |
3580 KB |
Output is correct |
11 |
Correct |
6 ms |
3580 KB |
Output is correct |
12 |
Correct |
6 ms |
3580 KB |
Output is correct |
13 |
Correct |
6 ms |
3580 KB |
Output is correct |
14 |
Correct |
6 ms |
3580 KB |
Output is correct |
15 |
Correct |
6 ms |
3580 KB |
Output is correct |
16 |
Correct |
6 ms |
3580 KB |
Output is correct |
17 |
Correct |
8 ms |
3664 KB |
Output is correct |
18 |
Correct |
8 ms |
3664 KB |
Output is correct |
19 |
Correct |
6 ms |
3664 KB |
Output is correct |
20 |
Correct |
5 ms |
3724 KB |
Output is correct |
21 |
Correct |
6 ms |
3724 KB |
Output is correct |
22 |
Correct |
6 ms |
3724 KB |
Output is correct |
23 |
Correct |
6 ms |
3724 KB |
Output is correct |
24 |
Correct |
7 ms |
3724 KB |
Output is correct |
25 |
Correct |
5 ms |
3724 KB |
Output is correct |
26 |
Correct |
7 ms |
3724 KB |
Output is correct |
27 |
Correct |
7 ms |
3724 KB |
Output is correct |
28 |
Correct |
7 ms |
3724 KB |
Output is correct |
29 |
Correct |
6 ms |
3724 KB |
Output is correct |
30 |
Correct |
7 ms |
3724 KB |
Output is correct |
31 |
Correct |
7 ms |
3724 KB |
Output is correct |
32 |
Correct |
6 ms |
3724 KB |
Output is correct |
33 |
Correct |
5 ms |
3724 KB |
Output is correct |
34 |
Correct |
5 ms |
3724 KB |
Output is correct |
35 |
Correct |
5 ms |
3724 KB |
Output is correct |
36 |
Correct |
5 ms |
3724 KB |
Output is correct |
37 |
Correct |
6 ms |
3724 KB |
Output is correct |
38 |
Correct |
6 ms |
3724 KB |
Output is correct |
39 |
Correct |
6 ms |
3724 KB |
Output is correct |
40 |
Correct |
6 ms |
3724 KB |
Output is correct |
41 |
Correct |
7 ms |
3724 KB |
Output is correct |
42 |
Correct |
7 ms |
3724 KB |
Output is correct |
43 |
Correct |
5 ms |
3724 KB |
Output is correct |
44 |
Correct |
6 ms |
3724 KB |
Output is correct |
45 |
Correct |
6 ms |
3724 KB |
Output is correct |
46 |
Correct |
11 ms |
4092 KB |
Output is correct |
47 |
Correct |
17 ms |
4092 KB |
Output is correct |
48 |
Correct |
13 ms |
4108 KB |
Output is correct |
49 |
Correct |
12 ms |
4492 KB |
Output is correct |
50 |
Correct |
13 ms |
4492 KB |
Output is correct |
51 |
Correct |
13 ms |
4524 KB |
Output is correct |
52 |
Correct |
14 ms |
4524 KB |
Output is correct |
53 |
Correct |
9 ms |
4524 KB |
Output is correct |
54 |
Correct |
10 ms |
4524 KB |
Output is correct |
55 |
Correct |
12 ms |
4524 KB |
Output is correct |
56 |
Correct |
9 ms |
4524 KB |
Output is correct |
57 |
Correct |
14 ms |
4524 KB |
Output is correct |
58 |
Correct |
24 ms |
4524 KB |
Output is correct |
59 |
Correct |
26 ms |
4524 KB |
Output is correct |
60 |
Correct |
22 ms |
4524 KB |
Output is correct |
61 |
Correct |
11 ms |
4524 KB |
Output is correct |
62 |
Correct |
10 ms |
4524 KB |
Output is correct |
63 |
Correct |
11 ms |
4524 KB |
Output is correct |
64 |
Correct |
11 ms |
4524 KB |
Output is correct |
65 |
Correct |
13 ms |
4524 KB |
Output is correct |
66 |
Correct |
15 ms |
4524 KB |
Output is correct |
67 |
Correct |
13 ms |
4524 KB |
Output is correct |
68 |
Correct |
8 ms |
4540 KB |
Output is correct |
69 |
Correct |
10 ms |
4540 KB |
Output is correct |
70 |
Correct |
11 ms |
4540 KB |
Output is correct |
71 |
Correct |
14 ms |
4540 KB |
Output is correct |
72 |
Correct |
17 ms |
4540 KB |
Output is correct |
73 |
Correct |
18 ms |
4540 KB |
Output is correct |
74 |
Correct |
11 ms |
4540 KB |
Output is correct |
75 |
Correct |
11 ms |
4540 KB |
Output is correct |
76 |
Correct |
11 ms |
4540 KB |
Output is correct |
77 |
Correct |
11 ms |
4540 KB |
Output is correct |
78 |
Correct |
10 ms |
4540 KB |
Output is correct |
79 |
Correct |
10 ms |
4540 KB |
Output is correct |
80 |
Correct |
13 ms |
4540 KB |
Output is correct |
81 |
Correct |
9 ms |
4540 KB |
Output is correct |
82 |
Correct |
14 ms |
4540 KB |
Output is correct |
83 |
Correct |
11 ms |
4540 KB |
Output is correct |
84 |
Correct |
11 ms |
4540 KB |
Output is correct |
85 |
Correct |
12 ms |
4540 KB |
Output is correct |
86 |
Correct |
10 ms |
4540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3192 KB |
Output is correct |
2 |
Correct |
5 ms |
3300 KB |
Output is correct |
3 |
Correct |
6 ms |
3376 KB |
Output is correct |
4 |
Correct |
10 ms |
3580 KB |
Output is correct |
5 |
Correct |
6 ms |
3580 KB |
Output is correct |
6 |
Correct |
7 ms |
3580 KB |
Output is correct |
7 |
Correct |
6 ms |
3580 KB |
Output is correct |
8 |
Correct |
5 ms |
3580 KB |
Output is correct |
9 |
Correct |
6 ms |
3580 KB |
Output is correct |
10 |
Correct |
5 ms |
3580 KB |
Output is correct |
11 |
Correct |
6 ms |
3580 KB |
Output is correct |
12 |
Correct |
6 ms |
3580 KB |
Output is correct |
13 |
Correct |
6 ms |
3580 KB |
Output is correct |
14 |
Correct |
6 ms |
3580 KB |
Output is correct |
15 |
Correct |
6 ms |
3580 KB |
Output is correct |
16 |
Correct |
6 ms |
3580 KB |
Output is correct |
17 |
Correct |
8 ms |
3664 KB |
Output is correct |
18 |
Correct |
8 ms |
3664 KB |
Output is correct |
19 |
Correct |
6 ms |
3664 KB |
Output is correct |
20 |
Correct |
5 ms |
3724 KB |
Output is correct |
21 |
Correct |
6 ms |
3724 KB |
Output is correct |
22 |
Correct |
6 ms |
3724 KB |
Output is correct |
23 |
Correct |
6 ms |
3724 KB |
Output is correct |
24 |
Correct |
7 ms |
3724 KB |
Output is correct |
25 |
Correct |
5 ms |
3724 KB |
Output is correct |
26 |
Correct |
7 ms |
3724 KB |
Output is correct |
27 |
Correct |
7 ms |
3724 KB |
Output is correct |
28 |
Correct |
7 ms |
3724 KB |
Output is correct |
29 |
Correct |
6 ms |
3724 KB |
Output is correct |
30 |
Correct |
7 ms |
3724 KB |
Output is correct |
31 |
Correct |
7 ms |
3724 KB |
Output is correct |
32 |
Correct |
6 ms |
3724 KB |
Output is correct |
33 |
Correct |
5 ms |
3724 KB |
Output is correct |
34 |
Correct |
5 ms |
3724 KB |
Output is correct |
35 |
Correct |
5 ms |
3724 KB |
Output is correct |
36 |
Correct |
5 ms |
3724 KB |
Output is correct |
37 |
Correct |
6 ms |
3724 KB |
Output is correct |
38 |
Correct |
6 ms |
3724 KB |
Output is correct |
39 |
Correct |
6 ms |
3724 KB |
Output is correct |
40 |
Correct |
6 ms |
3724 KB |
Output is correct |
41 |
Correct |
7 ms |
3724 KB |
Output is correct |
42 |
Correct |
7 ms |
3724 KB |
Output is correct |
43 |
Correct |
5 ms |
3724 KB |
Output is correct |
44 |
Correct |
6 ms |
3724 KB |
Output is correct |
45 |
Correct |
6 ms |
3724 KB |
Output is correct |
46 |
Correct |
11 ms |
4092 KB |
Output is correct |
47 |
Correct |
17 ms |
4092 KB |
Output is correct |
48 |
Correct |
13 ms |
4108 KB |
Output is correct |
49 |
Correct |
12 ms |
4492 KB |
Output is correct |
50 |
Correct |
13 ms |
4492 KB |
Output is correct |
51 |
Correct |
13 ms |
4524 KB |
Output is correct |
52 |
Correct |
14 ms |
4524 KB |
Output is correct |
53 |
Correct |
9 ms |
4524 KB |
Output is correct |
54 |
Correct |
10 ms |
4524 KB |
Output is correct |
55 |
Correct |
12 ms |
4524 KB |
Output is correct |
56 |
Correct |
9 ms |
4524 KB |
Output is correct |
57 |
Correct |
14 ms |
4524 KB |
Output is correct |
58 |
Correct |
24 ms |
4524 KB |
Output is correct |
59 |
Correct |
26 ms |
4524 KB |
Output is correct |
60 |
Correct |
22 ms |
4524 KB |
Output is correct |
61 |
Correct |
11 ms |
4524 KB |
Output is correct |
62 |
Correct |
10 ms |
4524 KB |
Output is correct |
63 |
Correct |
11 ms |
4524 KB |
Output is correct |
64 |
Correct |
11 ms |
4524 KB |
Output is correct |
65 |
Correct |
13 ms |
4524 KB |
Output is correct |
66 |
Correct |
15 ms |
4524 KB |
Output is correct |
67 |
Correct |
13 ms |
4524 KB |
Output is correct |
68 |
Correct |
8 ms |
4540 KB |
Output is correct |
69 |
Correct |
10 ms |
4540 KB |
Output is correct |
70 |
Correct |
11 ms |
4540 KB |
Output is correct |
71 |
Correct |
14 ms |
4540 KB |
Output is correct |
72 |
Correct |
17 ms |
4540 KB |
Output is correct |
73 |
Correct |
18 ms |
4540 KB |
Output is correct |
74 |
Correct |
11 ms |
4540 KB |
Output is correct |
75 |
Correct |
11 ms |
4540 KB |
Output is correct |
76 |
Correct |
11 ms |
4540 KB |
Output is correct |
77 |
Correct |
11 ms |
4540 KB |
Output is correct |
78 |
Correct |
10 ms |
4540 KB |
Output is correct |
79 |
Correct |
10 ms |
4540 KB |
Output is correct |
80 |
Correct |
13 ms |
4540 KB |
Output is correct |
81 |
Correct |
9 ms |
4540 KB |
Output is correct |
82 |
Correct |
14 ms |
4540 KB |
Output is correct |
83 |
Correct |
11 ms |
4540 KB |
Output is correct |
84 |
Correct |
11 ms |
4540 KB |
Output is correct |
85 |
Correct |
12 ms |
4540 KB |
Output is correct |
86 |
Correct |
10 ms |
4540 KB |
Output is correct |
87 |
Correct |
26 ms |
5252 KB |
Output is correct |
88 |
Correct |
105 ms |
8580 KB |
Output is correct |
89 |
Execution timed out |
2053 ms |
263168 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |