#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
template<class T> struct merge_sort_tree {
int _l, _r, _m;
vector<T> v;
merge_sort_tree *left, *right;
merge_sort_tree(int l, int r, vector<T> &e) {
v.resize(r - l + 1);
_l = l, _r = r, _m = (l + r) >> 1, v[0] = e[l];
if (l == r) left = right = nullptr;
else {
left = new merge_sort_tree(_l, _m, e);
right = new merge_sort_tree(_m + 1, _r, e);
vector<T> v1 = left->v, v2 = right->v;
v.clear();
int s1 = sz(v1), s2 = sz(v2);
v.reserve(s1 + s2);
int i = 0, j = 0;
while (i < s1 && j < s2) {
if (v1[i] <= v2[j]) v.push_back(v1[i]), i++;
else v.push_back(v2[j]), j++;
}
while (i < s1) v.push_back(v1[i]), i++;
while (j < s2) v.push_back(v2[j]), j++;
}
}
int count(int l, int r, T a, T b) {
if (a > b) return 0;
if (l > _r || r < _l) return 0;
if (_l >= l && _r <= r) return upper_bound(all(v), b) - lower_bound(all(v), a);
return left->count(l, r, a, b) + right->count(l, r, a, b);
}
};
const int N = 2e5 + 7, N2 = 6e5 + 2, LG = 22;
int n, k, a[N], b[N], t[N], ti[N2];
int spt_max_ti[N2][LG];
int lg2(int x) { return x ? 31 - __builtin_clz(x) : -1; }
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
for (int i = 0; i < k; i++) cin >> t[i];
vector<int> compr; compr.reserve(2 * n + k);
for (int i = 0; i < n; i++) compr.push_back(a[i]), compr.push_back(b[i]);
for (int i = 0; i < k; i++) compr.push_back(t[i]);
sort(compr.begin(), compr.end());
compr.erase(unique(compr.begin(), compr.end()), compr.end());
for (int i = 0; i < n; i++) {
a[i] = lower_bound(compr.begin(), compr.end(), a[i]) - compr.begin();
b[i] = lower_bound(compr.begin(), compr.end(), b[i]) - compr.begin();
}
for (int i = 0; i < k; i++)
t[i] = lower_bound(compr.begin(), compr.end(), t[i]) - compr.begin();
int max_ti = compr.size();
for (int i = 0; i <= max_ti; i++) ti[i] = -1;
for (int i = 0; i < k; i++) ti[t[i]] = i;
for (int i = 0; i <= max_ti; i++)
spt_max_ti[i][0] = ti[i];
for (int j = 1; j < LG; j++)
for (int i = 0; i + (1 << j) - 1 <= max_ti; i++)
spt_max_ti[i][j] = max(spt_max_ti[i][j - 1], spt_max_ti[i + (1 << (j - 1))][j - 1]);
auto query_max_ti = [&](int l, int r) {
int len = lg2(r - l + 1);
return max(spt_max_ti[l][len], spt_max_ti[r - (1 << len) + 1][len]);
};
vector<int> t_vector(k);
for (int i = 0; i < k; i++) t_vector[i] = t[i];
auto cnt_in_range_ab = merge_sort_tree<int>(0, k - 1, t_vector);
long long ans = 0;
for (int i = 0; i < n; i++) {
if (a[i] == b[i]) { ans += compr[a[i]]; continue; }
bool swapped = (a[i] > b[i]);
if (a[i] > b[i]) swap(a[i], b[i]);
auto last_pos = query_max_ti(a[i], b[i] - 1);
int cnt_flipped = cnt_in_range_ab.count(last_pos + 1, k - 1, b[i], int(1e9));
if (last_pos != -1 || swapped) swap(a[i], b[i]);
if (cnt_flipped % 2 == 0) ans += compr[a[i]];
else ans += compr[b[i]];
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
2 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
2 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
12 ms |
5596 KB |
Output is correct |
15 |
Correct |
32 ms |
10996 KB |
Output is correct |
16 |
Correct |
41 ms |
16332 KB |
Output is correct |
17 |
Correct |
54 ms |
21776 KB |
Output is correct |
18 |
Correct |
53 ms |
21780 KB |
Output is correct |
19 |
Correct |
57 ms |
21708 KB |
Output is correct |
20 |
Correct |
58 ms |
21724 KB |
Output is correct |
21 |
Correct |
49 ms |
21784 KB |
Output is correct |
22 |
Correct |
39 ms |
18188 KB |
Output is correct |
23 |
Correct |
48 ms |
16308 KB |
Output is correct |
24 |
Correct |
38 ms |
14972 KB |
Output is correct |
25 |
Correct |
40 ms |
19252 KB |
Output is correct |
26 |
Correct |
44 ms |
17448 KB |
Output is correct |
27 |
Correct |
54 ms |
18180 KB |
Output is correct |
28 |
Correct |
45 ms |
18100 KB |
Output is correct |
29 |
Correct |
62 ms |
19980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
2 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
12 ms |
5596 KB |
Output is correct |
15 |
Correct |
32 ms |
10996 KB |
Output is correct |
16 |
Correct |
41 ms |
16332 KB |
Output is correct |
17 |
Correct |
54 ms |
21776 KB |
Output is correct |
18 |
Correct |
53 ms |
21780 KB |
Output is correct |
19 |
Correct |
57 ms |
21708 KB |
Output is correct |
20 |
Correct |
58 ms |
21724 KB |
Output is correct |
21 |
Correct |
49 ms |
21784 KB |
Output is correct |
22 |
Correct |
39 ms |
18188 KB |
Output is correct |
23 |
Correct |
48 ms |
16308 KB |
Output is correct |
24 |
Correct |
38 ms |
14972 KB |
Output is correct |
25 |
Correct |
40 ms |
19252 KB |
Output is correct |
26 |
Correct |
44 ms |
17448 KB |
Output is correct |
27 |
Correct |
54 ms |
18180 KB |
Output is correct |
28 |
Correct |
45 ms |
18100 KB |
Output is correct |
29 |
Correct |
62 ms |
19980 KB |
Output is correct |
30 |
Correct |
150 ms |
72332 KB |
Output is correct |
31 |
Correct |
185 ms |
83012 KB |
Output is correct |
32 |
Correct |
233 ms |
93756 KB |
Output is correct |
33 |
Correct |
352 ms |
115276 KB |
Output is correct |
34 |
Correct |
147 ms |
72432 KB |
Output is correct |
35 |
Correct |
358 ms |
115308 KB |
Output is correct |
36 |
Correct |
353 ms |
115316 KB |
Output is correct |
37 |
Correct |
375 ms |
115332 KB |
Output is correct |
38 |
Correct |
358 ms |
115312 KB |
Output is correct |
39 |
Correct |
363 ms |
115328 KB |
Output is correct |
40 |
Correct |
332 ms |
114948 KB |
Output is correct |
41 |
Correct |
368 ms |
115228 KB |
Output is correct |
42 |
Correct |
369 ms |
115252 KB |
Output is correct |
43 |
Correct |
259 ms |
114708 KB |
Output is correct |
44 |
Correct |
257 ms |
114616 KB |
Output is correct |
45 |
Correct |
253 ms |
114536 KB |
Output is correct |
46 |
Correct |
225 ms |
86380 KB |
Output is correct |
47 |
Correct |
227 ms |
77316 KB |
Output is correct |
48 |
Correct |
298 ms |
97316 KB |
Output is correct |
49 |
Correct |
275 ms |
97320 KB |
Output is correct |