#include "nile.h"
#include <bits/stdc++.h>
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef pair<int, int> pr;
typedef array<int, 3> tp;
vector<int> parent, res, odd[2], even[2], sz, sum;
int find(int K) {
if (parent[K] != K)
parent[K] = find(parent[K]);
return parent[K];
}
int calc(int X) {
X = find(X);
if (sz[X] & 1)
res[X] = sum[X] + min(odd[1][X], even[0][X]);
else
res[X] = sum[X];
return res[X];
}
int merge(int X, int Y) {
X = find(X), Y = find(Y);
int ret = -res[X] - res[Y];
if (X > Y)
swap(X, Y);
if (sz[X] & 1) {
odd[0][X] = min(odd[0][X], even[0][Y]);
even[0][X] = min(even[0][X], odd[0][Y]);
odd[1][X] = min(odd[1][X], even[1][Y]);
even[1][X] = min(even[1][X], odd[1][Y]);
} else {
odd[0][X] = min(odd[0][X], odd[0][Y]);
even[0][X] = min(even[0][X], even[0][Y]);
odd[1][X] = min(odd[1][X], odd[1][Y]);
even[1][X] = min(even[1][X], even[1][Y]);
}
sum[X] = sum[X] + sum[Y];
parent[Y] = X;
sz[X] += sz[Y];
res[X] = calc(X);
ret += res[X];
return ret;
}
vector<int> calculate_costs(vector<signed> W, vector<signed> A,
vector<signed> B, vector<signed> E) {
int Q = E.size(), N = W.size(), D, cur = 0, X, Y;
vector<pr> queries, one, two;
vector<tp> arr(N);
parent.resize(N + 1), res.resize(N + 1), sz.resize(N + 1), sum.resize(N + 1);
for (int i = 0; i < 2; i++)
odd[i].resize(N + 1), even[i].resize(N + 1);
for (int i = 0; i < N; i++)
arr[i] = {W[i], A[i] - B[i], B[i]};
sort(arr.begin(), arr.end());
for (int i = 0; i < N; i++) {
parent[i] = i;
sz[i] = 1;
res[i] = arr[i][1] + arr[i][2];
cur += res[i];
sum[i] = arr[i][2];
odd[1][i] = arr[i][1];
odd[0][i] = even[0][i] = even[1][i] = INF;
}
vector<int> R(Q, 0);
for (int i = 1; i < N; i++)
one.push_back({arr[i][0] - arr[i - 1][0], i});
for (int i = 2; i < N; i++)
two.push_back({arr[i][0] - arr[i - 2][0], i});
sort(one.begin(), one.end(), greater<pr>());
sort(two.begin(), two.end(), greater<pr>());
for (int i = 0; i < Q; i++)
queries.push_back({E[i], i});
sort(queries.begin(), queries.end());
for (pr i : queries) {
D = i.first;
while (!one.empty() && one.back().first <= D) {
X = one.back().second, one.pop_back();
cur += merge(X - 1, X);
}
while (!two.empty() && two.back().first <= D) {
X = two.back().second - 1, two.pop_back();
Y = find(X);
cur -= res[Y];
if ((X - Y) & 1)
even[0][Y] = min(even[0][Y], arr[X][1]);
else
odd[0][Y] = min(odd[0][Y], arr[X][1]);
cur += calc(Y);
}
R[i.second] = cur;
}
return R;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
17344 KB |
Output is correct |
2 |
Correct |
41 ms |
19916 KB |
Output is correct |
3 |
Correct |
37 ms |
20004 KB |
Output is correct |
4 |
Correct |
37 ms |
19896 KB |
Output is correct |
5 |
Correct |
37 ms |
20044 KB |
Output is correct |
6 |
Correct |
41 ms |
19916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
17344 KB |
Output is correct |
2 |
Correct |
51 ms |
18940 KB |
Output is correct |
3 |
Correct |
51 ms |
18788 KB |
Output is correct |
4 |
Correct |
56 ms |
18872 KB |
Output is correct |
5 |
Correct |
54 ms |
18872 KB |
Output is correct |
6 |
Correct |
59 ms |
18860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
1 ms |
592 KB |
Output is correct |
9 |
Correct |
2 ms |
592 KB |
Output is correct |
10 |
Correct |
2 ms |
592 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
36 ms |
17344 KB |
Output is correct |
8 |
Correct |
41 ms |
19916 KB |
Output is correct |
9 |
Correct |
37 ms |
20004 KB |
Output is correct |
10 |
Correct |
37 ms |
19896 KB |
Output is correct |
11 |
Correct |
37 ms |
20044 KB |
Output is correct |
12 |
Correct |
41 ms |
19916 KB |
Output is correct |
13 |
Correct |
37 ms |
17344 KB |
Output is correct |
14 |
Correct |
51 ms |
18940 KB |
Output is correct |
15 |
Correct |
51 ms |
18788 KB |
Output is correct |
16 |
Correct |
56 ms |
18872 KB |
Output is correct |
17 |
Correct |
54 ms |
18872 KB |
Output is correct |
18 |
Correct |
59 ms |
18860 KB |
Output is correct |
19 |
Correct |
1 ms |
592 KB |
Output is correct |
20 |
Correct |
1 ms |
592 KB |
Output is correct |
21 |
Correct |
2 ms |
592 KB |
Output is correct |
22 |
Correct |
2 ms |
592 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
592 KB |
Output is correct |
25 |
Correct |
46 ms |
20152 KB |
Output is correct |
26 |
Correct |
73 ms |
20268 KB |
Output is correct |
27 |
Correct |
67 ms |
20400 KB |
Output is correct |
28 |
Correct |
61 ms |
20408 KB |
Output is correct |
29 |
Correct |
60 ms |
20152 KB |
Output is correct |
30 |
Correct |
67 ms |
20224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
17344 KB |
Output is correct |
2 |
Correct |
51 ms |
18940 KB |
Output is correct |
3 |
Correct |
51 ms |
18788 KB |
Output is correct |
4 |
Correct |
56 ms |
18872 KB |
Output is correct |
5 |
Correct |
54 ms |
18872 KB |
Output is correct |
6 |
Correct |
59 ms |
18860 KB |
Output is correct |
7 |
Correct |
64 ms |
22840 KB |
Output is correct |
8 |
Correct |
72 ms |
23096 KB |
Output is correct |
9 |
Correct |
79 ms |
22948 KB |
Output is correct |
10 |
Correct |
87 ms |
22888 KB |
Output is correct |
11 |
Correct |
80 ms |
22956 KB |
Output is correct |
12 |
Correct |
91 ms |
23040 KB |
Output is correct |
13 |
Correct |
83 ms |
23112 KB |
Output is correct |
14 |
Correct |
78 ms |
22796 KB |
Output is correct |
15 |
Correct |
96 ms |
22956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
36 ms |
17344 KB |
Output is correct |
9 |
Correct |
41 ms |
19916 KB |
Output is correct |
10 |
Correct |
37 ms |
20004 KB |
Output is correct |
11 |
Correct |
37 ms |
19896 KB |
Output is correct |
12 |
Correct |
37 ms |
20044 KB |
Output is correct |
13 |
Correct |
41 ms |
19916 KB |
Output is correct |
14 |
Correct |
37 ms |
17344 KB |
Output is correct |
15 |
Correct |
51 ms |
18940 KB |
Output is correct |
16 |
Correct |
51 ms |
18788 KB |
Output is correct |
17 |
Correct |
56 ms |
18872 KB |
Output is correct |
18 |
Correct |
54 ms |
18872 KB |
Output is correct |
19 |
Correct |
59 ms |
18860 KB |
Output is correct |
20 |
Correct |
1 ms |
592 KB |
Output is correct |
21 |
Correct |
1 ms |
592 KB |
Output is correct |
22 |
Correct |
2 ms |
592 KB |
Output is correct |
23 |
Correct |
2 ms |
592 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
592 KB |
Output is correct |
26 |
Correct |
46 ms |
20152 KB |
Output is correct |
27 |
Correct |
73 ms |
20268 KB |
Output is correct |
28 |
Correct |
67 ms |
20400 KB |
Output is correct |
29 |
Correct |
61 ms |
20408 KB |
Output is correct |
30 |
Correct |
60 ms |
20152 KB |
Output is correct |
31 |
Correct |
67 ms |
20224 KB |
Output is correct |
32 |
Correct |
64 ms |
22840 KB |
Output is correct |
33 |
Correct |
72 ms |
23096 KB |
Output is correct |
34 |
Correct |
79 ms |
22948 KB |
Output is correct |
35 |
Correct |
87 ms |
22888 KB |
Output is correct |
36 |
Correct |
80 ms |
22956 KB |
Output is correct |
37 |
Correct |
91 ms |
23040 KB |
Output is correct |
38 |
Correct |
83 ms |
23112 KB |
Output is correct |
39 |
Correct |
78 ms |
22796 KB |
Output is correct |
40 |
Correct |
96 ms |
22956 KB |
Output is correct |
41 |
Correct |
73 ms |
24360 KB |
Output is correct |
42 |
Correct |
99 ms |
24616 KB |
Output is correct |
43 |
Correct |
94 ms |
24628 KB |
Output is correct |
44 |
Correct |
117 ms |
24632 KB |
Output is correct |
45 |
Correct |
93 ms |
24644 KB |
Output is correct |
46 |
Correct |
96 ms |
24620 KB |
Output is correct |
47 |
Correct |
94 ms |
24636 KB |
Output is correct |
48 |
Correct |
86 ms |
24372 KB |
Output is correct |
49 |
Correct |
95 ms |
24640 KB |
Output is correct |