#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 << 18;
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8576 KB |
Output is correct |
2 |
Correct |
10 ms |
8576 KB |
Output is correct |
3 |
Correct |
11 ms |
8704 KB |
Output is correct |
4 |
Correct |
11 ms |
8704 KB |
Output is correct |
5 |
Correct |
11 ms |
8704 KB |
Output is correct |
6 |
Correct |
14 ms |
8704 KB |
Output is correct |
7 |
Correct |
11 ms |
8704 KB |
Output is correct |
8 |
Correct |
11 ms |
8704 KB |
Output is correct |
9 |
Correct |
11 ms |
8704 KB |
Output is correct |
10 |
Correct |
10 ms |
8704 KB |
Output is correct |
11 |
Correct |
11 ms |
8704 KB |
Output is correct |
12 |
Correct |
10 ms |
8704 KB |
Output is correct |
13 |
Correct |
11 ms |
8704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8576 KB |
Output is correct |
2 |
Correct |
10 ms |
8576 KB |
Output is correct |
3 |
Correct |
11 ms |
8704 KB |
Output is correct |
4 |
Correct |
11 ms |
8704 KB |
Output is correct |
5 |
Correct |
11 ms |
8704 KB |
Output is correct |
6 |
Correct |
14 ms |
8704 KB |
Output is correct |
7 |
Correct |
11 ms |
8704 KB |
Output is correct |
8 |
Correct |
11 ms |
8704 KB |
Output is correct |
9 |
Correct |
11 ms |
8704 KB |
Output is correct |
10 |
Correct |
10 ms |
8704 KB |
Output is correct |
11 |
Correct |
11 ms |
8704 KB |
Output is correct |
12 |
Correct |
10 ms |
8704 KB |
Output is correct |
13 |
Correct |
11 ms |
8704 KB |
Output is correct |
14 |
Correct |
23 ms |
9336 KB |
Output is correct |
15 |
Correct |
36 ms |
9976 KB |
Output is correct |
16 |
Correct |
50 ms |
10612 KB |
Output is correct |
17 |
Correct |
69 ms |
11508 KB |
Output is correct |
18 |
Correct |
64 ms |
11288 KB |
Output is correct |
19 |
Correct |
68 ms |
11380 KB |
Output is correct |
20 |
Correct |
69 ms |
11380 KB |
Output is correct |
21 |
Correct |
62 ms |
11636 KB |
Output is correct |
22 |
Correct |
48 ms |
10868 KB |
Output is correct |
23 |
Correct |
48 ms |
10868 KB |
Output is correct |
24 |
Correct |
46 ms |
10740 KB |
Output is correct |
25 |
Correct |
51 ms |
10868 KB |
Output is correct |
26 |
Correct |
50 ms |
10816 KB |
Output is correct |
27 |
Correct |
58 ms |
11124 KB |
Output is correct |
28 |
Correct |
54 ms |
11248 KB |
Output is correct |
29 |
Correct |
61 ms |
11252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8576 KB |
Output is correct |
2 |
Correct |
10 ms |
8576 KB |
Output is correct |
3 |
Correct |
11 ms |
8704 KB |
Output is correct |
4 |
Correct |
11 ms |
8704 KB |
Output is correct |
5 |
Correct |
11 ms |
8704 KB |
Output is correct |
6 |
Correct |
14 ms |
8704 KB |
Output is correct |
7 |
Correct |
11 ms |
8704 KB |
Output is correct |
8 |
Correct |
11 ms |
8704 KB |
Output is correct |
9 |
Correct |
11 ms |
8704 KB |
Output is correct |
10 |
Correct |
10 ms |
8704 KB |
Output is correct |
11 |
Correct |
11 ms |
8704 KB |
Output is correct |
12 |
Correct |
10 ms |
8704 KB |
Output is correct |
13 |
Correct |
11 ms |
8704 KB |
Output is correct |
14 |
Correct |
23 ms |
9336 KB |
Output is correct |
15 |
Correct |
36 ms |
9976 KB |
Output is correct |
16 |
Correct |
50 ms |
10612 KB |
Output is correct |
17 |
Correct |
69 ms |
11508 KB |
Output is correct |
18 |
Correct |
64 ms |
11288 KB |
Output is correct |
19 |
Correct |
68 ms |
11380 KB |
Output is correct |
20 |
Correct |
69 ms |
11380 KB |
Output is correct |
21 |
Correct |
62 ms |
11636 KB |
Output is correct |
22 |
Correct |
48 ms |
10868 KB |
Output is correct |
23 |
Correct |
48 ms |
10868 KB |
Output is correct |
24 |
Correct |
46 ms |
10740 KB |
Output is correct |
25 |
Correct |
51 ms |
10868 KB |
Output is correct |
26 |
Correct |
50 ms |
10816 KB |
Output is correct |
27 |
Correct |
58 ms |
11124 KB |
Output is correct |
28 |
Correct |
54 ms |
11248 KB |
Output is correct |
29 |
Correct |
61 ms |
11252 KB |
Output is correct |
30 |
Correct |
73 ms |
11896 KB |
Output is correct |
31 |
Correct |
139 ms |
14160 KB |
Output is correct |
32 |
Correct |
199 ms |
16752 KB |
Output is correct |
33 |
Incorrect |
318 ms |
22000 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |