#include "bits/stdc++.h"
#define int long long
using namespace std;
const long long INF = 1ll << 60;
const int N = 6e5 + 5;
const int L = 20;
int suf[N];
int a[N], b[N], n, k, t[N], MN[N], MX[N], T[L][N];
array<int, 2> srt_t[N];
vector<int> d;
vector<array<int, 2>> V[N], non;
bitset<N> bit;
bool kol = 0;
void add(int r) {
kol ^= 1;
r++;
for (; r < N; r += r & -r) bit[r] = bit[r] ^ 1;
}
bool get(int r) {
r++;
bool re = 0;
for (; r; r -= r & -r) re ^= bit[r];
return re;
}
bool get_suf(int r) {
return kol ^ get(r - 1);
}
int rmq(int l, int r) {
int t = __lg(r - l);
return max(T[t][l], T[t][r - (1 << t)]);
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
d.push_back(a[i]);
d.push_back(b[i]);
MN[i] = min(a[i], b[i]);
MX[i] = max(a[i], b[i]);
}
for (int i = 0; i < k; i++) {
cin >> t[i];
d.push_back(t[i]);
}
sort(d.begin(), d.end());
d.resize(unique(d.begin(), d.end()) - d.begin());
for (int i = 0; i < n; i++)
a[i] = lower_bound(d.begin(), d.end(), a[i]) - d.begin(),
b[i] = lower_bound(d.begin(), d.end(), b[i]) - d.begin();
for (int i = 0; i < k; i++)
t[i] = lower_bound(d.begin(), d.end(), t[i]) - d.begin();
for (int i = 0; i < k; i++)
srt_t[i] = {t[i], i};
sort(srt_t, srt_t + k);
for (int i = 0; i < k; i++)
T[0][i] = srt_t[i][1];
for (int l = 0; l < L - 1; l++)
for (int i = 0; i + (2 << l) <= k; i++)
T[l + 1][i] = max(T[l][i], T[l][i + (1 << l)]);
for (int i = 0; i < n; i++) {
int mn = min(a[i], b[i]), l, mx = max(a[i], b[i]);
int ind1 = lower_bound(srt_t, srt_t + k, array<int, 2>{mn, -1}) - srt_t;
int ind2 = upper_bound(srt_t, srt_t + k, array<int, 2>{mx, -1}) - srt_t;
if (ind1 >= ind2) l = -1;
else l = rmq(ind1, ind2);
//cout << "LATEST " << l + 1 << '\n';
int q;
if (l == -1) {
q = (a[i] <= b[i]);
non.push_back({q, i});
}
else {
q = 0;
V[l].push_back({q, i});
}
}
int S = 0;
for (int i = k - 1; i >= 0; i--) {
for (auto p: V[i]) {
int q = p[0];
int mx = max(a[p[1]], b[p[1]]);
q ^= get_suf(mx);
S += (q ? MN[p[1]] : MX[p[1]]);
}
add(t[i]);
}
for (auto p: non) {
int q = p[0];
int mx = max(a[p[1]], b[p[1]]);
q ^= get_suf(mx);
S += (q ? MN[p[1]] : MX[p[1]]);
//cout << (q ? MN[p[1]] : MX[p[1]]) << ' ';
}
cout << S << '\n';
return 0;
}
//به زآن که فروشند چه خواهند خرید
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14680 KB |
Output is correct |
2 |
Correct |
8 ms |
14684 KB |
Output is correct |
3 |
Correct |
8 ms |
14732 KB |
Output is correct |
4 |
Correct |
7 ms |
14680 KB |
Output is correct |
5 |
Correct |
8 ms |
14684 KB |
Output is correct |
6 |
Correct |
7 ms |
14684 KB |
Output is correct |
7 |
Correct |
8 ms |
14684 KB |
Output is correct |
8 |
Correct |
7 ms |
14684 KB |
Output is correct |
9 |
Correct |
7 ms |
14884 KB |
Output is correct |
10 |
Correct |
7 ms |
14892 KB |
Output is correct |
11 |
Correct |
7 ms |
14684 KB |
Output is correct |
12 |
Correct |
7 ms |
14912 KB |
Output is correct |
13 |
Correct |
8 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14680 KB |
Output is correct |
2 |
Correct |
8 ms |
14684 KB |
Output is correct |
3 |
Correct |
8 ms |
14732 KB |
Output is correct |
4 |
Correct |
7 ms |
14680 KB |
Output is correct |
5 |
Correct |
8 ms |
14684 KB |
Output is correct |
6 |
Correct |
7 ms |
14684 KB |
Output is correct |
7 |
Correct |
8 ms |
14684 KB |
Output is correct |
8 |
Correct |
7 ms |
14684 KB |
Output is correct |
9 |
Correct |
7 ms |
14884 KB |
Output is correct |
10 |
Correct |
7 ms |
14892 KB |
Output is correct |
11 |
Correct |
7 ms |
14684 KB |
Output is correct |
12 |
Correct |
7 ms |
14912 KB |
Output is correct |
13 |
Correct |
8 ms |
14684 KB |
Output is correct |
14 |
Correct |
17 ms |
16864 KB |
Output is correct |
15 |
Correct |
29 ms |
19416 KB |
Output is correct |
16 |
Correct |
42 ms |
21848 KB |
Output is correct |
17 |
Correct |
55 ms |
24400 KB |
Output is correct |
18 |
Correct |
56 ms |
24268 KB |
Output is correct |
19 |
Correct |
55 ms |
24272 KB |
Output is correct |
20 |
Correct |
56 ms |
24384 KB |
Output is correct |
21 |
Correct |
52 ms |
24876 KB |
Output is correct |
22 |
Correct |
40 ms |
23808 KB |
Output is correct |
23 |
Correct |
40 ms |
23816 KB |
Output is correct |
24 |
Correct |
42 ms |
23868 KB |
Output is correct |
25 |
Correct |
44 ms |
24520 KB |
Output is correct |
26 |
Correct |
41 ms |
23756 KB |
Output is correct |
27 |
Correct |
50 ms |
24272 KB |
Output is correct |
28 |
Correct |
47 ms |
24608 KB |
Output is correct |
29 |
Correct |
54 ms |
24272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14680 KB |
Output is correct |
2 |
Correct |
8 ms |
14684 KB |
Output is correct |
3 |
Correct |
8 ms |
14732 KB |
Output is correct |
4 |
Correct |
7 ms |
14680 KB |
Output is correct |
5 |
Correct |
8 ms |
14684 KB |
Output is correct |
6 |
Correct |
7 ms |
14684 KB |
Output is correct |
7 |
Correct |
8 ms |
14684 KB |
Output is correct |
8 |
Correct |
7 ms |
14684 KB |
Output is correct |
9 |
Correct |
7 ms |
14884 KB |
Output is correct |
10 |
Correct |
7 ms |
14892 KB |
Output is correct |
11 |
Correct |
7 ms |
14684 KB |
Output is correct |
12 |
Correct |
7 ms |
14912 KB |
Output is correct |
13 |
Correct |
8 ms |
14684 KB |
Output is correct |
14 |
Correct |
17 ms |
16864 KB |
Output is correct |
15 |
Correct |
29 ms |
19416 KB |
Output is correct |
16 |
Correct |
42 ms |
21848 KB |
Output is correct |
17 |
Correct |
55 ms |
24400 KB |
Output is correct |
18 |
Correct |
56 ms |
24268 KB |
Output is correct |
19 |
Correct |
55 ms |
24272 KB |
Output is correct |
20 |
Correct |
56 ms |
24384 KB |
Output is correct |
21 |
Correct |
52 ms |
24876 KB |
Output is correct |
22 |
Correct |
40 ms |
23808 KB |
Output is correct |
23 |
Correct |
40 ms |
23816 KB |
Output is correct |
24 |
Correct |
42 ms |
23868 KB |
Output is correct |
25 |
Correct |
44 ms |
24520 KB |
Output is correct |
26 |
Correct |
41 ms |
23756 KB |
Output is correct |
27 |
Correct |
50 ms |
24272 KB |
Output is correct |
28 |
Correct |
47 ms |
24608 KB |
Output is correct |
29 |
Correct |
54 ms |
24272 KB |
Output is correct |
30 |
Correct |
106 ms |
49916 KB |
Output is correct |
31 |
Correct |
149 ms |
53680 KB |
Output is correct |
32 |
Correct |
199 ms |
57916 KB |
Output is correct |
33 |
Correct |
297 ms |
66992 KB |
Output is correct |
34 |
Correct |
102 ms |
49432 KB |
Output is correct |
35 |
Correct |
301 ms |
67176 KB |
Output is correct |
36 |
Correct |
296 ms |
66916 KB |
Output is correct |
37 |
Correct |
298 ms |
66604 KB |
Output is correct |
38 |
Correct |
294 ms |
67180 KB |
Output is correct |
39 |
Correct |
309 ms |
66612 KB |
Output is correct |
40 |
Correct |
286 ms |
69312 KB |
Output is correct |
41 |
Correct |
297 ms |
66736 KB |
Output is correct |
42 |
Correct |
290 ms |
65968 KB |
Output is correct |
43 |
Correct |
186 ms |
68788 KB |
Output is correct |
44 |
Correct |
184 ms |
68876 KB |
Output is correct |
45 |
Correct |
188 ms |
68840 KB |
Output is correct |
46 |
Correct |
193 ms |
65056 KB |
Output is correct |
47 |
Correct |
197 ms |
64180 KB |
Output is correct |
48 |
Correct |
232 ms |
66404 KB |
Output is correct |
49 |
Correct |
228 ms |
67500 KB |
Output is correct |