#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
struct SegmentTree {
int n;
vector <int> t;
SegmentTree(int nn) {
n = nn;
t.resize(4 * n, -1);
}
void modify(int i, int val, int l, int r, int v) {
if (l == r) {
t[v] = max(t[v], val);
} else {
int m = (l + r) >> 1;
if (i <= m) modify(i, val, l, m, 2 * v + 1);
else modify(i, val, m + 1, r, 2 * v + 2);
t[v] = max(t[2 * v + 1], t[2 * v + 2]);
}
}
int get(int ql, int qr, int l, int r, int v) {
if (qr < l || r < ql) return -1;
if (ql <= l && r <= qr) return t[v];
int m = (l + r) >> 1;
return max(get(ql, qr, l, m, 2 * v + 1), get(ql, qr, m + 1, r, 2 * v + 2));
}
};
struct Fenwick {
int n;
vector <int> f;
Fenwick(int nn) {
n = nn;
f.resize(n);
}
void modify(int i, int val) {
for (; i < n; i |= i + 1) {
f[i] += val;
}
}
int get(int r) {
int res = 0;
for (; r >= 0; r &= r + 1, --r) {
res += f[r];
}
return res;
}
int sum(int l, int r) {
return get(r) - get(l - 1);
}
};
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, k;
cin >> n >> k;
vector <int> a(n), b(n);
vector <int> c;
for (int i = 0; i < n; ++i) {
cin >> a[i] >> b[i];
c.push_back(a[i]);
c.push_back(b[i]);
}
vector <int> t(k);
for (int i = 0; i < k; ++i) {
cin >> t[i];
c.push_back(t[i]);
}
sort(c.begin(), c.end());
c.resize(unique(c.begin(), c.end()) - c.begin());
for (int i = 0; i < n; ++i) {
a[i] = lower_bound(c.begin(), c.end(), a[i]) - c.begin();
b[i] = lower_bound(c.begin(), c.end(), b[i]) - c.begin();
}
for (int i = 0; i < k; ++i) {
t[i] = lower_bound(c.begin(), c.end(), t[i]) - c.begin();
}
int m = c.size();
SegmentTree st(m);
for (int i = 0; i < k; ++i) {
st.modify(t[i], i, 0, m - 1, 0);
}
vector <int> when(n, -1);
for (int i = 0; i < n; ++i) {
int mn = min(a[i], b[i]);
int mx = max(a[i], b[i]);
if (mn < mx) {
when[i] = st.get(mn, mx - 1, 0, m - 1, 0);
}
}
vector <vector <pair <int, int>>> who(k);
vector <int> gogo(n);
ll ans = 0;
for (int i = 0; i < n; ++i) {
gogo[i] = 0;
int go = when[i] + 1;
if (when[i] != -1 && b[i] > a[i]) {
gogo[i] = 1;
}
if (go < k) {
who[go].push_back({max(a[i], b[i]), i});
}
}
Fenwick f(m);
for (int i = k - 1; i >= 0; --i) {
f.modify(t[i], 1);
for (auto p : who[i]) {
gogo[p.second] ^= (f.sum(p.first, m - 1) % 2);
}
}
for (int i = 0; i < n; ++i) {
if (gogo[i] == 0) ans += c[a[i]];
else ans += c[b[i]];
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
16 ms |
1536 KB |
Output is correct |
15 |
Correct |
33 ms |
2808 KB |
Output is correct |
16 |
Correct |
51 ms |
3952 KB |
Output is correct |
17 |
Correct |
71 ms |
5236 KB |
Output is correct |
18 |
Correct |
73 ms |
5108 KB |
Output is correct |
19 |
Correct |
67 ms |
5232 KB |
Output is correct |
20 |
Correct |
68 ms |
5436 KB |
Output is correct |
21 |
Correct |
67 ms |
5052 KB |
Output is correct |
22 |
Correct |
48 ms |
4340 KB |
Output is correct |
23 |
Correct |
49 ms |
3956 KB |
Output is correct |
24 |
Correct |
48 ms |
3956 KB |
Output is correct |
25 |
Correct |
48 ms |
4336 KB |
Output is correct |
26 |
Correct |
50 ms |
4212 KB |
Output is correct |
27 |
Correct |
62 ms |
4596 KB |
Output is correct |
28 |
Correct |
53 ms |
4596 KB |
Output is correct |
29 |
Correct |
61 ms |
5204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
16 ms |
1536 KB |
Output is correct |
15 |
Correct |
33 ms |
2808 KB |
Output is correct |
16 |
Correct |
51 ms |
3952 KB |
Output is correct |
17 |
Correct |
71 ms |
5236 KB |
Output is correct |
18 |
Correct |
73 ms |
5108 KB |
Output is correct |
19 |
Correct |
67 ms |
5232 KB |
Output is correct |
20 |
Correct |
68 ms |
5436 KB |
Output is correct |
21 |
Correct |
67 ms |
5052 KB |
Output is correct |
22 |
Correct |
48 ms |
4340 KB |
Output is correct |
23 |
Correct |
49 ms |
3956 KB |
Output is correct |
24 |
Correct |
48 ms |
3956 KB |
Output is correct |
25 |
Correct |
48 ms |
4336 KB |
Output is correct |
26 |
Correct |
50 ms |
4212 KB |
Output is correct |
27 |
Correct |
62 ms |
4596 KB |
Output is correct |
28 |
Correct |
53 ms |
4596 KB |
Output is correct |
29 |
Correct |
61 ms |
5204 KB |
Output is correct |
30 |
Correct |
137 ms |
13420 KB |
Output is correct |
31 |
Correct |
207 ms |
17004 KB |
Output is correct |
32 |
Correct |
255 ms |
21224 KB |
Output is correct |
33 |
Correct |
409 ms |
30180 KB |
Output is correct |
34 |
Correct |
126 ms |
12712 KB |
Output is correct |
35 |
Correct |
409 ms |
30988 KB |
Output is correct |
36 |
Correct |
407 ms |
30016 KB |
Output is correct |
37 |
Correct |
414 ms |
30820 KB |
Output is correct |
38 |
Correct |
402 ms |
31148 KB |
Output is correct |
39 |
Correct |
411 ms |
30952 KB |
Output is correct |
40 |
Correct |
405 ms |
28944 KB |
Output is correct |
41 |
Correct |
404 ms |
30820 KB |
Output is correct |
42 |
Correct |
421 ms |
30692 KB |
Output is correct |
43 |
Correct |
276 ms |
28260 KB |
Output is correct |
44 |
Correct |
275 ms |
28532 KB |
Output is correct |
45 |
Correct |
280 ms |
28132 KB |
Output is correct |
46 |
Correct |
284 ms |
22372 KB |
Output is correct |
47 |
Correct |
288 ms |
21264 KB |
Output is correct |
48 |
Correct |
341 ms |
27240 KB |
Output is correct |
49 |
Correct |
353 ms |
27620 KB |
Output is correct |