#include <bits/stdc++.h>
using namespace std;
#define MAXN 100005
#define fi first
#define se second
#define rb(x) ((x) & (-(x)))
typedef long long lint;
typedef pair<int, int> pii;
int C[MAXN], A[MAXN], B[MAXN];
vector<int> ed[MAXN];
int par[20][MAXN], dep[MAXN], dfsord[MAXN], dfsr[MAXN], dfsn;
int cs[MAXN], ch[MAXN];
int seg[4 * MAXN], bit[MAXN];
//int xxxx = 0;
int anc(int x, int d) {
for(int i = 0; i < 20; i++) if((dep[x] - d) & (1 << i)) x = par[i][x];
return x;
}
void dfs(int x) {
dfsord[x] = ++dfsn;
for(auto a : ed[x]) {
par[0][a] = x;
dep[a] = dep[x] + 1;
dfs(a);
}
dfsr[x] = dfsn;
}
void updseg(int idx, int l, int r, int x, int y) {
if(l == r) seg[idx] = y;
else {
int m = (l + r) / 2;
if(x <= m) updseg(idx * 2, l, m, x, y);
else updseg(idx * 2 + 1, m + 1, r, x, y);
seg[idx] = max(seg[idx * 2], seg[idx * 2 + 1]);
}
}
int gseg(int idx, int l, int r, int x, int y) {
//if(idx == 1 && ++xxxx < 100) printf("gseg(x = %d, y = %d)\n", x, y);
if(x <= l && r <= y) return seg[idx];
if(r < x || y < l) return -1;
int m = (l + r) / 2;
return max(gseg(idx * 2, l, m, x, y), gseg(idx * 2 + 1, m + 1, r, x, y));
}
void updbit(int x, int y) { for(; x < MAXN; x += rb(x)) bit[x] += y; }
int gbit(int x) { int ans = 0; for(; x > 0; x -= rb(x)) ans += bit[x]; return ans; }
int main() {
int N;
scanf("%d", &N);
for(int i = 1; i <= N; i++) scanf("%d", C + i);
for(int i = 1; i < N; i++) scanf("%d%d", A + i, B + i);
for(int i = 1; i < N; i++) ed[A[i]].push_back(B[i]);
dep[1] = 1;
dfs(1);
for(int i = 1; i < 20; i++) for(int j = 1; j <= N; j++) par[i][j] = par[i - 1][par[i - 1][j]];
for(int i = 1; i <= N; i++) cs[i - 1] = C[i];
sort(cs, cs + N);
for(int i = 1; i <= N; i++) C[i] = lower_bound(cs, cs + N, C[i]) - cs + 1;
for(int i = 1; i < 4 * N; i++) seg[i] = -1;
updseg(1, 0, N, dfsord[1], 0);
ch[1] = 1;
B[0] = 1;
//printf("*\n");
for(int i = 1; i < N; i++) {
vector<pii> v;
for(int t = A[i]; t != 0;) {
//if(++xxxx < 100) printf("i = %d, t = %d\n", i, t);
int c = B[gseg(1, 0, N, dfsord[t], dfsr[t])];
//if(++xxxx < 100) printf("c = %d\n", c);
v.push_back(make_pair(c, dep[t] - ch[c] + 1));
int k = anc(t, ch[c] - 1);
ch[c] = dep[t] + 1;
t = k;
}
lint ans = 0ll;
//for(auto a : v) printf("a = (%d, %d)\n", a.fi, a.se);
for(auto a : v) {
ans += lint(a.se) * gbit(C[a.fi] - 1);
updbit(C[a.fi], a.se);
}
for(auto a : v) updbit(C[a.fi], -a.se);
ch[B[i]] = 1;
updseg(1, 0, N, dfsord[B[i]], i);
printf("%lld\n", ans);
}
return 0;
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:58:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
construction.cpp:59:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i = 1; i <= N; i++) scanf("%d", C + i);
~~~~~^~~~~~~~~~~~~
construction.cpp:60:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i = 1; i < N; i++) scanf("%d%d", A + i, B + i);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2808 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 |
2808 KB |
Output is correct |
5 |
Correct |
3 ms |
2936 KB |
Output is correct |
6 |
Correct |
5 ms |
2936 KB |
Output is correct |
7 |
Correct |
5 ms |
2936 KB |
Output is correct |
8 |
Correct |
5 ms |
3064 KB |
Output is correct |
9 |
Correct |
5 ms |
2936 KB |
Output is correct |
10 |
Correct |
5 ms |
2936 KB |
Output is correct |
11 |
Correct |
5 ms |
2936 KB |
Output is correct |
12 |
Correct |
5 ms |
2936 KB |
Output is correct |
13 |
Correct |
5 ms |
2936 KB |
Output is correct |
14 |
Correct |
5 ms |
2936 KB |
Output is correct |
15 |
Correct |
5 ms |
2808 KB |
Output is correct |
16 |
Correct |
5 ms |
2936 KB |
Output is correct |
17 |
Correct |
5 ms |
2936 KB |
Output is correct |
18 |
Correct |
5 ms |
2936 KB |
Output is correct |
19 |
Correct |
5 ms |
2936 KB |
Output is correct |
20 |
Correct |
5 ms |
2936 KB |
Output is correct |
21 |
Correct |
5 ms |
2936 KB |
Output is correct |
22 |
Correct |
5 ms |
2936 KB |
Output is correct |
23 |
Correct |
5 ms |
2936 KB |
Output is correct |
24 |
Correct |
5 ms |
2936 KB |
Output is correct |
25 |
Correct |
5 ms |
2936 KB |
Output is correct |
26 |
Correct |
5 ms |
2884 KB |
Output is correct |
27 |
Correct |
5 ms |
2936 KB |
Output is correct |
28 |
Correct |
5 ms |
2936 KB |
Output is correct |
29 |
Correct |
5 ms |
2936 KB |
Output is correct |
30 |
Correct |
5 ms |
2936 KB |
Output is correct |
31 |
Correct |
6 ms |
2936 KB |
Output is correct |
32 |
Correct |
5 ms |
2936 KB |
Output is correct |
33 |
Correct |
5 ms |
2936 KB |
Output is correct |
34 |
Correct |
5 ms |
2936 KB |
Output is correct |
35 |
Correct |
5 ms |
2936 KB |
Output is correct |
36 |
Correct |
5 ms |
2940 KB |
Output is correct |
37 |
Correct |
5 ms |
2936 KB |
Output is correct |
38 |
Correct |
5 ms |
2936 KB |
Output is correct |
39 |
Correct |
5 ms |
2936 KB |
Output is correct |
40 |
Correct |
5 ms |
2936 KB |
Output is correct |
41 |
Correct |
5 ms |
2936 KB |
Output is correct |
42 |
Correct |
5 ms |
2936 KB |
Output is correct |
43 |
Correct |
5 ms |
2936 KB |
Output is correct |
44 |
Correct |
5 ms |
2936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2808 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 |
2808 KB |
Output is correct |
5 |
Correct |
3 ms |
2936 KB |
Output is correct |
6 |
Correct |
5 ms |
2936 KB |
Output is correct |
7 |
Correct |
5 ms |
2936 KB |
Output is correct |
8 |
Correct |
5 ms |
3064 KB |
Output is correct |
9 |
Correct |
5 ms |
2936 KB |
Output is correct |
10 |
Correct |
5 ms |
2936 KB |
Output is correct |
11 |
Correct |
5 ms |
2936 KB |
Output is correct |
12 |
Correct |
5 ms |
2936 KB |
Output is correct |
13 |
Correct |
5 ms |
2936 KB |
Output is correct |
14 |
Correct |
5 ms |
2936 KB |
Output is correct |
15 |
Correct |
5 ms |
2808 KB |
Output is correct |
16 |
Correct |
5 ms |
2936 KB |
Output is correct |
17 |
Correct |
5 ms |
2936 KB |
Output is correct |
18 |
Correct |
5 ms |
2936 KB |
Output is correct |
19 |
Correct |
5 ms |
2936 KB |
Output is correct |
20 |
Correct |
5 ms |
2936 KB |
Output is correct |
21 |
Correct |
5 ms |
2936 KB |
Output is correct |
22 |
Correct |
5 ms |
2936 KB |
Output is correct |
23 |
Correct |
5 ms |
2936 KB |
Output is correct |
24 |
Correct |
5 ms |
2936 KB |
Output is correct |
25 |
Correct |
5 ms |
2936 KB |
Output is correct |
26 |
Correct |
5 ms |
2884 KB |
Output is correct |
27 |
Correct |
5 ms |
2936 KB |
Output is correct |
28 |
Correct |
5 ms |
2936 KB |
Output is correct |
29 |
Correct |
5 ms |
2936 KB |
Output is correct |
30 |
Correct |
5 ms |
2936 KB |
Output is correct |
31 |
Correct |
6 ms |
2936 KB |
Output is correct |
32 |
Correct |
5 ms |
2936 KB |
Output is correct |
33 |
Correct |
5 ms |
2936 KB |
Output is correct |
34 |
Correct |
5 ms |
2936 KB |
Output is correct |
35 |
Correct |
5 ms |
2936 KB |
Output is correct |
36 |
Correct |
5 ms |
2940 KB |
Output is correct |
37 |
Correct |
5 ms |
2936 KB |
Output is correct |
38 |
Correct |
5 ms |
2936 KB |
Output is correct |
39 |
Correct |
5 ms |
2936 KB |
Output is correct |
40 |
Correct |
5 ms |
2936 KB |
Output is correct |
41 |
Correct |
5 ms |
2936 KB |
Output is correct |
42 |
Correct |
5 ms |
2936 KB |
Output is correct |
43 |
Correct |
5 ms |
2936 KB |
Output is correct |
44 |
Correct |
5 ms |
2936 KB |
Output is correct |
45 |
Correct |
6 ms |
2936 KB |
Output is correct |
46 |
Correct |
15 ms |
3448 KB |
Output is correct |
47 |
Correct |
15 ms |
3448 KB |
Output is correct |
48 |
Correct |
15 ms |
3448 KB |
Output is correct |
49 |
Correct |
10 ms |
3704 KB |
Output is correct |
50 |
Correct |
10 ms |
3704 KB |
Output is correct |
51 |
Correct |
10 ms |
3704 KB |
Output is correct |
52 |
Correct |
10 ms |
3576 KB |
Output is correct |
53 |
Correct |
11 ms |
3576 KB |
Output is correct |
54 |
Correct |
11 ms |
3548 KB |
Output is correct |
55 |
Correct |
11 ms |
3576 KB |
Output is correct |
56 |
Correct |
11 ms |
3576 KB |
Output is correct |
57 |
Correct |
23 ms |
3448 KB |
Output is correct |
58 |
Correct |
24 ms |
3448 KB |
Output is correct |
59 |
Correct |
23 ms |
3448 KB |
Output is correct |
60 |
Correct |
22 ms |
3448 KB |
Output is correct |
61 |
Correct |
12 ms |
3580 KB |
Output is correct |
62 |
Correct |
12 ms |
3576 KB |
Output is correct |
63 |
Correct |
12 ms |
3576 KB |
Output is correct |
64 |
Correct |
16 ms |
3448 KB |
Output is correct |
65 |
Correct |
14 ms |
3448 KB |
Output is correct |
66 |
Correct |
15 ms |
3448 KB |
Output is correct |
67 |
Correct |
16 ms |
3448 KB |
Output is correct |
68 |
Correct |
9 ms |
3704 KB |
Output is correct |
69 |
Correct |
10 ms |
3576 KB |
Output is correct |
70 |
Correct |
10 ms |
3576 KB |
Output is correct |
71 |
Correct |
10 ms |
3576 KB |
Output is correct |
72 |
Correct |
21 ms |
3448 KB |
Output is correct |
73 |
Correct |
21 ms |
3496 KB |
Output is correct |
74 |
Correct |
11 ms |
3580 KB |
Output is correct |
75 |
Correct |
11 ms |
3448 KB |
Output is correct |
76 |
Correct |
12 ms |
3576 KB |
Output is correct |
77 |
Correct |
10 ms |
3448 KB |
Output is correct |
78 |
Correct |
12 ms |
3448 KB |
Output is correct |
79 |
Correct |
10 ms |
3448 KB |
Output is correct |
80 |
Correct |
10 ms |
3448 KB |
Output is correct |
81 |
Correct |
12 ms |
3576 KB |
Output is correct |
82 |
Correct |
12 ms |
3576 KB |
Output is correct |
83 |
Correct |
11 ms |
3452 KB |
Output is correct |
84 |
Correct |
11 ms |
3448 KB |
Output is correct |
85 |
Correct |
11 ms |
3448 KB |
Output is correct |
86 |
Correct |
11 ms |
3448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2808 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 |
2808 KB |
Output is correct |
5 |
Correct |
3 ms |
2936 KB |
Output is correct |
6 |
Correct |
5 ms |
2936 KB |
Output is correct |
7 |
Correct |
5 ms |
2936 KB |
Output is correct |
8 |
Correct |
5 ms |
3064 KB |
Output is correct |
9 |
Correct |
5 ms |
2936 KB |
Output is correct |
10 |
Correct |
5 ms |
2936 KB |
Output is correct |
11 |
Correct |
5 ms |
2936 KB |
Output is correct |
12 |
Correct |
5 ms |
2936 KB |
Output is correct |
13 |
Correct |
5 ms |
2936 KB |
Output is correct |
14 |
Correct |
5 ms |
2936 KB |
Output is correct |
15 |
Correct |
5 ms |
2808 KB |
Output is correct |
16 |
Correct |
5 ms |
2936 KB |
Output is correct |
17 |
Correct |
5 ms |
2936 KB |
Output is correct |
18 |
Correct |
5 ms |
2936 KB |
Output is correct |
19 |
Correct |
5 ms |
2936 KB |
Output is correct |
20 |
Correct |
5 ms |
2936 KB |
Output is correct |
21 |
Correct |
5 ms |
2936 KB |
Output is correct |
22 |
Correct |
5 ms |
2936 KB |
Output is correct |
23 |
Correct |
5 ms |
2936 KB |
Output is correct |
24 |
Correct |
5 ms |
2936 KB |
Output is correct |
25 |
Correct |
5 ms |
2936 KB |
Output is correct |
26 |
Correct |
5 ms |
2884 KB |
Output is correct |
27 |
Correct |
5 ms |
2936 KB |
Output is correct |
28 |
Correct |
5 ms |
2936 KB |
Output is correct |
29 |
Correct |
5 ms |
2936 KB |
Output is correct |
30 |
Correct |
5 ms |
2936 KB |
Output is correct |
31 |
Correct |
6 ms |
2936 KB |
Output is correct |
32 |
Correct |
5 ms |
2936 KB |
Output is correct |
33 |
Correct |
5 ms |
2936 KB |
Output is correct |
34 |
Correct |
5 ms |
2936 KB |
Output is correct |
35 |
Correct |
5 ms |
2936 KB |
Output is correct |
36 |
Correct |
5 ms |
2940 KB |
Output is correct |
37 |
Correct |
5 ms |
2936 KB |
Output is correct |
38 |
Correct |
5 ms |
2936 KB |
Output is correct |
39 |
Correct |
5 ms |
2936 KB |
Output is correct |
40 |
Correct |
5 ms |
2936 KB |
Output is correct |
41 |
Correct |
5 ms |
2936 KB |
Output is correct |
42 |
Correct |
5 ms |
2936 KB |
Output is correct |
43 |
Correct |
5 ms |
2936 KB |
Output is correct |
44 |
Correct |
5 ms |
2936 KB |
Output is correct |
45 |
Correct |
6 ms |
2936 KB |
Output is correct |
46 |
Correct |
15 ms |
3448 KB |
Output is correct |
47 |
Correct |
15 ms |
3448 KB |
Output is correct |
48 |
Correct |
15 ms |
3448 KB |
Output is correct |
49 |
Correct |
10 ms |
3704 KB |
Output is correct |
50 |
Correct |
10 ms |
3704 KB |
Output is correct |
51 |
Correct |
10 ms |
3704 KB |
Output is correct |
52 |
Correct |
10 ms |
3576 KB |
Output is correct |
53 |
Correct |
11 ms |
3576 KB |
Output is correct |
54 |
Correct |
11 ms |
3548 KB |
Output is correct |
55 |
Correct |
11 ms |
3576 KB |
Output is correct |
56 |
Correct |
11 ms |
3576 KB |
Output is correct |
57 |
Correct |
23 ms |
3448 KB |
Output is correct |
58 |
Correct |
24 ms |
3448 KB |
Output is correct |
59 |
Correct |
23 ms |
3448 KB |
Output is correct |
60 |
Correct |
22 ms |
3448 KB |
Output is correct |
61 |
Correct |
12 ms |
3580 KB |
Output is correct |
62 |
Correct |
12 ms |
3576 KB |
Output is correct |
63 |
Correct |
12 ms |
3576 KB |
Output is correct |
64 |
Correct |
16 ms |
3448 KB |
Output is correct |
65 |
Correct |
14 ms |
3448 KB |
Output is correct |
66 |
Correct |
15 ms |
3448 KB |
Output is correct |
67 |
Correct |
16 ms |
3448 KB |
Output is correct |
68 |
Correct |
9 ms |
3704 KB |
Output is correct |
69 |
Correct |
10 ms |
3576 KB |
Output is correct |
70 |
Correct |
10 ms |
3576 KB |
Output is correct |
71 |
Correct |
10 ms |
3576 KB |
Output is correct |
72 |
Correct |
21 ms |
3448 KB |
Output is correct |
73 |
Correct |
21 ms |
3496 KB |
Output is correct |
74 |
Correct |
11 ms |
3580 KB |
Output is correct |
75 |
Correct |
11 ms |
3448 KB |
Output is correct |
76 |
Correct |
12 ms |
3576 KB |
Output is correct |
77 |
Correct |
10 ms |
3448 KB |
Output is correct |
78 |
Correct |
12 ms |
3448 KB |
Output is correct |
79 |
Correct |
10 ms |
3448 KB |
Output is correct |
80 |
Correct |
10 ms |
3448 KB |
Output is correct |
81 |
Correct |
12 ms |
3576 KB |
Output is correct |
82 |
Correct |
12 ms |
3576 KB |
Output is correct |
83 |
Correct |
11 ms |
3452 KB |
Output is correct |
84 |
Correct |
11 ms |
3448 KB |
Output is correct |
85 |
Correct |
11 ms |
3448 KB |
Output is correct |
86 |
Correct |
11 ms |
3448 KB |
Output is correct |
87 |
Correct |
34 ms |
4472 KB |
Output is correct |
88 |
Correct |
119 ms |
7928 KB |
Output is correct |
89 |
Correct |
491 ms |
18696 KB |
Output is correct |
90 |
Correct |
484 ms |
18848 KB |
Output is correct |
91 |
Correct |
479 ms |
18812 KB |
Output is correct |
92 |
Correct |
193 ms |
24824 KB |
Output is correct |
93 |
Correct |
206 ms |
24824 KB |
Output is correct |
94 |
Correct |
219 ms |
24840 KB |
Output is correct |
95 |
Correct |
225 ms |
21496 KB |
Output is correct |
96 |
Correct |
231 ms |
21496 KB |
Output is correct |
97 |
Correct |
278 ms |
21496 KB |
Output is correct |
98 |
Correct |
252 ms |
21624 KB |
Output is correct |
99 |
Correct |
258 ms |
21040 KB |
Output is correct |
100 |
Correct |
850 ms |
19192 KB |
Output is correct |
101 |
Correct |
881 ms |
19124 KB |
Output is correct |
102 |
Correct |
880 ms |
19192 KB |
Output is correct |
103 |
Correct |
936 ms |
19220 KB |
Output is correct |
104 |
Correct |
304 ms |
20904 KB |
Output is correct |
105 |
Correct |
317 ms |
21112 KB |
Output is correct |
106 |
Correct |
305 ms |
21072 KB |
Output is correct |
107 |
Correct |
499 ms |
18296 KB |
Output is correct |
108 |
Correct |
473 ms |
18684 KB |
Output is correct |
109 |
Correct |
520 ms |
18808 KB |
Output is correct |
110 |
Correct |
194 ms |
24696 KB |
Output is correct |
111 |
Correct |
225 ms |
21492 KB |
Output is correct |
112 |
Correct |
224 ms |
21496 KB |
Output is correct |
113 |
Correct |
220 ms |
21032 KB |
Output is correct |
114 |
Correct |
872 ms |
19124 KB |
Output is correct |
115 |
Correct |
934 ms |
19320 KB |
Output is correct |
116 |
Correct |
295 ms |
20992 KB |
Output is correct |
117 |
Correct |
226 ms |
19832 KB |
Output is correct |
118 |
Correct |
222 ms |
19320 KB |
Output is correct |
119 |
Correct |
221 ms |
19192 KB |
Output is correct |
120 |
Correct |
237 ms |
19828 KB |
Output is correct |
121 |
Correct |
253 ms |
19448 KB |
Output is correct |
122 |
Correct |
217 ms |
19192 KB |
Output is correct |
123 |
Correct |
318 ms |
19864 KB |
Output is correct |
124 |
Correct |
313 ms |
19448 KB |
Output is correct |
125 |
Correct |
314 ms |
19320 KB |
Output is correct |
126 |
Correct |
308 ms |
19896 KB |
Output is correct |
127 |
Correct |
303 ms |
19544 KB |
Output is correct |
128 |
Correct |
296 ms |
19320 KB |
Output is correct |