#include <bits/stdc++.h>
using namespace std;
#define name ""
#define all(x) x.begin(), x.end()
const int N = 2e5 + 7;
struct merge_sort_tree{
int _l, _r, _m;
vector<int> v;
merge_sort_tree *lt, *rt;
merge_sort_tree(int l, int r, const vector<int> &e) : _l(l), _r(r), _m((l + r) >> 1), v(r - l + 1) {
if(l == r) v[0] = e[l], lt = rt = NULL;
else {
lt = new merge_sort_tree(_l, _m, e);
rt = new merge_sort_tree(_m + 1, _r, e);
merge(all(lt->v), all(rt->v), v.begin());
}
}
int query(int qs, int qe, int x, int y) {
if(qe < _l || qs > _r) return 0 ;
if(qs <= _l && _r <= qe) return upper_bound(all(v), y) - lower_bound(all(v), x);
return lt->query(qs, qe, x, y) + rt->query(qs, qe, x, y);
}
};
int c_index[3 * N], tree[12 * N];
int a[N], b[N], c[N];
int n, m;
void build(int id , int l, int r) {
if(l == r){
tree[id] = c_index[l];
return;
}
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
tree[id] = max(tree[id << 1], tree[id << 1 | 1]);
}
int get(int u, int v, int id, int l, int r) {
if(v < l || u > r) return 0;
if(u <= l && r <= v) return tree[id];
int mid = (l + r) >> 1;
return max(get(u, v, id << 1, l, mid), get(u, v, id << 1 | 1, mid + 1, r));
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
if(fopen("bug.inp", "r"))
freopen("bug.inp", "r", stdin), freopen("bug.out", "w", stdout);
else if (fopen(name".inp", "r"))
freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
cin >> n >> m;
vector<int> compress;
for (int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
compress.push_back(a[i]);
compress.push_back(b[i]);
}
for (int i = 1; i <= m; i++) cin >> c[i], compress.push_back(c[i]);
sort(all(compress));
compress.erase(unique(all(compress)), compress.end());
for (int i = 1; i <= n; i++) a[i] = lower_bound(all(compress), a[i]) - compress.begin() + 1;
for (int i = 1; i <= n; i++) b[i] = lower_bound(all(compress), b[i]) - compress.begin() + 1;
for (int i = 1; i <= m; i++){
c[i] = lower_bound(all(compress), c[i]) - compress.begin() + 1;
c_index[c[i]] = i;
}
build(1, 1, compress.size());
merge_sort_tree ms(0, m + 1, vector<int> (c, c + m + 1));
basic_string<int> ans;
for (int i = 1; i <= n; i++) {
if(a[i] == b[i]){
ans += compress[a[i] - 1];
continue;
}
bool swapp = (a[i] > b[i] ? (swap(a[i], b[i]), true) : false);
int last_qr = get(a[i], b[i] - 1, 1, 1, compress.size());
int cnt = ms.query(last_qr + 1, m, b[i], compress.size());
if(last_qr != 0 || swapp) swap(a[i], b[i]);
ans += compress[~cnt & 1 ? a[i] - 1 : b[i] - 1];
}
long long val = 0;
for (int i : ans) val += i;
cout << val;
return 0;
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:49:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | freopen("bug.inp", "r", stdin), freopen("bug.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:49:48: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | freopen("bug.inp", "r", stdin), freopen("bug.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:51:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:51:49: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6876 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
2 ms |
6896 KB |
Output is correct |
6 |
Correct |
2 ms |
6744 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
3 ms |
6744 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6880 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6876 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
2 ms |
6896 KB |
Output is correct |
6 |
Correct |
2 ms |
6744 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
3 ms |
6744 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6880 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
14 ms |
9248 KB |
Output is correct |
15 |
Correct |
27 ms |
12244 KB |
Output is correct |
16 |
Correct |
41 ms |
15180 KB |
Output is correct |
17 |
Correct |
56 ms |
18128 KB |
Output is correct |
18 |
Correct |
58 ms |
18024 KB |
Output is correct |
19 |
Correct |
55 ms |
18164 KB |
Output is correct |
20 |
Correct |
61 ms |
18128 KB |
Output is correct |
21 |
Correct |
56 ms |
18072 KB |
Output is correct |
22 |
Correct |
42 ms |
17616 KB |
Output is correct |
23 |
Correct |
42 ms |
17708 KB |
Output is correct |
24 |
Correct |
49 ms |
17540 KB |
Output is correct |
25 |
Correct |
44 ms |
17620 KB |
Output is correct |
26 |
Correct |
53 ms |
17840 KB |
Output is correct |
27 |
Correct |
76 ms |
18040 KB |
Output is correct |
28 |
Correct |
55 ms |
18128 KB |
Output is correct |
29 |
Correct |
112 ms |
18128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
2 ms |
6876 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
2 ms |
6896 KB |
Output is correct |
6 |
Correct |
2 ms |
6744 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
3 ms |
6744 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6880 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
14 ms |
9248 KB |
Output is correct |
15 |
Correct |
27 ms |
12244 KB |
Output is correct |
16 |
Correct |
41 ms |
15180 KB |
Output is correct |
17 |
Correct |
56 ms |
18128 KB |
Output is correct |
18 |
Correct |
58 ms |
18024 KB |
Output is correct |
19 |
Correct |
55 ms |
18164 KB |
Output is correct |
20 |
Correct |
61 ms |
18128 KB |
Output is correct |
21 |
Correct |
56 ms |
18072 KB |
Output is correct |
22 |
Correct |
42 ms |
17616 KB |
Output is correct |
23 |
Correct |
42 ms |
17708 KB |
Output is correct |
24 |
Correct |
49 ms |
17540 KB |
Output is correct |
25 |
Correct |
44 ms |
17620 KB |
Output is correct |
26 |
Correct |
53 ms |
17840 KB |
Output is correct |
27 |
Correct |
76 ms |
18040 KB |
Output is correct |
28 |
Correct |
55 ms |
18128 KB |
Output is correct |
29 |
Correct |
112 ms |
18128 KB |
Output is correct |
30 |
Correct |
114 ms |
61392 KB |
Output is correct |
31 |
Correct |
156 ms |
64580 KB |
Output is correct |
32 |
Correct |
219 ms |
66508 KB |
Output is correct |
33 |
Correct |
336 ms |
72696 KB |
Output is correct |
34 |
Correct |
102 ms |
60904 KB |
Output is correct |
35 |
Correct |
327 ms |
73028 KB |
Output is correct |
36 |
Correct |
323 ms |
73076 KB |
Output is correct |
37 |
Correct |
343 ms |
74316 KB |
Output is correct |
38 |
Correct |
321 ms |
73724 KB |
Output is correct |
39 |
Correct |
345 ms |
73048 KB |
Output is correct |
40 |
Correct |
310 ms |
72640 KB |
Output is correct |
41 |
Correct |
316 ms |
74168 KB |
Output is correct |
42 |
Correct |
338 ms |
73652 KB |
Output is correct |
43 |
Correct |
238 ms |
73332 KB |
Output is correct |
44 |
Correct |
230 ms |
71808 KB |
Output is correct |
45 |
Correct |
231 ms |
72396 KB |
Output is correct |
46 |
Correct |
235 ms |
68944 KB |
Output is correct |
47 |
Correct |
258 ms |
65984 KB |
Output is correct |
48 |
Correct |
364 ms |
71224 KB |
Output is correct |
49 |
Correct |
325 ms |
71244 KB |
Output is correct |