#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MAXN = 1e5 + 5;
int max_val[MAXN] = {0};
int sz[MAXN] = {0};
int parent[MAXN] = {0};
int value[MAXN][2] = {0};
int find(int r) {
return parent[r] == r ? r : parent[r] = find(parent[r]);
}
int calculate(int u) {
if (sz[u] % 2 == 0) return 0;
return max(value[u][u & 1], max_val[u]);
}
vector<long long> calculate_costs(vector<int> W, vector<int> A, vector<int> B, vector<int> E) {
int n = W.size();
int m = E.size();
vector<pair<int, pii>> v;
for (int i = 0; i < n; i++) {
v.push_back({W[i], {A[i], B[i]}});
}
sort(v.begin(), v.end());
for (int i = 0; i < n; i++) {
W[i] = v[i].first;
A[i] = v[i].second.first;
B[i] = v[i].second.second;
}
vector<pii> q1, q2;
for (int i = 0; i < n; i++) {
parent[i] = i;
sz[i] = 1;
value[i][i & 1] = B[i] - A[i];
max_val[i] = -2e9;
value[i][(i + 1) & 1] = -2e9;
}
for (int i = 0; i < n - 1; i++) {
q1.push_back({W[i + 1] - W[i], i});
}
for (int i = 1; i < n - 1; i++) {
q2.push_back({W[i + 1] - W[i - 1], i});
}
sort(q1.begin(), q1.end());
sort(q2.begin(), q2.end());
ll result_sum = 0;
for (int i = 0; i < n; i++) {
result_sum += A[i];
}
vector<pii> queries;
for (int i = 0; i < m; i++) {
queries.push_back({E[i], i});
}
sort(queries.begin(), queries.end());
int id1 = 0, id2 = 0;
vector<ll> res(m, 0);
for (auto [d, i] : queries) {
while (id1 < q1.size() && q1[id1].first <= d) {
int u = find(q1[id1].second), v = find(q1[id1].second + 1);
result_sum += calculate(u) + calculate(v);
parent[u] = v;
sz[v] += sz[u];
value[v][0] = max(value[v][0], value[u][0]);
value[v][1] = max(value[v][1], value[u][1]);
max_val[v] = max(max_val[v], max_val[u]);
result_sum -= calculate(v);
id1++;
}
while (id2 < q2.size() && q2[id2].first <= d) {
int u = find(q2[id2].second);
result_sum += calculate(u);
max_val[u] = max(max_val[u], B[q2[id2].second] - A[q2[id2].second]);
result_sum -= calculate(u);
id2++;
}
res[i] = result_sum;
}
return res;
}
Compilation message
nile.cpp: In function 'std::vector<long long int> calculate_costs(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
nile.cpp:70:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | while (id1 < q1.size() && q1[id1].first <= d) {
| ~~~~^~~~~~~~~~~
nile.cpp:81:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | while (id2 < q2.size() && q2[id2].first <= d) {
| ~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
592 KB |
Output is correct |
2 |
Correct |
2 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
2 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 |
34 ms |
9684 KB |
Output is correct |
2 |
Correct |
39 ms |
9664 KB |
Output is correct |
3 |
Correct |
36 ms |
9672 KB |
Output is correct |
4 |
Correct |
36 ms |
9664 KB |
Output is correct |
5 |
Correct |
37 ms |
9664 KB |
Output is correct |
6 |
Correct |
47 ms |
9672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
9152 KB |
Output is correct |
2 |
Correct |
36 ms |
9160 KB |
Output is correct |
3 |
Correct |
46 ms |
8392 KB |
Output is correct |
4 |
Correct |
44 ms |
8392 KB |
Output is correct |
5 |
Correct |
47 ms |
8896 KB |
Output is correct |
6 |
Correct |
56 ms |
8384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
592 KB |
Output is correct |
2 |
Correct |
2 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
2 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 |
2 ms |
592 KB |
Output is correct |
8 |
Correct |
2 ms |
592 KB |
Output is correct |
9 |
Correct |
2 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
592 KB |
Output is correct |
2 |
Correct |
2 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
592 KB |
Output is correct |
4 |
Correct |
2 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 |
34 ms |
9684 KB |
Output is correct |
8 |
Correct |
39 ms |
9664 KB |
Output is correct |
9 |
Correct |
36 ms |
9672 KB |
Output is correct |
10 |
Correct |
36 ms |
9664 KB |
Output is correct |
11 |
Correct |
37 ms |
9664 KB |
Output is correct |
12 |
Correct |
47 ms |
9672 KB |
Output is correct |
13 |
Correct |
33 ms |
9152 KB |
Output is correct |
14 |
Correct |
36 ms |
9160 KB |
Output is correct |
15 |
Correct |
46 ms |
8392 KB |
Output is correct |
16 |
Correct |
44 ms |
8392 KB |
Output is correct |
17 |
Correct |
47 ms |
8896 KB |
Output is correct |
18 |
Correct |
56 ms |
8384 KB |
Output is correct |
19 |
Correct |
2 ms |
592 KB |
Output is correct |
20 |
Correct |
2 ms |
592 KB |
Output is correct |
21 |
Correct |
2 ms |
592 KB |
Output is correct |
22 |
Correct |
1 ms |
592 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
760 KB |
Output is correct |
25 |
Correct |
39 ms |
8908 KB |
Output is correct |
26 |
Correct |
41 ms |
8964 KB |
Output is correct |
27 |
Correct |
53 ms |
8384 KB |
Output is correct |
28 |
Correct |
52 ms |
8396 KB |
Output is correct |
29 |
Correct |
50 ms |
8904 KB |
Output is correct |
30 |
Correct |
54 ms |
8416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
9152 KB |
Output is correct |
2 |
Correct |
36 ms |
9160 KB |
Output is correct |
3 |
Correct |
46 ms |
8392 KB |
Output is correct |
4 |
Correct |
44 ms |
8392 KB |
Output is correct |
5 |
Correct |
47 ms |
8896 KB |
Output is correct |
6 |
Correct |
56 ms |
8384 KB |
Output is correct |
7 |
Correct |
57 ms |
10688 KB |
Output is correct |
8 |
Correct |
56 ms |
12492 KB |
Output is correct |
9 |
Correct |
68 ms |
11968 KB |
Output is correct |
10 |
Correct |
77 ms |
11832 KB |
Output is correct |
11 |
Correct |
70 ms |
11988 KB |
Output is correct |
12 |
Correct |
68 ms |
11968 KB |
Output is correct |
13 |
Correct |
72 ms |
11968 KB |
Output is correct |
14 |
Correct |
71 ms |
12016 KB |
Output is correct |
15 |
Correct |
73 ms |
11972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
592 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
2 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 |
34 ms |
9684 KB |
Output is correct |
9 |
Correct |
39 ms |
9664 KB |
Output is correct |
10 |
Correct |
36 ms |
9672 KB |
Output is correct |
11 |
Correct |
36 ms |
9664 KB |
Output is correct |
12 |
Correct |
37 ms |
9664 KB |
Output is correct |
13 |
Correct |
47 ms |
9672 KB |
Output is correct |
14 |
Correct |
33 ms |
9152 KB |
Output is correct |
15 |
Correct |
36 ms |
9160 KB |
Output is correct |
16 |
Correct |
46 ms |
8392 KB |
Output is correct |
17 |
Correct |
44 ms |
8392 KB |
Output is correct |
18 |
Correct |
47 ms |
8896 KB |
Output is correct |
19 |
Correct |
56 ms |
8384 KB |
Output is correct |
20 |
Correct |
2 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 |
1 ms |
592 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
760 KB |
Output is correct |
26 |
Correct |
39 ms |
8908 KB |
Output is correct |
27 |
Correct |
41 ms |
8964 KB |
Output is correct |
28 |
Correct |
53 ms |
8384 KB |
Output is correct |
29 |
Correct |
52 ms |
8396 KB |
Output is correct |
30 |
Correct |
50 ms |
8904 KB |
Output is correct |
31 |
Correct |
54 ms |
8416 KB |
Output is correct |
32 |
Correct |
57 ms |
10688 KB |
Output is correct |
33 |
Correct |
56 ms |
12492 KB |
Output is correct |
34 |
Correct |
68 ms |
11968 KB |
Output is correct |
35 |
Correct |
77 ms |
11832 KB |
Output is correct |
36 |
Correct |
70 ms |
11988 KB |
Output is correct |
37 |
Correct |
68 ms |
11968 KB |
Output is correct |
38 |
Correct |
72 ms |
11968 KB |
Output is correct |
39 |
Correct |
71 ms |
12016 KB |
Output is correct |
40 |
Correct |
73 ms |
11972 KB |
Output is correct |
41 |
Correct |
64 ms |
13760 KB |
Output is correct |
42 |
Correct |
66 ms |
14024 KB |
Output is correct |
43 |
Correct |
86 ms |
13520 KB |
Output is correct |
44 |
Correct |
78 ms |
13504 KB |
Output is correct |
45 |
Correct |
82 ms |
13504 KB |
Output is correct |
46 |
Correct |
76 ms |
13512 KB |
Output is correct |
47 |
Correct |
78 ms |
13500 KB |
Output is correct |
48 |
Correct |
80 ms |
13480 KB |
Output is correct |
49 |
Correct |
80 ms |
13368 KB |
Output is correct |