#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = long long;
using std::cout;
using std::endl;
using std::cin;
struct SegmentTree {
std::vector<i64> seg;
int sz = 1;
SegmentTree(int n) {
while(sz < n) sz <<= 1;
seg.assign(sz * 2 - 1, 0);
}
void add(int k, int x = 1) {
k += sz - 1;
seg[k] += x;
while(k) {
k = (k - 1) >> 1;
seg[k] = seg[2 * k + 1] + seg[2 * k + 2];
}
}
void update(int k, int x = 1) {
k += sz - 1;
seg[k] = x;
while(k) {
k = (k - 1) >> 1;
seg[k] = seg[2 * k + 1] + seg[2 * k + 2];
}
}
i64 get(int a, int b, int k, int l, int r) {
if(b <= l || r <= a) return 0;
if(a <= l && r <= b) return seg[k];
i64 L = get(a, b, 2 * k + 1, l, (l + r) >> 1);
i64 R = get(a, b, 2 * k + 2, (l + r) >> 1, r);
return L + R;
}
i64 get(int a, int b) {
return get(a, b, 0, 0, sz);
}
i64 operator[](int k) {
return seg[k + sz - 1];
}
};
int main() {
int n; scanf("%d", &n);
std::vector<int> c(n), a(n - 1), b(n - 1), latte;
std::vector<int> number(n, -1);
for(int i = 0; i < n; i++) {
scanf("%d", &c[i]);
latte.push_back(c[i]);
}
for(int i = 0; i < n - 1; i++) {
scanf("%d%d", &a[i], &b[i]);
a[i]--; b[i]--;
number[b[i]] = i;
}
sort(begin(latte), end(latte)); latte.erase(unique(begin(latte), end(latte)), end(latte));
std::function<int (int)> getIndex = [&](int v) {
return lower_bound(begin(latte), end(latte), v) - begin(latte);
};
std::vector<int> parent(n, -1);
for(int i = 0; i < n - 1; i++) {
i64 ans = 0;
int now = a[i];
SegmentTree seg(latte.size());
while(now != -1) {
int k = getIndex(c[now]);
ans += seg.get(0, k);
seg.add(k);
c[now] = c[b[i]];
now = parent[now];
}
parent[b[i]] = a[i];
printf("%lld\n", ans);
// for(auto v: c) cout << v << " "; cout << endl;
}
return 0;
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:53: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:57:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &c[i]);
~~~~~^~~~~~~~~~~~~
construction.cpp:62:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a[i], &b[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
256 KB |
Output is correct |
8 |
Correct |
11 ms |
384 KB |
Output is correct |
9 |
Correct |
14 ms |
384 KB |
Output is correct |
10 |
Correct |
11 ms |
384 KB |
Output is correct |
11 |
Correct |
7 ms |
384 KB |
Output is correct |
12 |
Correct |
8 ms |
384 KB |
Output is correct |
13 |
Correct |
8 ms |
384 KB |
Output is correct |
14 |
Correct |
10 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
9 ms |
384 KB |
Output is correct |
20 |
Correct |
8 ms |
384 KB |
Output is correct |
21 |
Correct |
8 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
3 ms |
384 KB |
Output is correct |
26 |
Correct |
10 ms |
256 KB |
Output is correct |
27 |
Correct |
10 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
256 KB |
Output is correct |
29 |
Correct |
5 ms |
256 KB |
Output is correct |
30 |
Correct |
3 ms |
384 KB |
Output is correct |
31 |
Correct |
2 ms |
384 KB |
Output is correct |
32 |
Correct |
6 ms |
256 KB |
Output is correct |
33 |
Correct |
7 ms |
384 KB |
Output is correct |
34 |
Correct |
6 ms |
356 KB |
Output is correct |
35 |
Correct |
4 ms |
384 KB |
Output is correct |
36 |
Correct |
5 ms |
256 KB |
Output is correct |
37 |
Correct |
5 ms |
256 KB |
Output is correct |
38 |
Correct |
3 ms |
256 KB |
Output is correct |
39 |
Correct |
26 ms |
384 KB |
Output is correct |
40 |
Correct |
4 ms |
384 KB |
Output is correct |
41 |
Correct |
5 ms |
384 KB |
Output is correct |
42 |
Correct |
4 ms |
384 KB |
Output is correct |
43 |
Correct |
4 ms |
384 KB |
Output is correct |
44 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
256 KB |
Output is correct |
8 |
Correct |
11 ms |
384 KB |
Output is correct |
9 |
Correct |
14 ms |
384 KB |
Output is correct |
10 |
Correct |
11 ms |
384 KB |
Output is correct |
11 |
Correct |
7 ms |
384 KB |
Output is correct |
12 |
Correct |
8 ms |
384 KB |
Output is correct |
13 |
Correct |
8 ms |
384 KB |
Output is correct |
14 |
Correct |
10 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
9 ms |
384 KB |
Output is correct |
20 |
Correct |
8 ms |
384 KB |
Output is correct |
21 |
Correct |
8 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
3 ms |
384 KB |
Output is correct |
26 |
Correct |
10 ms |
256 KB |
Output is correct |
27 |
Correct |
10 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
256 KB |
Output is correct |
29 |
Correct |
5 ms |
256 KB |
Output is correct |
30 |
Correct |
3 ms |
384 KB |
Output is correct |
31 |
Correct |
2 ms |
384 KB |
Output is correct |
32 |
Correct |
6 ms |
256 KB |
Output is correct |
33 |
Correct |
7 ms |
384 KB |
Output is correct |
34 |
Correct |
6 ms |
356 KB |
Output is correct |
35 |
Correct |
4 ms |
384 KB |
Output is correct |
36 |
Correct |
5 ms |
256 KB |
Output is correct |
37 |
Correct |
5 ms |
256 KB |
Output is correct |
38 |
Correct |
3 ms |
256 KB |
Output is correct |
39 |
Correct |
26 ms |
384 KB |
Output is correct |
40 |
Correct |
4 ms |
384 KB |
Output is correct |
41 |
Correct |
5 ms |
384 KB |
Output is correct |
42 |
Correct |
4 ms |
384 KB |
Output is correct |
43 |
Correct |
4 ms |
384 KB |
Output is correct |
44 |
Correct |
3 ms |
384 KB |
Output is correct |
45 |
Correct |
6 ms |
384 KB |
Output is correct |
46 |
Correct |
32 ms |
512 KB |
Output is correct |
47 |
Correct |
30 ms |
512 KB |
Output is correct |
48 |
Correct |
27 ms |
512 KB |
Output is correct |
49 |
Correct |
848 ms |
704 KB |
Output is correct |
50 |
Correct |
746 ms |
760 KB |
Output is correct |
51 |
Correct |
803 ms |
512 KB |
Output is correct |
52 |
Correct |
458 ms |
512 KB |
Output is correct |
53 |
Correct |
478 ms |
632 KB |
Output is correct |
54 |
Correct |
418 ms |
632 KB |
Output is correct |
55 |
Correct |
403 ms |
632 KB |
Output is correct |
56 |
Correct |
499 ms |
604 KB |
Output is correct |
57 |
Correct |
29 ms |
512 KB |
Output is correct |
58 |
Correct |
43 ms |
512 KB |
Output is correct |
59 |
Correct |
33 ms |
632 KB |
Output is correct |
60 |
Correct |
41 ms |
512 KB |
Output is correct |
61 |
Correct |
426 ms |
512 KB |
Output is correct |
62 |
Correct |
376 ms |
512 KB |
Output is correct |
63 |
Correct |
587 ms |
512 KB |
Output is correct |
64 |
Correct |
8 ms |
512 KB |
Output is correct |
65 |
Correct |
6 ms |
512 KB |
Output is correct |
66 |
Correct |
12 ms |
512 KB |
Output is correct |
67 |
Correct |
23 ms |
512 KB |
Output is correct |
68 |
Correct |
391 ms |
620 KB |
Output is correct |
69 |
Correct |
374 ms |
640 KB |
Output is correct |
70 |
Correct |
204 ms |
384 KB |
Output is correct |
71 |
Correct |
217 ms |
528 KB |
Output is correct |
72 |
Correct |
39 ms |
512 KB |
Output is correct |
73 |
Correct |
11 ms |
512 KB |
Output is correct |
74 |
Correct |
230 ms |
512 KB |
Output is correct |
75 |
Correct |
198 ms |
512 KB |
Output is correct |
76 |
Correct |
168 ms |
636 KB |
Output is correct |
77 |
Correct |
113 ms |
512 KB |
Output is correct |
78 |
Correct |
107 ms |
640 KB |
Output is correct |
79 |
Correct |
61 ms |
512 KB |
Output is correct |
80 |
Correct |
48 ms |
580 KB |
Output is correct |
81 |
Correct |
182 ms |
512 KB |
Output is correct |
82 |
Correct |
146 ms |
512 KB |
Output is correct |
83 |
Correct |
106 ms |
732 KB |
Output is correct |
84 |
Correct |
88 ms |
512 KB |
Output is correct |
85 |
Correct |
65 ms |
512 KB |
Output is correct |
86 |
Correct |
50 ms |
496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
256 KB |
Output is correct |
8 |
Correct |
11 ms |
384 KB |
Output is correct |
9 |
Correct |
14 ms |
384 KB |
Output is correct |
10 |
Correct |
11 ms |
384 KB |
Output is correct |
11 |
Correct |
7 ms |
384 KB |
Output is correct |
12 |
Correct |
8 ms |
384 KB |
Output is correct |
13 |
Correct |
8 ms |
384 KB |
Output is correct |
14 |
Correct |
10 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
9 ms |
384 KB |
Output is correct |
20 |
Correct |
8 ms |
384 KB |
Output is correct |
21 |
Correct |
8 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
3 ms |
384 KB |
Output is correct |
26 |
Correct |
10 ms |
256 KB |
Output is correct |
27 |
Correct |
10 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
256 KB |
Output is correct |
29 |
Correct |
5 ms |
256 KB |
Output is correct |
30 |
Correct |
3 ms |
384 KB |
Output is correct |
31 |
Correct |
2 ms |
384 KB |
Output is correct |
32 |
Correct |
6 ms |
256 KB |
Output is correct |
33 |
Correct |
7 ms |
384 KB |
Output is correct |
34 |
Correct |
6 ms |
356 KB |
Output is correct |
35 |
Correct |
4 ms |
384 KB |
Output is correct |
36 |
Correct |
5 ms |
256 KB |
Output is correct |
37 |
Correct |
5 ms |
256 KB |
Output is correct |
38 |
Correct |
3 ms |
256 KB |
Output is correct |
39 |
Correct |
26 ms |
384 KB |
Output is correct |
40 |
Correct |
4 ms |
384 KB |
Output is correct |
41 |
Correct |
5 ms |
384 KB |
Output is correct |
42 |
Correct |
4 ms |
384 KB |
Output is correct |
43 |
Correct |
4 ms |
384 KB |
Output is correct |
44 |
Correct |
3 ms |
384 KB |
Output is correct |
45 |
Correct |
6 ms |
384 KB |
Output is correct |
46 |
Correct |
32 ms |
512 KB |
Output is correct |
47 |
Correct |
30 ms |
512 KB |
Output is correct |
48 |
Correct |
27 ms |
512 KB |
Output is correct |
49 |
Correct |
848 ms |
704 KB |
Output is correct |
50 |
Correct |
746 ms |
760 KB |
Output is correct |
51 |
Correct |
803 ms |
512 KB |
Output is correct |
52 |
Correct |
458 ms |
512 KB |
Output is correct |
53 |
Correct |
478 ms |
632 KB |
Output is correct |
54 |
Correct |
418 ms |
632 KB |
Output is correct |
55 |
Correct |
403 ms |
632 KB |
Output is correct |
56 |
Correct |
499 ms |
604 KB |
Output is correct |
57 |
Correct |
29 ms |
512 KB |
Output is correct |
58 |
Correct |
43 ms |
512 KB |
Output is correct |
59 |
Correct |
33 ms |
632 KB |
Output is correct |
60 |
Correct |
41 ms |
512 KB |
Output is correct |
61 |
Correct |
426 ms |
512 KB |
Output is correct |
62 |
Correct |
376 ms |
512 KB |
Output is correct |
63 |
Correct |
587 ms |
512 KB |
Output is correct |
64 |
Correct |
8 ms |
512 KB |
Output is correct |
65 |
Correct |
6 ms |
512 KB |
Output is correct |
66 |
Correct |
12 ms |
512 KB |
Output is correct |
67 |
Correct |
23 ms |
512 KB |
Output is correct |
68 |
Correct |
391 ms |
620 KB |
Output is correct |
69 |
Correct |
374 ms |
640 KB |
Output is correct |
70 |
Correct |
204 ms |
384 KB |
Output is correct |
71 |
Correct |
217 ms |
528 KB |
Output is correct |
72 |
Correct |
39 ms |
512 KB |
Output is correct |
73 |
Correct |
11 ms |
512 KB |
Output is correct |
74 |
Correct |
230 ms |
512 KB |
Output is correct |
75 |
Correct |
198 ms |
512 KB |
Output is correct |
76 |
Correct |
168 ms |
636 KB |
Output is correct |
77 |
Correct |
113 ms |
512 KB |
Output is correct |
78 |
Correct |
107 ms |
640 KB |
Output is correct |
79 |
Correct |
61 ms |
512 KB |
Output is correct |
80 |
Correct |
48 ms |
580 KB |
Output is correct |
81 |
Correct |
182 ms |
512 KB |
Output is correct |
82 |
Correct |
146 ms |
512 KB |
Output is correct |
83 |
Correct |
106 ms |
732 KB |
Output is correct |
84 |
Correct |
88 ms |
512 KB |
Output is correct |
85 |
Correct |
65 ms |
512 KB |
Output is correct |
86 |
Correct |
50 ms |
496 KB |
Output is correct |
87 |
Correct |
229 ms |
1204 KB |
Output is correct |
88 |
Correct |
1192 ms |
2524 KB |
Output is correct |
89 |
Execution timed out |
2048 ms |
7144 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |