#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
const int N = 1 << 18, inf = 2e9;
int mn[N << 1]; bool s[N << 1];
int q, p, x;
void upd(int v, int l, int r) {
if (r - l == 1) return mn[v] = x, void(s[v] = 1);
int m = (l + r) >> 1;
if (p < m) upd(v << 1, l, m);
else upd(v << 1|1, m, r);
mn[v] = min(mn[v << 1], mn[v << 1|1]);
s[v] = s[v << 1] ^ s[v << 1|1];
}
void upd(int a, int b) { p = a; x = b; upd(1, 0, q); }
int find(int v, int l, int r) {
if (x <= mn[v]) return 0;
if (r - l == 1) return r;
int m = (l + r) >> 1;
int res = find(v << 1|1, m, r);
if (res) return res;
return find(v << 1, l, m);
}
int find(int a) { x = a; return find(1, 0, q); }
int lo, hi;
bool sum(int v, int l, int r) {
if (hi <= l || r <= lo) return 0;
if (lo <= l && r <= hi) return s[v];
int m = (l + r) >> 1;
return sum(v << 1, l, m) ^ sum(v << 1|1, m, r);
}
bool sum(int l, int r) { lo = l; hi = r; return sum(1, 0, q); }
array<int, 2> a[N];
int t[N], ord[N];
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
fill(mn, mn + 2 * N, inf);
int n; cin >> n >> q;
rep(i, 0, n) cin >> a[i][0] >> a[i][1];
rep(i, 0, q) cin >> t[i], ord[i] = i;
sort(a, a + n, [](const auto &x, const auto &y) { return min(x[0], x[1]) > min(y[0], y[1]); });
sort(ord, ord + q, [&](int x, int y) { return t[x] > t[y]; });
int64_t ans = 0;
for (int i = 0, j = 0; i < n; ++i) {
bool b = 0; if (a[i][0] < a[i][1]) swap(a[i][0], a[i][1]), b = 1;
while (j < q && a[i][1] <= t[ord[j]]) upd(ord[j], t[ord[j]]), ++j;
int p = find(a[i][0]);
ans += a[i][(!p && b) ^ sum(p, q)];
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2432 KB |
Output is correct |
2 |
Correct |
2 ms |
2432 KB |
Output is correct |
3 |
Correct |
2 ms |
2432 KB |
Output is correct |
4 |
Correct |
2 ms |
2432 KB |
Output is correct |
5 |
Correct |
3 ms |
2432 KB |
Output is correct |
6 |
Correct |
2 ms |
2432 KB |
Output is correct |
7 |
Correct |
4 ms |
2432 KB |
Output is correct |
8 |
Correct |
3 ms |
2432 KB |
Output is correct |
9 |
Correct |
2 ms |
2432 KB |
Output is correct |
10 |
Correct |
2 ms |
2432 KB |
Output is correct |
11 |
Correct |
3 ms |
2432 KB |
Output is correct |
12 |
Correct |
2 ms |
2432 KB |
Output is correct |
13 |
Correct |
3 ms |
2432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2432 KB |
Output is correct |
2 |
Correct |
2 ms |
2432 KB |
Output is correct |
3 |
Correct |
2 ms |
2432 KB |
Output is correct |
4 |
Correct |
2 ms |
2432 KB |
Output is correct |
5 |
Correct |
3 ms |
2432 KB |
Output is correct |
6 |
Correct |
2 ms |
2432 KB |
Output is correct |
7 |
Correct |
4 ms |
2432 KB |
Output is correct |
8 |
Correct |
3 ms |
2432 KB |
Output is correct |
9 |
Correct |
2 ms |
2432 KB |
Output is correct |
10 |
Correct |
2 ms |
2432 KB |
Output is correct |
11 |
Correct |
3 ms |
2432 KB |
Output is correct |
12 |
Correct |
2 ms |
2432 KB |
Output is correct |
13 |
Correct |
3 ms |
2432 KB |
Output is correct |
14 |
Correct |
10 ms |
2584 KB |
Output is correct |
15 |
Correct |
19 ms |
2816 KB |
Output is correct |
16 |
Correct |
28 ms |
2944 KB |
Output is correct |
17 |
Correct |
38 ms |
3192 KB |
Output is correct |
18 |
Correct |
37 ms |
3064 KB |
Output is correct |
19 |
Correct |
38 ms |
3200 KB |
Output is correct |
20 |
Correct |
43 ms |
3192 KB |
Output is correct |
21 |
Correct |
36 ms |
3192 KB |
Output is correct |
22 |
Correct |
29 ms |
3200 KB |
Output is correct |
23 |
Correct |
30 ms |
3192 KB |
Output is correct |
24 |
Correct |
31 ms |
3200 KB |
Output is correct |
25 |
Correct |
27 ms |
3200 KB |
Output is correct |
26 |
Correct |
32 ms |
3072 KB |
Output is correct |
27 |
Correct |
45 ms |
3320 KB |
Output is correct |
28 |
Correct |
35 ms |
3200 KB |
Output is correct |
29 |
Correct |
36 ms |
3192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2432 KB |
Output is correct |
2 |
Correct |
2 ms |
2432 KB |
Output is correct |
3 |
Correct |
2 ms |
2432 KB |
Output is correct |
4 |
Correct |
2 ms |
2432 KB |
Output is correct |
5 |
Correct |
3 ms |
2432 KB |
Output is correct |
6 |
Correct |
2 ms |
2432 KB |
Output is correct |
7 |
Correct |
4 ms |
2432 KB |
Output is correct |
8 |
Correct |
3 ms |
2432 KB |
Output is correct |
9 |
Correct |
2 ms |
2432 KB |
Output is correct |
10 |
Correct |
2 ms |
2432 KB |
Output is correct |
11 |
Correct |
3 ms |
2432 KB |
Output is correct |
12 |
Correct |
2 ms |
2432 KB |
Output is correct |
13 |
Correct |
3 ms |
2432 KB |
Output is correct |
14 |
Correct |
10 ms |
2584 KB |
Output is correct |
15 |
Correct |
19 ms |
2816 KB |
Output is correct |
16 |
Correct |
28 ms |
2944 KB |
Output is correct |
17 |
Correct |
38 ms |
3192 KB |
Output is correct |
18 |
Correct |
37 ms |
3064 KB |
Output is correct |
19 |
Correct |
38 ms |
3200 KB |
Output is correct |
20 |
Correct |
43 ms |
3192 KB |
Output is correct |
21 |
Correct |
36 ms |
3192 KB |
Output is correct |
22 |
Correct |
29 ms |
3200 KB |
Output is correct |
23 |
Correct |
30 ms |
3192 KB |
Output is correct |
24 |
Correct |
31 ms |
3200 KB |
Output is correct |
25 |
Correct |
27 ms |
3200 KB |
Output is correct |
26 |
Correct |
32 ms |
3072 KB |
Output is correct |
27 |
Correct |
45 ms |
3320 KB |
Output is correct |
28 |
Correct |
35 ms |
3200 KB |
Output is correct |
29 |
Correct |
36 ms |
3192 KB |
Output is correct |
30 |
Correct |
101 ms |
4600 KB |
Output is correct |
31 |
Correct |
118 ms |
4984 KB |
Output is correct |
32 |
Correct |
158 ms |
5496 KB |
Output is correct |
33 |
Correct |
201 ms |
6136 KB |
Output is correct |
34 |
Correct |
94 ms |
4600 KB |
Output is correct |
35 |
Correct |
209 ms |
6136 KB |
Output is correct |
36 |
Correct |
210 ms |
6232 KB |
Output is correct |
37 |
Correct |
211 ms |
6136 KB |
Output is correct |
38 |
Correct |
208 ms |
6136 KB |
Output is correct |
39 |
Correct |
209 ms |
6264 KB |
Output is correct |
40 |
Correct |
182 ms |
6136 KB |
Output is correct |
41 |
Correct |
224 ms |
6136 KB |
Output is correct |
42 |
Correct |
216 ms |
6236 KB |
Output is correct |
43 |
Correct |
161 ms |
6132 KB |
Output is correct |
44 |
Correct |
163 ms |
6264 KB |
Output is correct |
45 |
Correct |
190 ms |
6396 KB |
Output is correct |
46 |
Correct |
170 ms |
6136 KB |
Output is correct |
47 |
Correct |
180 ms |
6264 KB |
Output is correct |
48 |
Correct |
200 ms |
6264 KB |
Output is correct |
49 |
Correct |
180 ms |
6136 KB |
Output is correct |