#include <bits/stdc++.h>
#define int long long
#define x first
#define y second
using namespace std;
using ll = long long;
typedef pair<int, int> ii;
const int N = 200100;
int n, k, a[N], b[N];
int Log[N], rmq[N][20];
int bit[N], ans[N];
vector <ii> vec, g[N];
void upd(int idx, int val) {
for (; idx; idx -= idx & -idx) {
bit[idx] += val;
}
}
int get(int idx) {
int re = 0;
for (; idx <= k; idx += idx & -idx) {
re += bit[idx];
}
return re;
}
int get(int l, int r) {
int _lg = Log[r - l + 1];
return max(rmq[l][_lg], rmq[r - (1 << _lg) + 1][_lg]);
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> a[i] >> b[i];
}
for (int x, i = 1; i <= k; ++i) {
cin >> x;
vec.push_back(ii(x, i));
}
Log[0] = Log[1] = 0;
for (int i = 2; i <= k; ++i) Log[i] = Log[i / 2] + 1;
sort(vec.begin(), vec.end());
for (int i = 0; i < k; ++i) rmq[i][0] = vec[i].y;
for (int j = 1; j < 20; ++j) {
for (int i = 0; i + (1 << j) <= k; ++i) {
rmq[i][j] = max(rmq[i][j - 1], rmq[i + (1 << j - 1)][j - 1]);
}
}
for (int i = 1; i <= n; ++i) {
int lb = min(a[i], b[i]), rb = max(a[i], b[i]);
int l = lower_bound(vec.begin(), vec.end(), ii(lb, -1)) - vec.begin();
int r = lower_bound(vec.begin(), vec.end(), ii(rb, -1)) - vec.begin() - 1;
int pos = 0;
if (l <= r) {
pos = get(l, r);
if (a[i] < b[i]) swap(a[i], b[i]);
}
g[r + 1].push_back(ii(i, pos + 1));
}
for (int i = k - 1; i >= 0; --i) {
upd(vec[i].y, 1);
for (ii e : g[i]) ans[e.x] = get(e.y) & 1;
}
ll sum = 0;
for (int i = 1; i <= n; ++i) {
sum += ans[i] ? b[i] : a[i];
//cerr << i << ' ' << ans[i] << '\n';
}
cout << sum << '\n';
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:56:51: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
56 | rmq[i][j] = max(rmq[i][j - 1], rmq[i + (1 << j - 1)][j - 1]);
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5248 KB |
Output is correct |
2 |
Correct |
4 ms |
5248 KB |
Output is correct |
3 |
Correct |
4 ms |
5376 KB |
Output is correct |
4 |
Correct |
4 ms |
5376 KB |
Output is correct |
5 |
Correct |
4 ms |
5376 KB |
Output is correct |
6 |
Correct |
5 ms |
5376 KB |
Output is correct |
7 |
Correct |
4 ms |
5376 KB |
Output is correct |
8 |
Correct |
4 ms |
5376 KB |
Output is correct |
9 |
Correct |
4 ms |
5248 KB |
Output is correct |
10 |
Correct |
4 ms |
5376 KB |
Output is correct |
11 |
Correct |
4 ms |
5376 KB |
Output is correct |
12 |
Correct |
4 ms |
5376 KB |
Output is correct |
13 |
Correct |
4 ms |
5248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5248 KB |
Output is correct |
2 |
Correct |
4 ms |
5248 KB |
Output is correct |
3 |
Correct |
4 ms |
5376 KB |
Output is correct |
4 |
Correct |
4 ms |
5376 KB |
Output is correct |
5 |
Correct |
4 ms |
5376 KB |
Output is correct |
6 |
Correct |
5 ms |
5376 KB |
Output is correct |
7 |
Correct |
4 ms |
5376 KB |
Output is correct |
8 |
Correct |
4 ms |
5376 KB |
Output is correct |
9 |
Correct |
4 ms |
5248 KB |
Output is correct |
10 |
Correct |
4 ms |
5376 KB |
Output is correct |
11 |
Correct |
4 ms |
5376 KB |
Output is correct |
12 |
Correct |
4 ms |
5376 KB |
Output is correct |
13 |
Correct |
4 ms |
5248 KB |
Output is correct |
14 |
Correct |
12 ms |
7424 KB |
Output is correct |
15 |
Correct |
24 ms |
9852 KB |
Output is correct |
16 |
Correct |
36 ms |
12148 KB |
Output is correct |
17 |
Correct |
50 ms |
14576 KB |
Output is correct |
18 |
Correct |
49 ms |
14572 KB |
Output is correct |
19 |
Correct |
48 ms |
14572 KB |
Output is correct |
20 |
Correct |
49 ms |
14704 KB |
Output is correct |
21 |
Correct |
42 ms |
14500 KB |
Output is correct |
22 |
Correct |
33 ms |
14584 KB |
Output is correct |
23 |
Correct |
33 ms |
14572 KB |
Output is correct |
24 |
Correct |
35 ms |
14576 KB |
Output is correct |
25 |
Correct |
33 ms |
14448 KB |
Output is correct |
26 |
Correct |
37 ms |
14188 KB |
Output is correct |
27 |
Correct |
42 ms |
14576 KB |
Output is correct |
28 |
Correct |
40 ms |
14576 KB |
Output is correct |
29 |
Correct |
46 ms |
14576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5248 KB |
Output is correct |
2 |
Correct |
4 ms |
5248 KB |
Output is correct |
3 |
Correct |
4 ms |
5376 KB |
Output is correct |
4 |
Correct |
4 ms |
5376 KB |
Output is correct |
5 |
Correct |
4 ms |
5376 KB |
Output is correct |
6 |
Correct |
5 ms |
5376 KB |
Output is correct |
7 |
Correct |
4 ms |
5376 KB |
Output is correct |
8 |
Correct |
4 ms |
5376 KB |
Output is correct |
9 |
Correct |
4 ms |
5248 KB |
Output is correct |
10 |
Correct |
4 ms |
5376 KB |
Output is correct |
11 |
Correct |
4 ms |
5376 KB |
Output is correct |
12 |
Correct |
4 ms |
5376 KB |
Output is correct |
13 |
Correct |
4 ms |
5248 KB |
Output is correct |
14 |
Correct |
12 ms |
7424 KB |
Output is correct |
15 |
Correct |
24 ms |
9852 KB |
Output is correct |
16 |
Correct |
36 ms |
12148 KB |
Output is correct |
17 |
Correct |
50 ms |
14576 KB |
Output is correct |
18 |
Correct |
49 ms |
14572 KB |
Output is correct |
19 |
Correct |
48 ms |
14572 KB |
Output is correct |
20 |
Correct |
49 ms |
14704 KB |
Output is correct |
21 |
Correct |
42 ms |
14500 KB |
Output is correct |
22 |
Correct |
33 ms |
14584 KB |
Output is correct |
23 |
Correct |
33 ms |
14572 KB |
Output is correct |
24 |
Correct |
35 ms |
14576 KB |
Output is correct |
25 |
Correct |
33 ms |
14448 KB |
Output is correct |
26 |
Correct |
37 ms |
14188 KB |
Output is correct |
27 |
Correct |
42 ms |
14576 KB |
Output is correct |
28 |
Correct |
40 ms |
14576 KB |
Output is correct |
29 |
Correct |
46 ms |
14576 KB |
Output is correct |
30 |
Correct |
117 ms |
43132 KB |
Output is correct |
31 |
Correct |
167 ms |
45152 KB |
Output is correct |
32 |
Correct |
216 ms |
47712 KB |
Output is correct |
33 |
Correct |
321 ms |
52448 KB |
Output is correct |
34 |
Correct |
106 ms |
42720 KB |
Output is correct |
35 |
Correct |
328 ms |
52576 KB |
Output is correct |
36 |
Correct |
322 ms |
52576 KB |
Output is correct |
37 |
Correct |
322 ms |
52448 KB |
Output is correct |
38 |
Correct |
321 ms |
52360 KB |
Output is correct |
39 |
Correct |
313 ms |
52444 KB |
Output is correct |
40 |
Correct |
242 ms |
52064 KB |
Output is correct |
41 |
Correct |
312 ms |
52448 KB |
Output is correct |
42 |
Correct |
310 ms |
52460 KB |
Output is correct |
43 |
Correct |
170 ms |
51396 KB |
Output is correct |
44 |
Correct |
170 ms |
51168 KB |
Output is correct |
45 |
Correct |
174 ms |
51384 KB |
Output is correct |
46 |
Correct |
187 ms |
52576 KB |
Output is correct |
47 |
Correct |
191 ms |
52708 KB |
Output is correct |
48 |
Correct |
261 ms |
52448 KB |
Output is correct |
49 |
Correct |
263 ms |
52320 KB |
Output is correct |