#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cassert>
#include <string>
#include <set>
#include <map>
#include <random>
#include <bitset>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;
const int N = 1 << 19;
int a[N], b[N], t[N], tr[N * 2], f[N];
vector<pair<int, int>> lq[N];
int getm(int cl, int cr, int v=1, int l=0, int r=N) {
if (cr <= l || r <= cl)
return -1;
if (cl <= l && r <= cr)
return tr[v];
return max(getm(cl, cr, v * 2, l, (l + r) / 2), getm(cl, cr, v * 2 + 1, (l + r) / 2, r));
}
void add(int p) {
for (int i = p; i < N; i |= (i + 1))
f[i]++;
}
int sum(int p) {
int s = 0;
for (int i = p; i; i &= (i - 1))
s += f[i - 1];
return s;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> li;
rep(i, n) {
cin >> a[i] >> b[i];
li.push_back(a[i]);
li.push_back(b[i]);
}
sort(all(li));
li.resize(unique(all(li)) - li.begin());
rep(i, n) {
a[i] = lower_bound(all(li), a[i]) - li.begin();
b[i] = lower_bound(all(li), b[i]) - li.begin();
}
rep(i, N)
tr[N + i] = -1;
rep(i, k) {
cin >> t[i];
t[i] = lower_bound(all(li), t[i] + 1) - li.begin();
tr[N + t[i]] = max(tr[N + t[i]], i);
}
for (int i = N - 1; i > 0; i--)
tr[i] = max(tr[i * 2], tr[i * 2 + 1]);
rep(i, n) {
int lp = getm(min(a[i], b[i]) + 1, max(a[i], b[i]) + 1);
lq[lp + 1].push_back({max(a[i], b[i]), i});
}
ll ans = 0, ca;
for (int i = k - 1; i >= 0; i--) {
for (auto &pp : lq[i + 1]) {
ca = (k - i - 1 - sum(pp.first + 1)) % 2;
if ((a[pp.second] > b[pp.second]) ^ ca)
ans += li[a[pp.second]];
else
ans += li[b[pp.second]];
}
add(t[i]);
}
for (auto &pp : lq[0]) {
ca = (k - sum(pp.first + 1)) % 2;
if (ca)
ans += li[b[pp.second]];
else
ans += li[a[pp.second]];
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16896 KB |
Output is correct |
2 |
Correct |
16 ms |
16896 KB |
Output is correct |
3 |
Correct |
16 ms |
16896 KB |
Output is correct |
4 |
Correct |
16 ms |
16896 KB |
Output is correct |
5 |
Correct |
17 ms |
16896 KB |
Output is correct |
6 |
Correct |
17 ms |
16896 KB |
Output is correct |
7 |
Correct |
17 ms |
16948 KB |
Output is correct |
8 |
Correct |
16 ms |
16896 KB |
Output is correct |
9 |
Correct |
16 ms |
16896 KB |
Output is correct |
10 |
Correct |
15 ms |
16896 KB |
Output is correct |
11 |
Correct |
16 ms |
16896 KB |
Output is correct |
12 |
Correct |
16 ms |
16896 KB |
Output is correct |
13 |
Correct |
16 ms |
16896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16896 KB |
Output is correct |
2 |
Correct |
16 ms |
16896 KB |
Output is correct |
3 |
Correct |
16 ms |
16896 KB |
Output is correct |
4 |
Correct |
16 ms |
16896 KB |
Output is correct |
5 |
Correct |
17 ms |
16896 KB |
Output is correct |
6 |
Correct |
17 ms |
16896 KB |
Output is correct |
7 |
Correct |
17 ms |
16948 KB |
Output is correct |
8 |
Correct |
16 ms |
16896 KB |
Output is correct |
9 |
Correct |
16 ms |
16896 KB |
Output is correct |
10 |
Correct |
15 ms |
16896 KB |
Output is correct |
11 |
Correct |
16 ms |
16896 KB |
Output is correct |
12 |
Correct |
16 ms |
16896 KB |
Output is correct |
13 |
Correct |
16 ms |
16896 KB |
Output is correct |
14 |
Correct |
28 ms |
17528 KB |
Output is correct |
15 |
Correct |
44 ms |
18168 KB |
Output is correct |
16 |
Correct |
56 ms |
18676 KB |
Output is correct |
17 |
Correct |
71 ms |
19188 KB |
Output is correct |
18 |
Correct |
71 ms |
19188 KB |
Output is correct |
19 |
Correct |
70 ms |
19188 KB |
Output is correct |
20 |
Correct |
72 ms |
19188 KB |
Output is correct |
21 |
Correct |
68 ms |
19444 KB |
Output is correct |
22 |
Correct |
54 ms |
19060 KB |
Output is correct |
23 |
Correct |
54 ms |
19060 KB |
Output is correct |
24 |
Correct |
54 ms |
18932 KB |
Output is correct |
25 |
Correct |
54 ms |
19060 KB |
Output is correct |
26 |
Correct |
54 ms |
18804 KB |
Output is correct |
27 |
Correct |
62 ms |
18932 KB |
Output is correct |
28 |
Correct |
62 ms |
19060 KB |
Output is correct |
29 |
Correct |
64 ms |
19060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16896 KB |
Output is correct |
2 |
Correct |
16 ms |
16896 KB |
Output is correct |
3 |
Correct |
16 ms |
16896 KB |
Output is correct |
4 |
Correct |
16 ms |
16896 KB |
Output is correct |
5 |
Correct |
17 ms |
16896 KB |
Output is correct |
6 |
Correct |
17 ms |
16896 KB |
Output is correct |
7 |
Correct |
17 ms |
16948 KB |
Output is correct |
8 |
Correct |
16 ms |
16896 KB |
Output is correct |
9 |
Correct |
16 ms |
16896 KB |
Output is correct |
10 |
Correct |
15 ms |
16896 KB |
Output is correct |
11 |
Correct |
16 ms |
16896 KB |
Output is correct |
12 |
Correct |
16 ms |
16896 KB |
Output is correct |
13 |
Correct |
16 ms |
16896 KB |
Output is correct |
14 |
Correct |
28 ms |
17528 KB |
Output is correct |
15 |
Correct |
44 ms |
18168 KB |
Output is correct |
16 |
Correct |
56 ms |
18676 KB |
Output is correct |
17 |
Correct |
71 ms |
19188 KB |
Output is correct |
18 |
Correct |
71 ms |
19188 KB |
Output is correct |
19 |
Correct |
70 ms |
19188 KB |
Output is correct |
20 |
Correct |
72 ms |
19188 KB |
Output is correct |
21 |
Correct |
68 ms |
19444 KB |
Output is correct |
22 |
Correct |
54 ms |
19060 KB |
Output is correct |
23 |
Correct |
54 ms |
19060 KB |
Output is correct |
24 |
Correct |
54 ms |
18932 KB |
Output is correct |
25 |
Correct |
54 ms |
19060 KB |
Output is correct |
26 |
Correct |
54 ms |
18804 KB |
Output is correct |
27 |
Correct |
62 ms |
18932 KB |
Output is correct |
28 |
Correct |
62 ms |
19060 KB |
Output is correct |
29 |
Correct |
64 ms |
19060 KB |
Output is correct |
30 |
Correct |
79 ms |
18808 KB |
Output is correct |
31 |
Correct |
131 ms |
20212 KB |
Output is correct |
32 |
Correct |
201 ms |
21872 KB |
Output is correct |
33 |
Correct |
334 ms |
25320 KB |
Output is correct |
34 |
Correct |
56 ms |
19576 KB |
Output is correct |
35 |
Correct |
334 ms |
30680 KB |
Output is correct |
36 |
Correct |
340 ms |
30316 KB |
Output is correct |
37 |
Correct |
334 ms |
30316 KB |
Output is correct |
38 |
Correct |
339 ms |
30444 KB |
Output is correct |
39 |
Correct |
354 ms |
30312 KB |
Output is correct |
40 |
Correct |
303 ms |
29668 KB |
Output is correct |
41 |
Correct |
337 ms |
30204 KB |
Output is correct |
42 |
Correct |
335 ms |
30056 KB |
Output is correct |
43 |
Correct |
221 ms |
28904 KB |
Output is correct |
44 |
Correct |
250 ms |
28904 KB |
Output is correct |
45 |
Correct |
233 ms |
28912 KB |
Output is correct |
46 |
Correct |
235 ms |
28264 KB |
Output is correct |
47 |
Correct |
205 ms |
27364 KB |
Output is correct |
48 |
Correct |
271 ms |
29584 KB |
Output is correct |
49 |
Correct |
266 ms |
29928 KB |
Output is correct |