#include <algorithm>
#include <cstring>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#include <vector>
using namespace std;
using namespace __gnu_pbds;
#define pow2(p) (1 << (p))
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
int n, k;
vector<int> a, b, t, flips;
void makePancakes() {
vector<int> t_sorted = t;
sort(t_sorted.begin(), t_sorted.end());
for (int i = 0; i < n; i++) {
flips[i] +=
t_sorted.end() -
lower_bound(t_sorted.begin(), t_sorted.end(), max(a[i], b[i]));
}
}
void adjustFlips() {
vector<pair<int, int>> tp(k);
for (int i = 0; i < k; i++) {
tp[i].first = t[i];
tp[i].second = i;
}
sort(tp.begin(), tp.end());
int st[k][20];
memset(st, 0, sizeof(st));
for (int i = 0; i < k; i++) {
st[i][0] = tp[i].second;
}
for (int j = 1; j < 20; j++) {
for (int i = 0; i + (1 << j) <= k; i++) {
st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
int log2[200005];
log2[1] = 0;
for (int i = 2; i < 200005; i++) {
log2[i] = log2[i / 2] + 1;
}
vector<pair<int, int>> p(n);
for (int i = 0; i < n; i++) {
p[i].second = i;
p[i].first = -1;
int l =
lower_bound(tp.begin(), tp.end(), make_pair(min(a[i], b[i]), -1)) -
tp.begin();
int r =
lower_bound(tp.begin(), tp.end(), make_pair(max(a[i], b[i]), -1)) -
tp.begin();
if (l == r) continue;
r--;
flips[i] += (b[i] > a[i]);
int j = log2[r - l + 1];
p[i].first = max(st[l][j], st[r - (1 << j) + 1][j]);
}
sort(p.begin(), p.end());
int ti = 0, pi = 0;
while (p[pi].first == -1) pi++;
ordered_set os;
while (ti < k) {
os.insert({t[ti], ti});
while (pi < n and p[pi].first == ti) {
flips[p[pi].second] -=
os.size() -
os.order_of_key({max(a[p[pi].second], b[p[pi].second]), -1});
pi++;
}
ti++;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
a.resize(n), b.resize(n), flips.resize(n);
t.resize(k);
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
}
for (int i = 0; i < k; i++) {
cin >> t[i];
}
makePancakes();
adjustFlips();
long long ans = 0;
for (int i = 0; i < n; i++) {
if (flips[i] % 2)
ans += b[i];
else
ans += a[i];
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1228 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
2 ms |
1228 KB |
Output is correct |
4 |
Correct |
2 ms |
1188 KB |
Output is correct |
5 |
Correct |
2 ms |
1228 KB |
Output is correct |
6 |
Correct |
2 ms |
1228 KB |
Output is correct |
7 |
Correct |
2 ms |
1228 KB |
Output is correct |
8 |
Correct |
2 ms |
1228 KB |
Output is correct |
9 |
Correct |
2 ms |
1228 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
1228 KB |
Output is correct |
13 |
Correct |
2 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1228 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
2 ms |
1228 KB |
Output is correct |
4 |
Correct |
2 ms |
1188 KB |
Output is correct |
5 |
Correct |
2 ms |
1228 KB |
Output is correct |
6 |
Correct |
2 ms |
1228 KB |
Output is correct |
7 |
Correct |
2 ms |
1228 KB |
Output is correct |
8 |
Correct |
2 ms |
1228 KB |
Output is correct |
9 |
Correct |
2 ms |
1228 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
1228 KB |
Output is correct |
13 |
Correct |
2 ms |
1228 KB |
Output is correct |
14 |
Correct |
12 ms |
2764 KB |
Output is correct |
15 |
Correct |
25 ms |
4540 KB |
Output is correct |
16 |
Correct |
39 ms |
6148 KB |
Output is correct |
17 |
Correct |
52 ms |
7964 KB |
Output is correct |
18 |
Correct |
53 ms |
7868 KB |
Output is correct |
19 |
Correct |
52 ms |
7980 KB |
Output is correct |
20 |
Correct |
56 ms |
7980 KB |
Output is correct |
21 |
Correct |
48 ms |
7920 KB |
Output is correct |
22 |
Correct |
38 ms |
7868 KB |
Output is correct |
23 |
Correct |
39 ms |
7972 KB |
Output is correct |
24 |
Correct |
40 ms |
7972 KB |
Output is correct |
25 |
Correct |
37 ms |
7936 KB |
Output is correct |
26 |
Correct |
48 ms |
7752 KB |
Output is correct |
27 |
Correct |
49 ms |
7968 KB |
Output is correct |
28 |
Correct |
52 ms |
8024 KB |
Output is correct |
29 |
Correct |
48 ms |
7964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1228 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
2 ms |
1228 KB |
Output is correct |
4 |
Correct |
2 ms |
1188 KB |
Output is correct |
5 |
Correct |
2 ms |
1228 KB |
Output is correct |
6 |
Correct |
2 ms |
1228 KB |
Output is correct |
7 |
Correct |
2 ms |
1228 KB |
Output is correct |
8 |
Correct |
2 ms |
1228 KB |
Output is correct |
9 |
Correct |
2 ms |
1228 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
1228 KB |
Output is correct |
13 |
Correct |
2 ms |
1228 KB |
Output is correct |
14 |
Correct |
12 ms |
2764 KB |
Output is correct |
15 |
Correct |
25 ms |
4540 KB |
Output is correct |
16 |
Correct |
39 ms |
6148 KB |
Output is correct |
17 |
Correct |
52 ms |
7964 KB |
Output is correct |
18 |
Correct |
53 ms |
7868 KB |
Output is correct |
19 |
Correct |
52 ms |
7980 KB |
Output is correct |
20 |
Correct |
56 ms |
7980 KB |
Output is correct |
21 |
Correct |
48 ms |
7920 KB |
Output is correct |
22 |
Correct |
38 ms |
7868 KB |
Output is correct |
23 |
Correct |
39 ms |
7972 KB |
Output is correct |
24 |
Correct |
40 ms |
7972 KB |
Output is correct |
25 |
Correct |
37 ms |
7936 KB |
Output is correct |
26 |
Correct |
48 ms |
7752 KB |
Output is correct |
27 |
Correct |
49 ms |
7968 KB |
Output is correct |
28 |
Correct |
52 ms |
8024 KB |
Output is correct |
29 |
Correct |
48 ms |
7964 KB |
Output is correct |
30 |
Correct |
219 ms |
31680 KB |
Output is correct |
31 |
Correct |
254 ms |
32600 KB |
Output is correct |
32 |
Correct |
310 ms |
37336 KB |
Output is correct |
33 |
Correct |
387 ms |
41268 KB |
Output is correct |
34 |
Correct |
183 ms |
33436 KB |
Output is correct |
35 |
Correct |
405 ms |
41468 KB |
Output is correct |
36 |
Correct |
397 ms |
41428 KB |
Output is correct |
37 |
Correct |
390 ms |
41428 KB |
Output is correct |
38 |
Correct |
369 ms |
41388 KB |
Output is correct |
39 |
Correct |
396 ms |
41304 KB |
Output is correct |
40 |
Correct |
330 ms |
41044 KB |
Output is correct |
41 |
Correct |
427 ms |
41240 KB |
Output is correct |
42 |
Correct |
416 ms |
41344 KB |
Output is correct |
43 |
Correct |
307 ms |
40668 KB |
Output is correct |
44 |
Correct |
301 ms |
40568 KB |
Output is correct |
45 |
Correct |
277 ms |
40536 KB |
Output is correct |
46 |
Correct |
312 ms |
39464 KB |
Output is correct |
47 |
Correct |
284 ms |
39348 KB |
Output is correct |
48 |
Correct |
321 ms |
41292 KB |
Output is correct |
49 |
Correct |
324 ms |
41304 KB |
Output is correct |