#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;
int n, q;
pair<int, int> a[200001];
int t[200001], segtree[2400001], bit[600001];
vector<pair<int, int>> c;
bool inv[200001], visited[200001];
void build(int node = 1, int l = 0, int r = 2 * n + q - 1) {
if (l == r) segtree[node] = max(c[l].second, 0);
else {
int mid = (l + r) / 2;
build(node * 2, l, mid);
build(node * 2 + 1, mid + 1, r);
segtree[node] = max(segtree[node * 2], segtree[node * 2 + 1]);
}
}
int rmq(int x, int y, int node = 1, int l = 0, int r = 2 * n + q - 1) {
if (l > y || r < x) return 0;
if (l >= x && r <= y) return segtree[node];
int mid = (l + r) / 2;
return max(rmq(x, y, node * 2, l, mid), rmq(x, y, node * 2 + 1, mid + 1, r));
}
void add(int pos) { for (; pos <= q; pos += (pos & (-pos))) bit[pos]++; }
int rsq(int x, int y) {
int ans = 0;
for (; y; y -= (y & (-y))) ans += bit[y];
for (x--; x; x -= (x & (-x))) ans -= bit[x];
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
FOR(i, 1, n + 1) {
cin >> a[i].first >> a[i].second;
inv[i] = a[i].first < a[i].second;
if (inv[i]) swap(a[i].first, a[i].second);
c.push_back({a[i].first, -i});
c.push_back({a[i].second, -i});
}
FOR(i, 1, q + 1) {
cin >> t[i];
c.push_back({t[i], i});
}
sort(c.begin(), c.end());
build();
FOR(i, 1, n + 1) {
a[i].first = lower_bound(c.begin(), c.end(), make_pair(a[i].first, -i)) - c.begin();
a[i].second = lower_bound(c.begin(), c.end(), make_pair(a[i].second, -i)) - c.begin();
}
ll ans = 0;
for (int i = c.size() - 1; i; i--) {
int k = c[i].second;
if (k < 0) {
k *= -1;
if (visited[k]) continue;
visited[k] = true;
int mn = rmq(a[k].second, a[k].first);
int cnt = rsq(mn + 1, q) + (mn == 0 && inv[k]);
if (cnt & 1) ans += c[a[k].second].first;
else ans += c[a[k].first].first;
} else add(k);
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
380 KB |
Output is correct |
2 |
Correct |
6 ms |
376 KB |
Output is correct |
3 |
Correct |
6 ms |
376 KB |
Output is correct |
4 |
Correct |
6 ms |
504 KB |
Output is correct |
5 |
Correct |
7 ms |
376 KB |
Output is correct |
6 |
Correct |
6 ms |
504 KB |
Output is correct |
7 |
Correct |
6 ms |
504 KB |
Output is correct |
8 |
Correct |
6 ms |
504 KB |
Output is correct |
9 |
Correct |
6 ms |
504 KB |
Output is correct |
10 |
Correct |
6 ms |
504 KB |
Output is correct |
11 |
Correct |
6 ms |
632 KB |
Output is correct |
12 |
Correct |
6 ms |
504 KB |
Output is correct |
13 |
Correct |
7 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
380 KB |
Output is correct |
2 |
Correct |
6 ms |
376 KB |
Output is correct |
3 |
Correct |
6 ms |
376 KB |
Output is correct |
4 |
Correct |
6 ms |
504 KB |
Output is correct |
5 |
Correct |
7 ms |
376 KB |
Output is correct |
6 |
Correct |
6 ms |
504 KB |
Output is correct |
7 |
Correct |
6 ms |
504 KB |
Output is correct |
8 |
Correct |
6 ms |
504 KB |
Output is correct |
9 |
Correct |
6 ms |
504 KB |
Output is correct |
10 |
Correct |
6 ms |
504 KB |
Output is correct |
11 |
Correct |
6 ms |
632 KB |
Output is correct |
12 |
Correct |
6 ms |
504 KB |
Output is correct |
13 |
Correct |
7 ms |
504 KB |
Output is correct |
14 |
Correct |
19 ms |
1504 KB |
Output is correct |
15 |
Correct |
37 ms |
2548 KB |
Output is correct |
16 |
Correct |
53 ms |
3564 KB |
Output is correct |
17 |
Correct |
68 ms |
4340 KB |
Output is correct |
18 |
Correct |
69 ms |
4336 KB |
Output is correct |
19 |
Correct |
69 ms |
4336 KB |
Output is correct |
20 |
Correct |
65 ms |
4336 KB |
Output is correct |
21 |
Correct |
64 ms |
4336 KB |
Output is correct |
22 |
Correct |
50 ms |
3820 KB |
Output is correct |
23 |
Correct |
51 ms |
3824 KB |
Output is correct |
24 |
Correct |
51 ms |
3952 KB |
Output is correct |
25 |
Correct |
52 ms |
3872 KB |
Output is correct |
26 |
Correct |
62 ms |
4080 KB |
Output is correct |
27 |
Correct |
62 ms |
4336 KB |
Output is correct |
28 |
Correct |
61 ms |
4332 KB |
Output is correct |
29 |
Correct |
62 ms |
4460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
380 KB |
Output is correct |
2 |
Correct |
6 ms |
376 KB |
Output is correct |
3 |
Correct |
6 ms |
376 KB |
Output is correct |
4 |
Correct |
6 ms |
504 KB |
Output is correct |
5 |
Correct |
7 ms |
376 KB |
Output is correct |
6 |
Correct |
6 ms |
504 KB |
Output is correct |
7 |
Correct |
6 ms |
504 KB |
Output is correct |
8 |
Correct |
6 ms |
504 KB |
Output is correct |
9 |
Correct |
6 ms |
504 KB |
Output is correct |
10 |
Correct |
6 ms |
504 KB |
Output is correct |
11 |
Correct |
6 ms |
632 KB |
Output is correct |
12 |
Correct |
6 ms |
504 KB |
Output is correct |
13 |
Correct |
7 ms |
504 KB |
Output is correct |
14 |
Correct |
19 ms |
1504 KB |
Output is correct |
15 |
Correct |
37 ms |
2548 KB |
Output is correct |
16 |
Correct |
53 ms |
3564 KB |
Output is correct |
17 |
Correct |
68 ms |
4340 KB |
Output is correct |
18 |
Correct |
69 ms |
4336 KB |
Output is correct |
19 |
Correct |
69 ms |
4336 KB |
Output is correct |
20 |
Correct |
65 ms |
4336 KB |
Output is correct |
21 |
Correct |
64 ms |
4336 KB |
Output is correct |
22 |
Correct |
50 ms |
3820 KB |
Output is correct |
23 |
Correct |
51 ms |
3824 KB |
Output is correct |
24 |
Correct |
51 ms |
3952 KB |
Output is correct |
25 |
Correct |
52 ms |
3872 KB |
Output is correct |
26 |
Correct |
62 ms |
4080 KB |
Output is correct |
27 |
Correct |
62 ms |
4336 KB |
Output is correct |
28 |
Correct |
61 ms |
4332 KB |
Output is correct |
29 |
Correct |
62 ms |
4460 KB |
Output is correct |
30 |
Correct |
81 ms |
8040 KB |
Output is correct |
31 |
Correct |
140 ms |
11872 KB |
Output is correct |
32 |
Correct |
219 ms |
14204 KB |
Output is correct |
33 |
Correct |
379 ms |
22740 KB |
Output is correct |
34 |
Correct |
66 ms |
7656 KB |
Output is correct |
35 |
Correct |
373 ms |
22948 KB |
Output is correct |
36 |
Correct |
371 ms |
22740 KB |
Output is correct |
37 |
Correct |
371 ms |
22740 KB |
Output is correct |
38 |
Correct |
357 ms |
22740 KB |
Output is correct |
39 |
Correct |
354 ms |
22708 KB |
Output is correct |
40 |
Correct |
349 ms |
22480 KB |
Output is correct |
41 |
Correct |
351 ms |
22744 KB |
Output is correct |
42 |
Correct |
349 ms |
22740 KB |
Output is correct |
43 |
Correct |
257 ms |
22028 KB |
Output is correct |
44 |
Correct |
242 ms |
21972 KB |
Output is correct |
45 |
Correct |
251 ms |
21952 KB |
Output is correct |
46 |
Correct |
257 ms |
20820 KB |
Output is correct |
47 |
Correct |
259 ms |
20828 KB |
Output is correct |
48 |
Correct |
322 ms |
22776 KB |
Output is correct |
49 |
Correct |
329 ms |
22748 KB |
Output is correct |