#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5;
int N, K;
pair<int,int> A[MAXN + 10];
int bit[MAXN + 10];
int X[MAXN + 10];
void inc(int i) {
for(i++; i <= K; i += i & (-i)) {
bit[i] ^= 1;
}
}
int sum(int r) {
int res = 0;
for(r++; r > 0; r -= r & (-r)) {
res ^= bit[r];
}
return res;
}
int sum(int l, int r) {
return sum(r) ^ sum(l);
}
int mx[4 * MAXN + 10];
void mx_upd(int cn, int b, int e, int i, int x) {
if(i < b || i > e) return;
if(b == e) {
mx[cn] = x;
return;
}
int m = (b + e) / 2;
mx_upd(cn << 1, b, m, i, x);
mx_upd(cn << 1 | 1, m+1, e, i, x);
mx[cn] = max(mx[cn << 1], mx[cn << 1 | 1]);
}
int nxt(int cn, int b, int e, int x) {
if(mx[cn] < x) return -1;
if(b == e) return b;
int m = (b + e) / 2;
if(mx[cn<<1|1] < x) return nxt(cn << 1, b, m, x);
return nxt(cn << 1 | 1, m+1, e, x);
}
int solve(int i) {
int l = nxt(1, 0, K-1, min(A[i].first, A[i].second));
int p = sum(l, K-1);
if(l != -1) {
if(A[i].first < A[i].second) swap(A[i].first, A[i].second);
}
int res = p ? A[i].second : A[i].first;
return res;
}
int main() {
scanf("%d%d", &N, &K);
for(int i = 0; i < N; i++) {
scanf("%d%d", &A[i].first, &A[i].second);
}
for(int i = 0; i < K; i++) {
scanf("%d", &X[i]);
}
sort(A, A + N,
[](pair<int,int> a, pair<int,int> b) {
int ma = max(a.first, a.second);
int mb = max(b.first, b.second);
if(ma != mb) return ma < mb;
return a < b;
});
reverse(A, A + N);
memset(mx, -1, sizeof mx);
for(int i = 0; i < K; i++) {
if(X[i] < max(A[0].first, A[0].second)) { // S
mx_upd(1, 0, K-1, i, X[i]);
}
}
set<pair<long long,int>> st;
for(int i = 0; i < K; i++) {
st.emplace(X[i], i);
}
while(st.size() && st.rbegin()->first >= max(A[0].first, A[0].second)) {
int j = st.rbegin()->second;
inc(j);
mx_upd(1, 0, K-1, j, -1);
st.erase(*st.rbegin());
}
long long res = solve(0);
for(int i = 1; i < N; i++) {
while(st.size() && st.rbegin()->first >= max(A[i].first, A[i].second)) {
int j = st.rbegin()->second;
mx_upd(1, 0, K-1, j, -1);
inc(j);
st.erase(*st.rbegin());
}
res += solve(i);
}
printf("%lld\n", res);
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:64:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &K);
~~~~~^~~~~~~~~~~~~~~~
fortune_telling2.cpp:66:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &A[i].first, &A[i].second);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:69:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &X[i]);
~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3576 KB |
Output is correct |
2 |
Correct |
5 ms |
3576 KB |
Output is correct |
3 |
Correct |
6 ms |
3628 KB |
Output is correct |
4 |
Correct |
6 ms |
3680 KB |
Output is correct |
5 |
Correct |
6 ms |
3720 KB |
Output is correct |
6 |
Correct |
6 ms |
3720 KB |
Output is correct |
7 |
Correct |
6 ms |
3720 KB |
Output is correct |
8 |
Correct |
5 ms |
3744 KB |
Output is correct |
9 |
Correct |
5 ms |
3820 KB |
Output is correct |
10 |
Correct |
5 ms |
3820 KB |
Output is correct |
11 |
Correct |
6 ms |
3916 KB |
Output is correct |
12 |
Correct |
6 ms |
3916 KB |
Output is correct |
13 |
Correct |
5 ms |
3916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3576 KB |
Output is correct |
2 |
Correct |
5 ms |
3576 KB |
Output is correct |
3 |
Correct |
6 ms |
3628 KB |
Output is correct |
4 |
Correct |
6 ms |
3680 KB |
Output is correct |
5 |
Correct |
6 ms |
3720 KB |
Output is correct |
6 |
Correct |
6 ms |
3720 KB |
Output is correct |
7 |
Correct |
6 ms |
3720 KB |
Output is correct |
8 |
Correct |
5 ms |
3744 KB |
Output is correct |
9 |
Correct |
5 ms |
3820 KB |
Output is correct |
10 |
Correct |
5 ms |
3820 KB |
Output is correct |
11 |
Correct |
6 ms |
3916 KB |
Output is correct |
12 |
Correct |
6 ms |
3916 KB |
Output is correct |
13 |
Correct |
5 ms |
3916 KB |
Output is correct |
14 |
Correct |
22 ms |
4476 KB |
Output is correct |
15 |
Correct |
37 ms |
5288 KB |
Output is correct |
16 |
Correct |
75 ms |
6352 KB |
Output is correct |
17 |
Correct |
75 ms |
6988 KB |
Output is correct |
18 |
Correct |
70 ms |
6988 KB |
Output is correct |
19 |
Correct |
77 ms |
6988 KB |
Output is correct |
20 |
Correct |
87 ms |
6988 KB |
Output is correct |
21 |
Correct |
58 ms |
7036 KB |
Output is correct |
22 |
Correct |
55 ms |
7036 KB |
Output is correct |
23 |
Correct |
56 ms |
7036 KB |
Output is correct |
24 |
Correct |
58 ms |
7036 KB |
Output is correct |
25 |
Correct |
82 ms |
7036 KB |
Output is correct |
26 |
Correct |
66 ms |
7036 KB |
Output is correct |
27 |
Correct |
83 ms |
7036 KB |
Output is correct |
28 |
Correct |
68 ms |
7036 KB |
Output is correct |
29 |
Correct |
73 ms |
7036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3576 KB |
Output is correct |
2 |
Correct |
5 ms |
3576 KB |
Output is correct |
3 |
Correct |
6 ms |
3628 KB |
Output is correct |
4 |
Correct |
6 ms |
3680 KB |
Output is correct |
5 |
Correct |
6 ms |
3720 KB |
Output is correct |
6 |
Correct |
6 ms |
3720 KB |
Output is correct |
7 |
Correct |
6 ms |
3720 KB |
Output is correct |
8 |
Correct |
5 ms |
3744 KB |
Output is correct |
9 |
Correct |
5 ms |
3820 KB |
Output is correct |
10 |
Correct |
5 ms |
3820 KB |
Output is correct |
11 |
Correct |
6 ms |
3916 KB |
Output is correct |
12 |
Correct |
6 ms |
3916 KB |
Output is correct |
13 |
Correct |
5 ms |
3916 KB |
Output is correct |
14 |
Correct |
22 ms |
4476 KB |
Output is correct |
15 |
Correct |
37 ms |
5288 KB |
Output is correct |
16 |
Correct |
75 ms |
6352 KB |
Output is correct |
17 |
Correct |
75 ms |
6988 KB |
Output is correct |
18 |
Correct |
70 ms |
6988 KB |
Output is correct |
19 |
Correct |
77 ms |
6988 KB |
Output is correct |
20 |
Correct |
87 ms |
6988 KB |
Output is correct |
21 |
Correct |
58 ms |
7036 KB |
Output is correct |
22 |
Correct |
55 ms |
7036 KB |
Output is correct |
23 |
Correct |
56 ms |
7036 KB |
Output is correct |
24 |
Correct |
58 ms |
7036 KB |
Output is correct |
25 |
Correct |
82 ms |
7036 KB |
Output is correct |
26 |
Correct |
66 ms |
7036 KB |
Output is correct |
27 |
Correct |
83 ms |
7036 KB |
Output is correct |
28 |
Correct |
68 ms |
7036 KB |
Output is correct |
29 |
Correct |
73 ms |
7036 KB |
Output is correct |
30 |
Correct |
364 ms |
17992 KB |
Output is correct |
31 |
Correct |
557 ms |
18300 KB |
Output is correct |
32 |
Correct |
501 ms |
18644 KB |
Output is correct |
33 |
Correct |
540 ms |
19528 KB |
Output is correct |
34 |
Correct |
330 ms |
19528 KB |
Output is correct |
35 |
Correct |
599 ms |
19528 KB |
Output is correct |
36 |
Correct |
455 ms |
19528 KB |
Output is correct |
37 |
Correct |
714 ms |
19528 KB |
Output is correct |
38 |
Correct |
575 ms |
19528 KB |
Output is correct |
39 |
Correct |
616 ms |
19528 KB |
Output is correct |
40 |
Correct |
485 ms |
19528 KB |
Output is correct |
41 |
Correct |
577 ms |
19528 KB |
Output is correct |
42 |
Correct |
516 ms |
19528 KB |
Output is correct |
43 |
Correct |
285 ms |
19528 KB |
Output is correct |
44 |
Correct |
286 ms |
19528 KB |
Output is correct |
45 |
Correct |
281 ms |
19528 KB |
Output is correct |
46 |
Correct |
409 ms |
19528 KB |
Output is correct |
47 |
Correct |
403 ms |
19528 KB |
Output is correct |
48 |
Correct |
539 ms |
19528 KB |
Output is correct |
49 |
Correct |
586 ms |
19528 KB |
Output is correct |