#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44
using namespace std;
struct SegTree {
struct Node {
int cnt;
int mx;
};
vector <Node> aint;
int n;
inline void init(int _n) {
n = _n;
aint.resize(4 * n + 1, {0, 0});
}
inline void refresh(int nod) {
aint[nod].cnt = aint[2 * nod].cnt + aint[2 * nod + 1].cnt;
aint[nod].mx = max(aint[2 * nod].mx, aint[2 * nod + 1].mx);
}
void update_cnt(int nod, int left, int right, int pos) {
if(left == right) {
aint[nod].cnt++;
}
else {
int mid = (left + right) / 2;
if(pos <= mid) update_cnt(2 * nod, left, mid, pos);
else update_cnt(2 * nod + 1, mid + 1, right, pos);
refresh(nod);
}
}
void update_mx(int nod, int left, int right, int pos, int mx) {
if(left == right) {
aint[nod].mx = mx;
}
else {
int mid = (left + right) / 2;
if(pos <= mid) update_mx(2 * nod, left, mid, pos, mx);
else update_mx(2 * nod + 1, mid + 1, right, pos, mx);
refresh(nod);
}
}
int query_mx(int nod, int left, int right, int val) {
if(aint[nod].mx < val) {
return 0;
}
if(left == right) {
return left;
}
else {
int mid = (left + right) / 2;
if(aint[2 * nod + 1].mx >= val) return query_mx(2 * nod + 1, mid + 1, right, val);
return query_mx(2 * nod, left, mid, val);
}
}
int query_cnt(int nod, int left, int right, int l, int r) {
if(l > r) return 0;
if(l <= left && right <= r) {
return aint[nod].cnt;
}
else {
int mid = (left + right) / 2;
int ans = 0;
if(l <= mid) ans += query_cnt(2 * nod, left, mid, l, r);
if(mid < r) ans += query_cnt(2 * nod + 1, mid + 1, right, l, r);
return ans;
}
}
};
int main() {
//ifstream cin("A.in");
//ofstream cout("A.out");
int i, n, m;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
vector <int> ord(n + 1), a(n + 1), b(n + 1);
for(i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
ord[i] = i;
}
sort(next(ord.begin()), ord.end(), [&](const int &x, const int &y) {
return max(a[x], b[x]) > max(a[y], b[y]);
});
vector < pair <int, int> > t(m + 1);
for(i = 1; i <= m; i++) {
cin >> t[i].first;
t[i].second = i;
}
sort(next(t.begin()), t.end(), [&](const pair <int, int> &a, const pair <int, int> &b) {
return a.first > b.first;
});
SegTree st; st.init(m);
for(i = 1; i <= m; i++) {
st.update_mx(1, 1, m, t[i].second, t[i].first);
}
ll ans = 0;
int pos = 1;
for(i = 1; i <= n; i++) {
while(pos <= m && t[pos].first >= max(a[ord[i]], b[ord[i]])) {
st.update_cnt(1, 1, m, t[pos].second);
st.update_mx(1, 1, m, t[pos].second, 0);
pos++;
}
int p = st.query_mx(1, 1, m, min(a[ord[i]], b[ord[i]]));
if(p != 0) {
if(a[ord[i]] < b[ord[i]]) {
swap(a[ord[i]], b[ord[i]]);
}
}
if(st.query_cnt(1, 1, m, p + 1, m) % 2) {
ans += b[ord[i]];
}
else {
ans += a[ord[i]];
}
}
cout << ans;
//cin.close();
//cout.close();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
11 ms |
1152 KB |
Output is correct |
15 |
Correct |
21 ms |
1892 KB |
Output is correct |
16 |
Correct |
31 ms |
2816 KB |
Output is correct |
17 |
Correct |
45 ms |
3584 KB |
Output is correct |
18 |
Correct |
45 ms |
3584 KB |
Output is correct |
19 |
Correct |
40 ms |
3576 KB |
Output is correct |
20 |
Correct |
44 ms |
3576 KB |
Output is correct |
21 |
Correct |
34 ms |
3584 KB |
Output is correct |
22 |
Correct |
30 ms |
3192 KB |
Output is correct |
23 |
Correct |
32 ms |
3072 KB |
Output is correct |
24 |
Correct |
35 ms |
3064 KB |
Output is correct |
25 |
Correct |
28 ms |
3064 KB |
Output is correct |
26 |
Correct |
41 ms |
3328 KB |
Output is correct |
27 |
Correct |
44 ms |
3704 KB |
Output is correct |
28 |
Correct |
44 ms |
3576 KB |
Output is correct |
29 |
Correct |
44 ms |
3584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
11 ms |
1152 KB |
Output is correct |
15 |
Correct |
21 ms |
1892 KB |
Output is correct |
16 |
Correct |
31 ms |
2816 KB |
Output is correct |
17 |
Correct |
45 ms |
3584 KB |
Output is correct |
18 |
Correct |
45 ms |
3584 KB |
Output is correct |
19 |
Correct |
40 ms |
3576 KB |
Output is correct |
20 |
Correct |
44 ms |
3576 KB |
Output is correct |
21 |
Correct |
34 ms |
3584 KB |
Output is correct |
22 |
Correct |
30 ms |
3192 KB |
Output is correct |
23 |
Correct |
32 ms |
3072 KB |
Output is correct |
24 |
Correct |
35 ms |
3064 KB |
Output is correct |
25 |
Correct |
28 ms |
3064 KB |
Output is correct |
26 |
Correct |
41 ms |
3328 KB |
Output is correct |
27 |
Correct |
44 ms |
3704 KB |
Output is correct |
28 |
Correct |
44 ms |
3576 KB |
Output is correct |
29 |
Correct |
44 ms |
3584 KB |
Output is correct |
30 |
Correct |
157 ms |
10616 KB |
Output is correct |
31 |
Correct |
195 ms |
11640 KB |
Output is correct |
32 |
Correct |
224 ms |
13176 KB |
Output is correct |
33 |
Correct |
289 ms |
16376 KB |
Output is correct |
34 |
Correct |
101 ms |
10104 KB |
Output is correct |
35 |
Correct |
307 ms |
16408 KB |
Output is correct |
36 |
Correct |
270 ms |
16504 KB |
Output is correct |
37 |
Correct |
282 ms |
16504 KB |
Output is correct |
38 |
Correct |
261 ms |
16376 KB |
Output is correct |
39 |
Correct |
293 ms |
16376 KB |
Output is correct |
40 |
Correct |
194 ms |
16092 KB |
Output is correct |
41 |
Correct |
288 ms |
16376 KB |
Output is correct |
42 |
Correct |
266 ms |
16272 KB |
Output is correct |
43 |
Correct |
148 ms |
15608 KB |
Output is correct |
44 |
Correct |
144 ms |
15724 KB |
Output is correct |
45 |
Correct |
138 ms |
15608 KB |
Output is correct |
46 |
Correct |
192 ms |
14432 KB |
Output is correct |
47 |
Correct |
223 ms |
14328 KB |
Output is correct |
48 |
Correct |
268 ms |
16552 KB |
Output is correct |
49 |
Correct |
251 ms |
16504 KB |
Output is correct |