#include <bits/stdc++.h>
using namespace std;
const int kN = 2e5;
int aib[1 + kN];
void maxSelf(int &x, int y) {
if (x < y) {
x = y;
}
}
struct ST {
int n;
vector<int> t;
ST(int N) : n(N) {
int dim = 1;
while (dim < n) {
dim *= 2;
}
t.resize(dim * 2);
}
void update(int x, int lx, int rx, int pos, int v) {
if (lx == rx) {
t[x] = v;
return;
}
int mid = (lx + rx) / 2;
if (pos <= mid) {
update(x * 2, lx, mid, pos, v);
} else {
update(x * 2 + 1, mid + 1, rx, pos, v);
}
t[x] = max(t[x * 2], t[x * 2 + 1]);
}
void update(int pos, int v) {
update(1, 1, n, pos, v);
}
int query(int x, int lx, int rx, int st, int dr) {
if (st <= lx && rx <= dr) {
return t[x];
}
int mid = (lx + rx) / 2, ans = 0;
if (st <= mid) {
maxSelf(ans, query(x * 2, lx, mid, st, dr));
}
if (mid < dr) {
maxSelf(ans, query(x * 2 + 1, mid + 1, rx, st, dr));
}
return ans;
}
int query(int st, int dr) {
if (dr < st) {
return 0;
}
return query(1, 1, n, st, dr);
}
};
void update(int x) {
for (int i = x; i > 0; i = i & (i - 1)) {
aib[i] += 1;
}
}
int query(int x, int n) {
int ans = 0;
for (int i = x; i <= n; i += i & -i) {
ans += aib[i];
}
return ans;
}
void testCase() {
int n, k;
cin >> n >> k;
vector<int> a(n + 1), b(n + 1);
for (int i = 1; i <= n; ++i) {
cin >> a[i] >> b[i];
}
vector<int> t(k + 1), v(k);
for (int i = 1; i <= k; ++i) {
cin >> t[i];
v[i - 1] = t[i];
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
int N = v.size() + 1;
ST st(N);
for (int i = 1; i <= k; ++i) {
t[i] = distance(v.begin(), upper_bound(v.begin(), v.end(), t[i]));
st.update(t[i], i);
}
vector<vector<int>> cards(k + 1);
for (int i = 1; i <= n; ++i) {
int l = distance(v.begin(), lower_bound(v.begin(), v.end(), min(a[i], b[i]))) + 1;
int r = distance(v.begin(), lower_bound(v.begin(), v.end(), max(a[i], b[i])));
cards[st.query(l, r)].emplace_back(i);
}
int64_t ans = 0;
for (int i = k; i >= 0; --i) {
for (int index : cards[i]) {
if (i != 0 && a[index] < b[index]) {
swap(a[index], b[index]);
}
int pos = distance(v.begin(), lower_bound(v.begin(), v.end(), max(a[index], b[index]))) + 1;
if (query(pos, N) % 2 == 1) {
ans += b[index];
} else {
ans += a[index];
}
}
update(t[i]);
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
412 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
412 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
13 ms |
1244 KB |
Output is correct |
15 |
Correct |
25 ms |
2124 KB |
Output is correct |
16 |
Correct |
40 ms |
2856 KB |
Output is correct |
17 |
Correct |
47 ms |
3916 KB |
Output is correct |
18 |
Correct |
49 ms |
3972 KB |
Output is correct |
19 |
Correct |
50 ms |
3940 KB |
Output is correct |
20 |
Correct |
47 ms |
3936 KB |
Output is correct |
21 |
Correct |
49 ms |
4052 KB |
Output is correct |
22 |
Correct |
35 ms |
3212 KB |
Output is correct |
23 |
Correct |
40 ms |
3196 KB |
Output is correct |
24 |
Correct |
36 ms |
3180 KB |
Output is correct |
25 |
Correct |
32 ms |
3500 KB |
Output is correct |
26 |
Correct |
36 ms |
3660 KB |
Output is correct |
27 |
Correct |
43 ms |
3980 KB |
Output is correct |
28 |
Correct |
40 ms |
3956 KB |
Output is correct |
29 |
Correct |
40 ms |
4096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
412 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
13 ms |
1244 KB |
Output is correct |
15 |
Correct |
25 ms |
2124 KB |
Output is correct |
16 |
Correct |
40 ms |
2856 KB |
Output is correct |
17 |
Correct |
47 ms |
3916 KB |
Output is correct |
18 |
Correct |
49 ms |
3972 KB |
Output is correct |
19 |
Correct |
50 ms |
3940 KB |
Output is correct |
20 |
Correct |
47 ms |
3936 KB |
Output is correct |
21 |
Correct |
49 ms |
4052 KB |
Output is correct |
22 |
Correct |
35 ms |
3212 KB |
Output is correct |
23 |
Correct |
40 ms |
3196 KB |
Output is correct |
24 |
Correct |
36 ms |
3180 KB |
Output is correct |
25 |
Correct |
32 ms |
3500 KB |
Output is correct |
26 |
Correct |
36 ms |
3660 KB |
Output is correct |
27 |
Correct |
43 ms |
3980 KB |
Output is correct |
28 |
Correct |
40 ms |
3956 KB |
Output is correct |
29 |
Correct |
40 ms |
4096 KB |
Output is correct |
30 |
Correct |
128 ms |
11624 KB |
Output is correct |
31 |
Correct |
149 ms |
13132 KB |
Output is correct |
32 |
Correct |
186 ms |
14444 KB |
Output is correct |
33 |
Correct |
319 ms |
17728 KB |
Output is correct |
34 |
Correct |
111 ms |
11356 KB |
Output is correct |
35 |
Correct |
279 ms |
17708 KB |
Output is correct |
36 |
Correct |
308 ms |
17632 KB |
Output is correct |
37 |
Correct |
281 ms |
17732 KB |
Output is correct |
38 |
Correct |
314 ms |
17912 KB |
Output is correct |
39 |
Correct |
279 ms |
17944 KB |
Output is correct |
40 |
Correct |
266 ms |
17500 KB |
Output is correct |
41 |
Correct |
279 ms |
17788 KB |
Output is correct |
42 |
Correct |
300 ms |
17740 KB |
Output is correct |
43 |
Correct |
207 ms |
17064 KB |
Output is correct |
44 |
Correct |
203 ms |
17048 KB |
Output is correct |
45 |
Correct |
190 ms |
16896 KB |
Output is correct |
46 |
Correct |
184 ms |
15840 KB |
Output is correct |
47 |
Correct |
188 ms |
14432 KB |
Output is correct |
48 |
Correct |
273 ms |
17984 KB |
Output is correct |
49 |
Correct |
226 ms |
17896 KB |
Output is correct |