#include<bits/stdc++.h>
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;
struct MNode { int a = -1;
MNode() {}
MNode(int x) : a(x) {}
friend MNode operator+(const MNode &a, const MNode &b) {
return MNode(max(a.a, b.a));
}
};
struct PNode { int a = 0;
PNode() {}
PNode(int x) : a(x) {}
friend PNode operator+(const PNode &a, const PNode &b) {
return PNode(a.a^b.a);
}
};
template<class Node>
struct SegTree {
int n;
vector<Node> tree;
SegTree(int N) : n(N), tree(2*n) {}
void upd(int pos, Node val) {
for(tree[pos+=n]=val;pos/=2;)
tree[pos] = tree[2*pos]+tree[2*pos+1];
}
Node query(int l, int r) {
Node res;
for(l += n, r += n; l < r; l>>=1,r>>=1) {
if(l&1) res = res + tree[l++];
if(r&1) res = tree[--r] + res;
}
return res;
}
};
int n, k;
vector<array<int, 2>> f, ops_sorted;
vector<int> ops, start;
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
f.resize(n);
start.resize(n);
for(auto &[a, b] : f) cin >> a >> b;
ops.resize(k);
for(auto &i : ops) cin >> i;
for(int i = 0; i < k; i++) ops_sorted.push_back({ops[i], i});
for(int i = 0; i < n; i++) ops_sorted.push_back({max(f[i][0], f[i][1])-1, i|(1<<30)});
sort(all(ops_sorted));
SegTree<MNode> ms(k);
for(auto [v, i] : ops_sorted) {
if(i&(1<<30)) {
i -= 1<<30;
int pos = k;
for(int z = 1<<18; z>>=1;)
if(pos-z>=0 && ms.query(pos-z, k).a < min(f[i][0], f[i][1]))
pos -= z;
start[i] = pos;
} else {
ms.upd(i, MNode(v));
}
}
SegTree<PNode> xr(k);
ops_sorted.clear();
for(int i = 0; i < k; i++) ops_sorted.push_back({ops[i], i});
for(int i = 0; i < n; i++) ops_sorted.push_back({max(f[i][0], f[i][1]), i-(1<<30)});
sort(all(ops_sorted), greater<>());
ll sum = 0;
for(auto [v, i] : ops_sorted) {
if(i<0) {
i+=1<<30;
if(start[i] && f[i][0] < f[i][1]) swap(f[i][0], f[i][1]);
if(xr.query(start[i], k).a)
swap(f[i][0], f[i][1]);
sum += f[i][0];
} else {
xr.upd(i, PNode(1));
}
}
cout << sum << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
320 KB |
Output is correct |
14 |
Correct |
16 ms |
1100 KB |
Output is correct |
15 |
Correct |
24 ms |
1776 KB |
Output is correct |
16 |
Correct |
38 ms |
2492 KB |
Output is correct |
17 |
Correct |
57 ms |
3328 KB |
Output is correct |
18 |
Correct |
54 ms |
3332 KB |
Output is correct |
19 |
Correct |
51 ms |
3276 KB |
Output is correct |
20 |
Correct |
56 ms |
3320 KB |
Output is correct |
21 |
Correct |
53 ms |
3284 KB |
Output is correct |
22 |
Correct |
48 ms |
2880 KB |
Output is correct |
23 |
Correct |
48 ms |
2880 KB |
Output is correct |
24 |
Correct |
48 ms |
2808 KB |
Output is correct |
25 |
Correct |
46 ms |
2828 KB |
Output is correct |
26 |
Correct |
51 ms |
3020 KB |
Output is correct |
27 |
Correct |
64 ms |
3268 KB |
Output is correct |
28 |
Correct |
66 ms |
3276 KB |
Output is correct |
29 |
Correct |
58 ms |
3308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
320 KB |
Output is correct |
14 |
Correct |
16 ms |
1100 KB |
Output is correct |
15 |
Correct |
24 ms |
1776 KB |
Output is correct |
16 |
Correct |
38 ms |
2492 KB |
Output is correct |
17 |
Correct |
57 ms |
3328 KB |
Output is correct |
18 |
Correct |
54 ms |
3332 KB |
Output is correct |
19 |
Correct |
51 ms |
3276 KB |
Output is correct |
20 |
Correct |
56 ms |
3320 KB |
Output is correct |
21 |
Correct |
53 ms |
3284 KB |
Output is correct |
22 |
Correct |
48 ms |
2880 KB |
Output is correct |
23 |
Correct |
48 ms |
2880 KB |
Output is correct |
24 |
Correct |
48 ms |
2808 KB |
Output is correct |
25 |
Correct |
46 ms |
2828 KB |
Output is correct |
26 |
Correct |
51 ms |
3020 KB |
Output is correct |
27 |
Correct |
64 ms |
3268 KB |
Output is correct |
28 |
Correct |
66 ms |
3276 KB |
Output is correct |
29 |
Correct |
58 ms |
3308 KB |
Output is correct |
30 |
Correct |
112 ms |
8244 KB |
Output is correct |
31 |
Correct |
151 ms |
9744 KB |
Output is correct |
32 |
Correct |
228 ms |
11588 KB |
Output is correct |
33 |
Correct |
317 ms |
15568 KB |
Output is correct |
34 |
Correct |
103 ms |
7856 KB |
Output is correct |
35 |
Correct |
330 ms |
15608 KB |
Output is correct |
36 |
Correct |
326 ms |
15504 KB |
Output is correct |
37 |
Correct |
388 ms |
15612 KB |
Output is correct |
38 |
Correct |
319 ms |
15488 KB |
Output is correct |
39 |
Correct |
318 ms |
15636 KB |
Output is correct |
40 |
Correct |
334 ms |
15308 KB |
Output is correct |
41 |
Correct |
399 ms |
15564 KB |
Output is correct |
42 |
Correct |
332 ms |
15540 KB |
Output is correct |
43 |
Correct |
288 ms |
14828 KB |
Output is correct |
44 |
Correct |
287 ms |
14900 KB |
Output is correct |
45 |
Correct |
280 ms |
14848 KB |
Output is correct |
46 |
Correct |
287 ms |
13644 KB |
Output is correct |
47 |
Correct |
302 ms |
13508 KB |
Output is correct |
48 |
Correct |
408 ms |
15580 KB |
Output is correct |
49 |
Correct |
390 ms |
15548 KB |
Output is correct |