#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);
};
assert(n <= 4000);
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]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 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 |
384 KB |
Output is correct |
8 |
Correct |
13 ms |
384 KB |
Output is correct |
9 |
Correct |
12 ms |
384 KB |
Output is correct |
10 |
Correct |
15 ms |
296 KB |
Output is correct |
11 |
Correct |
8 ms |
384 KB |
Output is correct |
12 |
Correct |
10 ms |
384 KB |
Output is correct |
13 |
Correct |
7 ms |
384 KB |
Output is correct |
14 |
Correct |
8 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 |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
6 ms |
384 KB |
Output is correct |
20 |
Correct |
9 ms |
256 KB |
Output is correct |
21 |
Correct |
10 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
4 ms |
384 KB |
Output is correct |
26 |
Correct |
7 ms |
384 KB |
Output is correct |
27 |
Correct |
8 ms |
384 KB |
Output is correct |
28 |
Correct |
6 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
4 ms |
384 KB |
Output is correct |
31 |
Correct |
3 ms |
384 KB |
Output is correct |
32 |
Correct |
6 ms |
428 KB |
Output is correct |
33 |
Correct |
6 ms |
384 KB |
Output is correct |
34 |
Correct |
4 ms |
384 KB |
Output is correct |
35 |
Correct |
4 ms |
384 KB |
Output is correct |
36 |
Correct |
3 ms |
384 KB |
Output is correct |
37 |
Correct |
3 ms |
384 KB |
Output is correct |
38 |
Correct |
4 ms |
384 KB |
Output is correct |
39 |
Correct |
5 ms |
360 KB |
Output is correct |
40 |
Correct |
4 ms |
384 KB |
Output is correct |
41 |
Correct |
4 ms |
384 KB |
Output is correct |
42 |
Correct |
4 ms |
256 KB |
Output is correct |
43 |
Correct |
3 ms |
384 KB |
Output is correct |
44 |
Correct |
4 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 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 |
384 KB |
Output is correct |
8 |
Correct |
13 ms |
384 KB |
Output is correct |
9 |
Correct |
12 ms |
384 KB |
Output is correct |
10 |
Correct |
15 ms |
296 KB |
Output is correct |
11 |
Correct |
8 ms |
384 KB |
Output is correct |
12 |
Correct |
10 ms |
384 KB |
Output is correct |
13 |
Correct |
7 ms |
384 KB |
Output is correct |
14 |
Correct |
8 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 |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
6 ms |
384 KB |
Output is correct |
20 |
Correct |
9 ms |
256 KB |
Output is correct |
21 |
Correct |
10 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
4 ms |
384 KB |
Output is correct |
26 |
Correct |
7 ms |
384 KB |
Output is correct |
27 |
Correct |
8 ms |
384 KB |
Output is correct |
28 |
Correct |
6 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
4 ms |
384 KB |
Output is correct |
31 |
Correct |
3 ms |
384 KB |
Output is correct |
32 |
Correct |
6 ms |
428 KB |
Output is correct |
33 |
Correct |
6 ms |
384 KB |
Output is correct |
34 |
Correct |
4 ms |
384 KB |
Output is correct |
35 |
Correct |
4 ms |
384 KB |
Output is correct |
36 |
Correct |
3 ms |
384 KB |
Output is correct |
37 |
Correct |
3 ms |
384 KB |
Output is correct |
38 |
Correct |
4 ms |
384 KB |
Output is correct |
39 |
Correct |
5 ms |
360 KB |
Output is correct |
40 |
Correct |
4 ms |
384 KB |
Output is correct |
41 |
Correct |
4 ms |
384 KB |
Output is correct |
42 |
Correct |
4 ms |
256 KB |
Output is correct |
43 |
Correct |
3 ms |
384 KB |
Output is correct |
44 |
Correct |
4 ms |
256 KB |
Output is correct |
45 |
Correct |
29 ms |
384 KB |
Output is correct |
46 |
Correct |
24 ms |
512 KB |
Output is correct |
47 |
Correct |
33 ms |
512 KB |
Output is correct |
48 |
Correct |
33 ms |
512 KB |
Output is correct |
49 |
Correct |
800 ms |
632 KB |
Output is correct |
50 |
Correct |
939 ms |
512 KB |
Output is correct |
51 |
Correct |
768 ms |
636 KB |
Output is correct |
52 |
Correct |
393 ms |
652 KB |
Output is correct |
53 |
Correct |
418 ms |
512 KB |
Output is correct |
54 |
Correct |
458 ms |
636 KB |
Output is correct |
55 |
Correct |
366 ms |
512 KB |
Output is correct |
56 |
Correct |
402 ms |
512 KB |
Output is correct |
57 |
Correct |
34 ms |
512 KB |
Output is correct |
58 |
Correct |
30 ms |
512 KB |
Output is correct |
59 |
Correct |
30 ms |
512 KB |
Output is correct |
60 |
Correct |
35 ms |
512 KB |
Output is correct |
61 |
Correct |
444 ms |
632 KB |
Output is correct |
62 |
Correct |
376 ms |
760 KB |
Output is correct |
63 |
Correct |
385 ms |
732 KB |
Output is correct |
64 |
Correct |
7 ms |
356 KB |
Output is correct |
65 |
Correct |
6 ms |
384 KB |
Output is correct |
66 |
Correct |
11 ms |
512 KB |
Output is correct |
67 |
Correct |
17 ms |
384 KB |
Output is correct |
68 |
Correct |
402 ms |
588 KB |
Output is correct |
69 |
Correct |
368 ms |
648 KB |
Output is correct |
70 |
Correct |
223 ms |
504 KB |
Output is correct |
71 |
Correct |
162 ms |
384 KB |
Output is correct |
72 |
Correct |
30 ms |
512 KB |
Output is correct |
73 |
Correct |
10 ms |
384 KB |
Output is correct |
74 |
Correct |
190 ms |
452 KB |
Output is correct |
75 |
Correct |
221 ms |
512 KB |
Output is correct |
76 |
Correct |
136 ms |
512 KB |
Output is correct |
77 |
Correct |
110 ms |
636 KB |
Output is correct |
78 |
Correct |
85 ms |
608 KB |
Output is correct |
79 |
Correct |
69 ms |
504 KB |
Output is correct |
80 |
Correct |
48 ms |
460 KB |
Output is correct |
81 |
Correct |
183 ms |
532 KB |
Output is correct |
82 |
Correct |
196 ms |
516 KB |
Output is correct |
83 |
Correct |
115 ms |
760 KB |
Output is correct |
84 |
Correct |
98 ms |
384 KB |
Output is correct |
85 |
Correct |
65 ms |
428 KB |
Output is correct |
86 |
Correct |
50 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 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 |
384 KB |
Output is correct |
8 |
Correct |
13 ms |
384 KB |
Output is correct |
9 |
Correct |
12 ms |
384 KB |
Output is correct |
10 |
Correct |
15 ms |
296 KB |
Output is correct |
11 |
Correct |
8 ms |
384 KB |
Output is correct |
12 |
Correct |
10 ms |
384 KB |
Output is correct |
13 |
Correct |
7 ms |
384 KB |
Output is correct |
14 |
Correct |
8 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 |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
6 ms |
384 KB |
Output is correct |
20 |
Correct |
9 ms |
256 KB |
Output is correct |
21 |
Correct |
10 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
4 ms |
384 KB |
Output is correct |
26 |
Correct |
7 ms |
384 KB |
Output is correct |
27 |
Correct |
8 ms |
384 KB |
Output is correct |
28 |
Correct |
6 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
4 ms |
384 KB |
Output is correct |
31 |
Correct |
3 ms |
384 KB |
Output is correct |
32 |
Correct |
6 ms |
428 KB |
Output is correct |
33 |
Correct |
6 ms |
384 KB |
Output is correct |
34 |
Correct |
4 ms |
384 KB |
Output is correct |
35 |
Correct |
4 ms |
384 KB |
Output is correct |
36 |
Correct |
3 ms |
384 KB |
Output is correct |
37 |
Correct |
3 ms |
384 KB |
Output is correct |
38 |
Correct |
4 ms |
384 KB |
Output is correct |
39 |
Correct |
5 ms |
360 KB |
Output is correct |
40 |
Correct |
4 ms |
384 KB |
Output is correct |
41 |
Correct |
4 ms |
384 KB |
Output is correct |
42 |
Correct |
4 ms |
256 KB |
Output is correct |
43 |
Correct |
3 ms |
384 KB |
Output is correct |
44 |
Correct |
4 ms |
256 KB |
Output is correct |
45 |
Correct |
29 ms |
384 KB |
Output is correct |
46 |
Correct |
24 ms |
512 KB |
Output is correct |
47 |
Correct |
33 ms |
512 KB |
Output is correct |
48 |
Correct |
33 ms |
512 KB |
Output is correct |
49 |
Correct |
800 ms |
632 KB |
Output is correct |
50 |
Correct |
939 ms |
512 KB |
Output is correct |
51 |
Correct |
768 ms |
636 KB |
Output is correct |
52 |
Correct |
393 ms |
652 KB |
Output is correct |
53 |
Correct |
418 ms |
512 KB |
Output is correct |
54 |
Correct |
458 ms |
636 KB |
Output is correct |
55 |
Correct |
366 ms |
512 KB |
Output is correct |
56 |
Correct |
402 ms |
512 KB |
Output is correct |
57 |
Correct |
34 ms |
512 KB |
Output is correct |
58 |
Correct |
30 ms |
512 KB |
Output is correct |
59 |
Correct |
30 ms |
512 KB |
Output is correct |
60 |
Correct |
35 ms |
512 KB |
Output is correct |
61 |
Correct |
444 ms |
632 KB |
Output is correct |
62 |
Correct |
376 ms |
760 KB |
Output is correct |
63 |
Correct |
385 ms |
732 KB |
Output is correct |
64 |
Correct |
7 ms |
356 KB |
Output is correct |
65 |
Correct |
6 ms |
384 KB |
Output is correct |
66 |
Correct |
11 ms |
512 KB |
Output is correct |
67 |
Correct |
17 ms |
384 KB |
Output is correct |
68 |
Correct |
402 ms |
588 KB |
Output is correct |
69 |
Correct |
368 ms |
648 KB |
Output is correct |
70 |
Correct |
223 ms |
504 KB |
Output is correct |
71 |
Correct |
162 ms |
384 KB |
Output is correct |
72 |
Correct |
30 ms |
512 KB |
Output is correct |
73 |
Correct |
10 ms |
384 KB |
Output is correct |
74 |
Correct |
190 ms |
452 KB |
Output is correct |
75 |
Correct |
221 ms |
512 KB |
Output is correct |
76 |
Correct |
136 ms |
512 KB |
Output is correct |
77 |
Correct |
110 ms |
636 KB |
Output is correct |
78 |
Correct |
85 ms |
608 KB |
Output is correct |
79 |
Correct |
69 ms |
504 KB |
Output is correct |
80 |
Correct |
48 ms |
460 KB |
Output is correct |
81 |
Correct |
183 ms |
532 KB |
Output is correct |
82 |
Correct |
196 ms |
516 KB |
Output is correct |
83 |
Correct |
115 ms |
760 KB |
Output is correct |
84 |
Correct |
98 ms |
384 KB |
Output is correct |
85 |
Correct |
65 ms |
428 KB |
Output is correct |
86 |
Correct |
50 ms |
384 KB |
Output is correct |
87 |
Runtime error |
9 ms |
1024 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
88 |
Halted |
0 ms |
0 KB |
- |