#include <bits/stdc++.h>
#define long long long
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int N = 4e5+5;
struct SegmentTreeMax {
int t[N << 1];
void update(int x, int k) {
x += N;
for(t[x] = max(t[x], k); x != 1; x >>= 1)
t[x >> 1] = max(t[x], t[x ^ 1]);
}
int query(int l, int r) {
int ret = 0;
for(l += N, r += N+1; l < r; l >>= 1, r >>= 1) {
if(l & 1) ret = max(ret, t[l++]);
if(r & 1) ret = max(ret, t[--r]);
}
return ret;
}
} t1;
struct SegmentTreeSum {
int t[N << 1];
void update(int x, int k) {
for(t[x += N] += k; x != 1; x >>= 1)
t[x >> 1] = t[x] + t[x ^ 1];
}
int query(int l, int r) {
int ret = 0;
for(l += N, r += N+1; l < r; l >>= 1, r >>= 1) {
if(l & 1) ret += t[l++];
if(r & 1) ret += t[--r];
}
return ret;
}
} t2;
int n, k;
int A[N], B[N], Q[N], cnt[N], pos[N];
vector<int> coord;
vector<pii> vec[N];
int main() {
scanf("%d %d", &n, &k);
for(int i = 1; i <= n; i++) {
scanf("%d %d", A + i, B + i);
coord.emplace_back(A[i]);
coord.emplace_back(B[i]);
}
sort(coord.begin(), coord.end());
coord.resize(unique(coord.begin(), coord.end()) - coord.begin());
auto get = [&](int x) { return lower_bound(coord.begin(), coord.end(), x) - coord.begin() + 1; };
for(int i = 1; i <= k; i++) {
scanf("%d", Q + i);
int idx = upper_bound(coord.begin(), coord.end(), Q[i]) - coord.begin();
t1.update(idx, i);
}
for(int i = 1; i <= n; i++) {
int ia = get(A[i]), ib = get(B[i]);
pos[i] = t1.query(min(ia, ib), max(ia, ib) - 1);
vec[pos[i]].emplace_back(max(ia, ib), i);
}
for(int i = k; ~i; i--) {
int idx = upper_bound(coord.begin(), coord.end(), Q[i]) - coord.begin();
t2.update(idx, 1);
for(pii p : vec[i]) cnt[p.y] += t2.query(p.x, N - 1);
}
long ans = 0;
for(int i = 1; i <= n; i++) {
int now = (pos[i] && A[i] < B[i]) + cnt[i];
ans += now & 1 ? B[i] : A[i];
}
printf("%lld\n", ans);
return 0;
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:51:10: 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:53:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", A + i, B + i);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:62:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", Q + i);
~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9720 KB |
Output is correct |
2 |
Correct |
11 ms |
9848 KB |
Output is correct |
3 |
Correct |
12 ms |
9848 KB |
Output is correct |
4 |
Correct |
12 ms |
9976 KB |
Output is correct |
5 |
Correct |
12 ms |
9976 KB |
Output is correct |
6 |
Correct |
12 ms |
9952 KB |
Output is correct |
7 |
Correct |
12 ms |
9976 KB |
Output is correct |
8 |
Correct |
12 ms |
9976 KB |
Output is correct |
9 |
Correct |
11 ms |
9848 KB |
Output is correct |
10 |
Correct |
12 ms |
9848 KB |
Output is correct |
11 |
Correct |
11 ms |
9848 KB |
Output is correct |
12 |
Correct |
11 ms |
9848 KB |
Output is correct |
13 |
Correct |
12 ms |
9980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9720 KB |
Output is correct |
2 |
Correct |
11 ms |
9848 KB |
Output is correct |
3 |
Correct |
12 ms |
9848 KB |
Output is correct |
4 |
Correct |
12 ms |
9976 KB |
Output is correct |
5 |
Correct |
12 ms |
9976 KB |
Output is correct |
6 |
Correct |
12 ms |
9952 KB |
Output is correct |
7 |
Correct |
12 ms |
9976 KB |
Output is correct |
8 |
Correct |
12 ms |
9976 KB |
Output is correct |
9 |
Correct |
11 ms |
9848 KB |
Output is correct |
10 |
Correct |
12 ms |
9848 KB |
Output is correct |
11 |
Correct |
11 ms |
9848 KB |
Output is correct |
12 |
Correct |
11 ms |
9848 KB |
Output is correct |
13 |
Correct |
12 ms |
9980 KB |
Output is correct |
14 |
Correct |
26 ms |
10748 KB |
Output is correct |
15 |
Correct |
42 ms |
11692 KB |
Output is correct |
16 |
Correct |
59 ms |
11892 KB |
Output is correct |
17 |
Correct |
77 ms |
13300 KB |
Output is correct |
18 |
Correct |
80 ms |
12660 KB |
Output is correct |
19 |
Correct |
77 ms |
12708 KB |
Output is correct |
20 |
Correct |
76 ms |
12656 KB |
Output is correct |
21 |
Correct |
71 ms |
12912 KB |
Output is correct |
22 |
Correct |
56 ms |
12656 KB |
Output is correct |
23 |
Correct |
55 ms |
12400 KB |
Output is correct |
24 |
Correct |
52 ms |
12144 KB |
Output is correct |
25 |
Correct |
52 ms |
12656 KB |
Output is correct |
26 |
Correct |
55 ms |
11760 KB |
Output is correct |
27 |
Correct |
64 ms |
12016 KB |
Output is correct |
28 |
Correct |
60 ms |
12144 KB |
Output is correct |
29 |
Correct |
73 ms |
12396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
9720 KB |
Output is correct |
2 |
Correct |
11 ms |
9848 KB |
Output is correct |
3 |
Correct |
12 ms |
9848 KB |
Output is correct |
4 |
Correct |
12 ms |
9976 KB |
Output is correct |
5 |
Correct |
12 ms |
9976 KB |
Output is correct |
6 |
Correct |
12 ms |
9952 KB |
Output is correct |
7 |
Correct |
12 ms |
9976 KB |
Output is correct |
8 |
Correct |
12 ms |
9976 KB |
Output is correct |
9 |
Correct |
11 ms |
9848 KB |
Output is correct |
10 |
Correct |
12 ms |
9848 KB |
Output is correct |
11 |
Correct |
11 ms |
9848 KB |
Output is correct |
12 |
Correct |
11 ms |
9848 KB |
Output is correct |
13 |
Correct |
12 ms |
9980 KB |
Output is correct |
14 |
Correct |
26 ms |
10748 KB |
Output is correct |
15 |
Correct |
42 ms |
11692 KB |
Output is correct |
16 |
Correct |
59 ms |
11892 KB |
Output is correct |
17 |
Correct |
77 ms |
13300 KB |
Output is correct |
18 |
Correct |
80 ms |
12660 KB |
Output is correct |
19 |
Correct |
77 ms |
12708 KB |
Output is correct |
20 |
Correct |
76 ms |
12656 KB |
Output is correct |
21 |
Correct |
71 ms |
12912 KB |
Output is correct |
22 |
Correct |
56 ms |
12656 KB |
Output is correct |
23 |
Correct |
55 ms |
12400 KB |
Output is correct |
24 |
Correct |
52 ms |
12144 KB |
Output is correct |
25 |
Correct |
52 ms |
12656 KB |
Output is correct |
26 |
Correct |
55 ms |
11760 KB |
Output is correct |
27 |
Correct |
64 ms |
12016 KB |
Output is correct |
28 |
Correct |
60 ms |
12144 KB |
Output is correct |
29 |
Correct |
73 ms |
12396 KB |
Output is correct |
30 |
Correct |
118 ms |
11384 KB |
Output is correct |
31 |
Correct |
182 ms |
14064 KB |
Output is correct |
32 |
Correct |
258 ms |
17256 KB |
Output is correct |
33 |
Correct |
396 ms |
24036 KB |
Output is correct |
34 |
Correct |
62 ms |
10616 KB |
Output is correct |
35 |
Correct |
405 ms |
24040 KB |
Output is correct |
36 |
Correct |
407 ms |
23784 KB |
Output is correct |
37 |
Correct |
397 ms |
23652 KB |
Output is correct |
38 |
Correct |
402 ms |
23908 KB |
Output is correct |
39 |
Correct |
402 ms |
23652 KB |
Output is correct |
40 |
Correct |
335 ms |
23172 KB |
Output is correct |
41 |
Correct |
407 ms |
23572 KB |
Output is correct |
42 |
Correct |
398 ms |
23396 KB |
Output is correct |
43 |
Correct |
192 ms |
19680 KB |
Output is correct |
44 |
Correct |
202 ms |
20304 KB |
Output is correct |
45 |
Correct |
196 ms |
21856 KB |
Output is correct |
46 |
Correct |
260 ms |
22372 KB |
Output is correct |
47 |
Correct |
245 ms |
20452 KB |
Output is correct |
48 |
Correct |
284 ms |
20584 KB |
Output is correct |
49 |
Correct |
280 ms |
20964 KB |
Output is correct |