#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sz size()
#define ll long long
#define ld long double
#define all(_v) _v.begin(), _v.end()
#define pii pair <int, int>
#define pll pair <ll, ll>
#define veci vector <int>
#define vecll vector <ll>
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const double PI = 3.1415926535897932384626433832795;
const double eps = 1e-9;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;
ll _mergeSort(int arr[], int temp[], int left, int right);
ll mergee(int arr[], int temp[], int left, int mid, int right);
ll mergeSort(int arr[], int array_size) {
int temp[array_size];
return _mergeSort(arr, temp, 0, array_size - 1);
}
ll _mergeSort(int arr[], int temp[], int left, int right) {
ll mid, inv_count = 0;
if (right > left) {
mid = (right + left) / 2;
inv_count += _mergeSort(arr, temp, left, mid);
inv_count += _mergeSort(arr, temp, mid + 1, right);
inv_count += mergee(arr, temp, left, mid + 1, right);
}
return inv_count;
}
ll mergee(int arr[], int temp[], int left, int mid, int right) {
int i, j, k;
ll inv_count = 0;
i = left;
j = mid;
k = left;
while ((i <= mid - 1) && (j <= right)) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
}
else {
temp[k++] = arr[j++];
inv_count = inv_count + (mid - i);
}
}
while (i <= mid - 1)
temp[k++] = arr[i++];
while (j <= right)
temp[k++] = arr[j++];
for (i = left; i <= right; i++)
arr[i] = temp[i];
return inv_count;
}
const int MAXN = 1e5 + 10;
int n;
int c[MAXN];
int p[MAXN];
void solve() {
cin >> n;
for(int i = 1; i <= n; ++i)
cin >> c[i];
p[1] = -1;
for(int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
p[v] = u;
int x = u;
veci inv_u;
while(x != -1) inv_u.pb(c[x]), x = p[x];
reverse(all(inv_u));
int cntinv[inv_u.sz];
for(int j = 0; j < inv_u.sz; ++j) cntinv[j] = inv_u[j];
cout << mergeSort(cntinv, inv_u.sz) << '\n';
x = u;
while(x != -1) c[x] = c[v], x = p[x];
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T = 1;
///cin >> T;
while(T--) solve(), cout << '\n';
return 0;
}
Compilation message
construction.cpp: In function 'void solve()':
construction.cpp:87:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for(int j = 0; j < inv_u.sz; ++j) cntinv[j] = inv_u[j];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
3 ms |
384 KB |
Output is correct |
28 |
Correct |
3 ms |
384 KB |
Output is correct |
29 |
Correct |
3 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
3 ms |
384 KB |
Output is correct |
33 |
Correct |
2 ms |
512 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
384 KB |
Output is correct |
36 |
Correct |
2 ms |
384 KB |
Output is correct |
37 |
Correct |
1 ms |
384 KB |
Output is correct |
38 |
Correct |
1 ms |
384 KB |
Output is correct |
39 |
Correct |
2 ms |
384 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
0 ms |
384 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
2 ms |
384 KB |
Output is correct |
44 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
3 ms |
384 KB |
Output is correct |
28 |
Correct |
3 ms |
384 KB |
Output is correct |
29 |
Correct |
3 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
3 ms |
384 KB |
Output is correct |
33 |
Correct |
2 ms |
512 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
384 KB |
Output is correct |
36 |
Correct |
2 ms |
384 KB |
Output is correct |
37 |
Correct |
1 ms |
384 KB |
Output is correct |
38 |
Correct |
1 ms |
384 KB |
Output is correct |
39 |
Correct |
2 ms |
384 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
0 ms |
384 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
2 ms |
384 KB |
Output is correct |
44 |
Correct |
1 ms |
384 KB |
Output is correct |
45 |
Correct |
1 ms |
384 KB |
Output is correct |
46 |
Correct |
5 ms |
416 KB |
Output is correct |
47 |
Correct |
4 ms |
384 KB |
Output is correct |
48 |
Correct |
4 ms |
384 KB |
Output is correct |
49 |
Correct |
308 ms |
600 KB |
Output is correct |
50 |
Correct |
307 ms |
384 KB |
Output is correct |
51 |
Correct |
304 ms |
504 KB |
Output is correct |
52 |
Correct |
148 ms |
384 KB |
Output is correct |
53 |
Correct |
148 ms |
384 KB |
Output is correct |
54 |
Correct |
149 ms |
384 KB |
Output is correct |
55 |
Correct |
154 ms |
384 KB |
Output is correct |
56 |
Correct |
156 ms |
384 KB |
Output is correct |
57 |
Correct |
4 ms |
384 KB |
Output is correct |
58 |
Correct |
4 ms |
384 KB |
Output is correct |
59 |
Correct |
4 ms |
384 KB |
Output is correct |
60 |
Correct |
4 ms |
384 KB |
Output is correct |
61 |
Correct |
151 ms |
384 KB |
Output is correct |
62 |
Correct |
153 ms |
384 KB |
Output is correct |
63 |
Correct |
150 ms |
384 KB |
Output is correct |
64 |
Correct |
4 ms |
384 KB |
Output is correct |
65 |
Correct |
3 ms |
384 KB |
Output is correct |
66 |
Correct |
4 ms |
384 KB |
Output is correct |
67 |
Correct |
3 ms |
384 KB |
Output is correct |
68 |
Correct |
315 ms |
524 KB |
Output is correct |
69 |
Correct |
149 ms |
456 KB |
Output is correct |
70 |
Correct |
151 ms |
384 KB |
Output is correct |
71 |
Correct |
148 ms |
384 KB |
Output is correct |
72 |
Correct |
4 ms |
384 KB |
Output is correct |
73 |
Correct |
4 ms |
384 KB |
Output is correct |
74 |
Correct |
151 ms |
384 KB |
Output is correct |
75 |
Correct |
74 ms |
384 KB |
Output is correct |
76 |
Correct |
52 ms |
384 KB |
Output is correct |
77 |
Correct |
38 ms |
384 KB |
Output is correct |
78 |
Correct |
76 ms |
436 KB |
Output is correct |
79 |
Correct |
51 ms |
384 KB |
Output is correct |
80 |
Correct |
38 ms |
432 KB |
Output is correct |
81 |
Correct |
77 ms |
384 KB |
Output is correct |
82 |
Correct |
51 ms |
384 KB |
Output is correct |
83 |
Correct |
39 ms |
384 KB |
Output is correct |
84 |
Correct |
76 ms |
384 KB |
Output is correct |
85 |
Correct |
60 ms |
384 KB |
Output is correct |
86 |
Correct |
39 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
3 ms |
384 KB |
Output is correct |
28 |
Correct |
3 ms |
384 KB |
Output is correct |
29 |
Correct |
3 ms |
384 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
3 ms |
384 KB |
Output is correct |
33 |
Correct |
2 ms |
512 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
384 KB |
Output is correct |
36 |
Correct |
2 ms |
384 KB |
Output is correct |
37 |
Correct |
1 ms |
384 KB |
Output is correct |
38 |
Correct |
1 ms |
384 KB |
Output is correct |
39 |
Correct |
2 ms |
384 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
0 ms |
384 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
2 ms |
384 KB |
Output is correct |
44 |
Correct |
1 ms |
384 KB |
Output is correct |
45 |
Correct |
1 ms |
384 KB |
Output is correct |
46 |
Correct |
5 ms |
416 KB |
Output is correct |
47 |
Correct |
4 ms |
384 KB |
Output is correct |
48 |
Correct |
4 ms |
384 KB |
Output is correct |
49 |
Correct |
308 ms |
600 KB |
Output is correct |
50 |
Correct |
307 ms |
384 KB |
Output is correct |
51 |
Correct |
304 ms |
504 KB |
Output is correct |
52 |
Correct |
148 ms |
384 KB |
Output is correct |
53 |
Correct |
148 ms |
384 KB |
Output is correct |
54 |
Correct |
149 ms |
384 KB |
Output is correct |
55 |
Correct |
154 ms |
384 KB |
Output is correct |
56 |
Correct |
156 ms |
384 KB |
Output is correct |
57 |
Correct |
4 ms |
384 KB |
Output is correct |
58 |
Correct |
4 ms |
384 KB |
Output is correct |
59 |
Correct |
4 ms |
384 KB |
Output is correct |
60 |
Correct |
4 ms |
384 KB |
Output is correct |
61 |
Correct |
151 ms |
384 KB |
Output is correct |
62 |
Correct |
153 ms |
384 KB |
Output is correct |
63 |
Correct |
150 ms |
384 KB |
Output is correct |
64 |
Correct |
4 ms |
384 KB |
Output is correct |
65 |
Correct |
3 ms |
384 KB |
Output is correct |
66 |
Correct |
4 ms |
384 KB |
Output is correct |
67 |
Correct |
3 ms |
384 KB |
Output is correct |
68 |
Correct |
315 ms |
524 KB |
Output is correct |
69 |
Correct |
149 ms |
456 KB |
Output is correct |
70 |
Correct |
151 ms |
384 KB |
Output is correct |
71 |
Correct |
148 ms |
384 KB |
Output is correct |
72 |
Correct |
4 ms |
384 KB |
Output is correct |
73 |
Correct |
4 ms |
384 KB |
Output is correct |
74 |
Correct |
151 ms |
384 KB |
Output is correct |
75 |
Correct |
74 ms |
384 KB |
Output is correct |
76 |
Correct |
52 ms |
384 KB |
Output is correct |
77 |
Correct |
38 ms |
384 KB |
Output is correct |
78 |
Correct |
76 ms |
436 KB |
Output is correct |
79 |
Correct |
51 ms |
384 KB |
Output is correct |
80 |
Correct |
38 ms |
432 KB |
Output is correct |
81 |
Correct |
77 ms |
384 KB |
Output is correct |
82 |
Correct |
51 ms |
384 KB |
Output is correct |
83 |
Correct |
39 ms |
384 KB |
Output is correct |
84 |
Correct |
76 ms |
384 KB |
Output is correct |
85 |
Correct |
60 ms |
384 KB |
Output is correct |
86 |
Correct |
39 ms |
384 KB |
Output is correct |
87 |
Correct |
9 ms |
512 KB |
Output is correct |
88 |
Correct |
27 ms |
684 KB |
Output is correct |
89 |
Correct |
109 ms |
1528 KB |
Output is correct |
90 |
Correct |
105 ms |
1400 KB |
Output is correct |
91 |
Correct |
109 ms |
1464 KB |
Output is correct |
92 |
Execution timed out |
2081 ms |
1480 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |