#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define file "input"
void setIO() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(file".inp", "r")) {
freopen(file".inp", "r", stdin);
freopen(file".out", "w", stdout);
}
}
const int N = 2e5+5;
int n, k, a[N], b[N], t[N];
vector<int> sgt[N * 4];
ll ans;
void build(int id = 1, int lb = 1, int rb = k) {
if (lb ^ rb) {
int k = id << 1, mb = (lb + rb) >> 1;
build(k, lb, mb);
build(k+1, mb+1, rb);
sgt[id].resize(sz(sgt[k]) + sz(sgt[k+1]));
merge(all(sgt[k]), all(sgt[k+1]), sgt[id].begin());
}
else {
sgt[id].push_back(t[lb]);
}
// cout << lb << " " << rb << ":\n";
// fore(x, sgt[id]) cout << x << " ";
// cout << "\n";
}
int query1(int v1, int v2, int r, int id = 1, int lb = 1, int rb = k) {
if (lb > r) return -1;
if (rb <= r) {
// cout << lb << " " << rb << "\n";
// cout << id << "\n";
// fore(x, sgt[id]) cout << x << " ";
// cout << "\n";
auto it = lower_bound(all(sgt[id]), v1);
if (it == sgt[id].end()) return -1;
if (*it > v2) return -1;
}
if (lb == rb) {
return lb;
}
int k = id << 1, mb = (lb + rb) >> 1;
int ans = query1(v1, v2, r, k+1, mb+1, rb);
if (ans == -1) ans = query1(v1, v2, r, k, lb, mb);
return ans;
}
int query2(int l, int r, int value, int id = 1, int lb = 1, int rb = k) {
if (l <= lb && rb <= r) {
return sz(sgt[id]) - (lower_bound(all(sgt[id]), value) - sgt[id].begin());
}
else if (rb < l || lb > r) {
return 0;
}
int k = id << 1, mb = (lb + rb) >> 1;
return query2(l, r, value, k, lb, mb) + query2(l, r, value, k+1, mb+1, rb);
}
int main() {
setIO();
cin >> n >> k;
foru(i, 1, n) cin >> a[i] >> b[i];
foru(i, 1, k) cin >> t[i];
int mx = *max_element(t+1, t+k+1);
build();
// cout << query1(9, 9, k) << "\n";
// cout << query1(a[1], mx, k) << "\n";
foru(i, 1, n) {
if (a[i] == b[i]) {
ans += a[i];
continue;
}
// cout << a[i] << " " << b[i] << "\n";
if (a[i] < b[i]) {
int pos = query1(b[i], mx, k);
if (pos < query1(a[i], mx, k)) {
ans += b[i];
continue;
}
int pos2 = max(0, query1(a[i], b[i]-1, pos-1));
int cnt = query2(pos2+1, pos, b[i]);
if ((cnt + (pos2 == 0)) & 1) ans += a[i];
else ans += b[i];
}
else {
int pos = query1(a[i], mx, k);
if (pos < query1(b[i], mx, k)) {
ans += a[i];
continue;
}
int pos2 = max(0, query1(b[i], a[i]-1, pos-1));
int cnt = query2(pos2+1, pos, a[i]);
if (cnt & 1) ans += b[i];
else ans += a[i];
}
// cout << ans << "\n";
}
cout << ans;
}
Compilation message
fortune_telling2.cpp: In function 'void setIO()':
fortune_telling2.cpp:18:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen(file".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen(file".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
21084 KB |
Output is correct |
2 |
Correct |
5 ms |
21084 KB |
Output is correct |
3 |
Correct |
5 ms |
21280 KB |
Output is correct |
4 |
Correct |
5 ms |
21084 KB |
Output is correct |
5 |
Correct |
5 ms |
21280 KB |
Output is correct |
6 |
Correct |
5 ms |
21080 KB |
Output is correct |
7 |
Correct |
6 ms |
21084 KB |
Output is correct |
8 |
Correct |
5 ms |
21080 KB |
Output is correct |
9 |
Correct |
5 ms |
21080 KB |
Output is correct |
10 |
Correct |
5 ms |
21084 KB |
Output is correct |
11 |
Correct |
5 ms |
21084 KB |
Output is correct |
12 |
Correct |
5 ms |
21176 KB |
Output is correct |
13 |
Correct |
6 ms |
21084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
21084 KB |
Output is correct |
2 |
Correct |
5 ms |
21084 KB |
Output is correct |
3 |
Correct |
5 ms |
21280 KB |
Output is correct |
4 |
Correct |
5 ms |
21084 KB |
Output is correct |
5 |
Correct |
5 ms |
21280 KB |
Output is correct |
6 |
Correct |
5 ms |
21080 KB |
Output is correct |
7 |
Correct |
6 ms |
21084 KB |
Output is correct |
8 |
Correct |
5 ms |
21080 KB |
Output is correct |
9 |
Correct |
5 ms |
21080 KB |
Output is correct |
10 |
Correct |
5 ms |
21084 KB |
Output is correct |
11 |
Correct |
5 ms |
21084 KB |
Output is correct |
12 |
Correct |
5 ms |
21176 KB |
Output is correct |
13 |
Correct |
6 ms |
21084 KB |
Output is correct |
14 |
Correct |
21 ms |
22364 KB |
Output is correct |
15 |
Correct |
42 ms |
23832 KB |
Output is correct |
16 |
Correct |
60 ms |
25172 KB |
Output is correct |
17 |
Correct |
87 ms |
26712 KB |
Output is correct |
18 |
Correct |
88 ms |
26488 KB |
Output is correct |
19 |
Correct |
87 ms |
26960 KB |
Output is correct |
20 |
Correct |
91 ms |
26700 KB |
Output is correct |
21 |
Correct |
71 ms |
26460 KB |
Output is correct |
22 |
Correct |
41 ms |
26204 KB |
Output is correct |
23 |
Correct |
46 ms |
26332 KB |
Output is correct |
24 |
Correct |
50 ms |
26204 KB |
Output is correct |
25 |
Correct |
40 ms |
26204 KB |
Output is correct |
26 |
Correct |
62 ms |
26452 KB |
Output is correct |
27 |
Correct |
147 ms |
26712 KB |
Output is correct |
28 |
Correct |
72 ms |
26708 KB |
Output is correct |
29 |
Correct |
130 ms |
26712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
21084 KB |
Output is correct |
2 |
Correct |
5 ms |
21084 KB |
Output is correct |
3 |
Correct |
5 ms |
21280 KB |
Output is correct |
4 |
Correct |
5 ms |
21084 KB |
Output is correct |
5 |
Correct |
5 ms |
21280 KB |
Output is correct |
6 |
Correct |
5 ms |
21080 KB |
Output is correct |
7 |
Correct |
6 ms |
21084 KB |
Output is correct |
8 |
Correct |
5 ms |
21080 KB |
Output is correct |
9 |
Correct |
5 ms |
21080 KB |
Output is correct |
10 |
Correct |
5 ms |
21084 KB |
Output is correct |
11 |
Correct |
5 ms |
21084 KB |
Output is correct |
12 |
Correct |
5 ms |
21176 KB |
Output is correct |
13 |
Correct |
6 ms |
21084 KB |
Output is correct |
14 |
Correct |
21 ms |
22364 KB |
Output is correct |
15 |
Correct |
42 ms |
23832 KB |
Output is correct |
16 |
Correct |
60 ms |
25172 KB |
Output is correct |
17 |
Correct |
87 ms |
26712 KB |
Output is correct |
18 |
Correct |
88 ms |
26488 KB |
Output is correct |
19 |
Correct |
87 ms |
26960 KB |
Output is correct |
20 |
Correct |
91 ms |
26700 KB |
Output is correct |
21 |
Correct |
71 ms |
26460 KB |
Output is correct |
22 |
Correct |
41 ms |
26204 KB |
Output is correct |
23 |
Correct |
46 ms |
26332 KB |
Output is correct |
24 |
Correct |
50 ms |
26204 KB |
Output is correct |
25 |
Correct |
40 ms |
26204 KB |
Output is correct |
26 |
Correct |
62 ms |
26452 KB |
Output is correct |
27 |
Correct |
147 ms |
26712 KB |
Output is correct |
28 |
Correct |
72 ms |
26708 KB |
Output is correct |
29 |
Correct |
130 ms |
26712 KB |
Output is correct |
30 |
Correct |
96 ms |
46932 KB |
Output is correct |
31 |
Correct |
188 ms |
47700 KB |
Output is correct |
32 |
Correct |
323 ms |
48980 KB |
Output is correct |
33 |
Correct |
569 ms |
51028 KB |
Output is correct |
34 |
Correct |
77 ms |
46932 KB |
Output is correct |
35 |
Correct |
554 ms |
51136 KB |
Output is correct |
36 |
Correct |
569 ms |
51152 KB |
Output is correct |
37 |
Correct |
584 ms |
51280 KB |
Output is correct |
38 |
Correct |
542 ms |
51156 KB |
Output is correct |
39 |
Correct |
580 ms |
51284 KB |
Output is correct |
40 |
Correct |
416 ms |
51024 KB |
Output is correct |
41 |
Correct |
547 ms |
51172 KB |
Output is correct |
42 |
Correct |
589 ms |
51028 KB |
Output is correct |
43 |
Correct |
207 ms |
50736 KB |
Output is correct |
44 |
Correct |
206 ms |
50432 KB |
Output is correct |
45 |
Correct |
199 ms |
50360 KB |
Output is correct |
46 |
Correct |
249 ms |
49520 KB |
Output is correct |
47 |
Correct |
342 ms |
48976 KB |
Output is correct |
48 |
Correct |
440 ms |
51284 KB |
Output is correct |
49 |
Correct |
349 ms |
51028 KB |
Output is correct |