// Call my Name and Save me from The Dark
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
#define SZ(x) (int) x.size()
#define F first
#define S second
#define lc id << 1
#define rc lc | 1
const int N = 1e6 + 10;
int F[N], seg[N << 2], M[N], A[N], B[N], C[N], n, q;
vector<int> cp, vec[2][N];
void modify(int p, int x, int id = 1, int l = 0, int r = SZ(cp)) {
if (r - l < 2) {
seg[id] = max(seg[id], x);
return;
}
int mid = (l + r) >> 1;
if (p < mid) modify(p, x, lc, l, mid);
else modify(p, x, rc, mid, r);
seg[id] = max(seg[lc], seg[rc]);
}
int get(int ql, int qr, int id = 1, int l = 0, int r = SZ(cp)) {
if (qr <= l || r <= ql) return 0;
if (ql <= l && r <= qr) return seg[id];
int mid = (l + r) >> 1;
return max(get(ql, qr, lc, l, mid), get(ql, qr, rc, mid, r));
}
int id(int x) {
return lower_bound(cp.begin(), cp.end(), x) - cp.begin();
}
int get(int p) {
int ret = 0;
for (p++; p; p -= p & -p)
ret += F[p];
return ret;
}
void upd(int p, int x) {
for (p++; p < N; p += p & -p)
F[p] += x;
}
int main() {
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++)
scanf("%d%d", &A[i], &B[i]), cp.push_back(A[i]), cp.push_back(B[i]);
for (int i = 1; i <= q; i++)
scanf("%d", &C[i]), cp.push_back(C[i]);
sort(cp.begin(), cp.end());
cp.resize(unique(cp.begin(), cp.end()) - cp.begin());
for (int i = 1; i <= q; i++)
modify(id(C[i]), i);
for (int i = 1; i <= n; i++) {
M[i] = get(id(min(A[i], B[i])), id(max(A[i], B[i])));
}
for (int i = 1; i <= n; i++) {
vec[0][id(max(A[i], B[i]))].push_back(i);
}
ll tot = 0;
for (int i = 1; i <= q; i++)
vec[1][id(C[i])].push_back(i);
for (int i = SZ(cp) - 1; ~i; i--) {
for (int j : vec[1][i]) upd(j, 1);
for (int j : vec[0][i]) {
int x = get(N - 2) - get(M[j]);
if (!M[j]) {
tot += (x & 1 ? B[j] : A[j]);
} else {
if (A[j] > B[j]) swap(A[j], B[j]);
tot += (x & 1 ? A[j] : B[j]);
}
}
}
printf("%lld\n", tot);
return 0;
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:54:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
54 | scanf("%d%d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~
fortune_telling2.cpp:56:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
56 | scanf("%d%d", &A[i], &B[i]), cp.push_back(A[i]), cp.push_back(B[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:58:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
58 | scanf("%d", &C[i]), cp.push_back(C[i]);
| ~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
47468 KB |
Output is correct |
2 |
Correct |
34 ms |
47468 KB |
Output is correct |
3 |
Correct |
34 ms |
47468 KB |
Output is correct |
4 |
Correct |
34 ms |
47468 KB |
Output is correct |
5 |
Correct |
35 ms |
47596 KB |
Output is correct |
6 |
Correct |
34 ms |
47468 KB |
Output is correct |
7 |
Correct |
34 ms |
47468 KB |
Output is correct |
8 |
Correct |
34 ms |
47468 KB |
Output is correct |
9 |
Correct |
35 ms |
47468 KB |
Output is correct |
10 |
Correct |
33 ms |
47468 KB |
Output is correct |
11 |
Correct |
34 ms |
47468 KB |
Output is correct |
12 |
Correct |
34 ms |
47468 KB |
Output is correct |
13 |
Correct |
34 ms |
47616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
47468 KB |
Output is correct |
2 |
Correct |
34 ms |
47468 KB |
Output is correct |
3 |
Correct |
34 ms |
47468 KB |
Output is correct |
4 |
Correct |
34 ms |
47468 KB |
Output is correct |
5 |
Correct |
35 ms |
47596 KB |
Output is correct |
6 |
Correct |
34 ms |
47468 KB |
Output is correct |
7 |
Correct |
34 ms |
47468 KB |
Output is correct |
8 |
Correct |
34 ms |
47468 KB |
Output is correct |
9 |
Correct |
35 ms |
47468 KB |
Output is correct |
10 |
Correct |
33 ms |
47468 KB |
Output is correct |
11 |
Correct |
34 ms |
47468 KB |
Output is correct |
12 |
Correct |
34 ms |
47468 KB |
Output is correct |
13 |
Correct |
34 ms |
47616 KB |
Output is correct |
14 |
Correct |
55 ms |
48876 KB |
Output is correct |
15 |
Correct |
79 ms |
50280 KB |
Output is correct |
16 |
Correct |
114 ms |
52068 KB |
Output is correct |
17 |
Correct |
131 ms |
53348 KB |
Output is correct |
18 |
Correct |
134 ms |
53348 KB |
Output is correct |
19 |
Correct |
135 ms |
53348 KB |
Output is correct |
20 |
Correct |
133 ms |
53348 KB |
Output is correct |
21 |
Correct |
125 ms |
53220 KB |
Output is correct |
22 |
Correct |
103 ms |
52580 KB |
Output is correct |
23 |
Correct |
102 ms |
51940 KB |
Output is correct |
24 |
Correct |
100 ms |
51940 KB |
Output is correct |
25 |
Correct |
108 ms |
52652 KB |
Output is correct |
26 |
Correct |
110 ms |
52880 KB |
Output is correct |
27 |
Correct |
119 ms |
53488 KB |
Output is correct |
28 |
Correct |
114 ms |
53348 KB |
Output is correct |
29 |
Correct |
122 ms |
53476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
47468 KB |
Output is correct |
2 |
Correct |
34 ms |
47468 KB |
Output is correct |
3 |
Correct |
34 ms |
47468 KB |
Output is correct |
4 |
Correct |
34 ms |
47468 KB |
Output is correct |
5 |
Correct |
35 ms |
47596 KB |
Output is correct |
6 |
Correct |
34 ms |
47468 KB |
Output is correct |
7 |
Correct |
34 ms |
47468 KB |
Output is correct |
8 |
Correct |
34 ms |
47468 KB |
Output is correct |
9 |
Correct |
35 ms |
47468 KB |
Output is correct |
10 |
Correct |
33 ms |
47468 KB |
Output is correct |
11 |
Correct |
34 ms |
47468 KB |
Output is correct |
12 |
Correct |
34 ms |
47468 KB |
Output is correct |
13 |
Correct |
34 ms |
47616 KB |
Output is correct |
14 |
Correct |
55 ms |
48876 KB |
Output is correct |
15 |
Correct |
79 ms |
50280 KB |
Output is correct |
16 |
Correct |
114 ms |
52068 KB |
Output is correct |
17 |
Correct |
131 ms |
53348 KB |
Output is correct |
18 |
Correct |
134 ms |
53348 KB |
Output is correct |
19 |
Correct |
135 ms |
53348 KB |
Output is correct |
20 |
Correct |
133 ms |
53348 KB |
Output is correct |
21 |
Correct |
125 ms |
53220 KB |
Output is correct |
22 |
Correct |
103 ms |
52580 KB |
Output is correct |
23 |
Correct |
102 ms |
51940 KB |
Output is correct |
24 |
Correct |
100 ms |
51940 KB |
Output is correct |
25 |
Correct |
108 ms |
52652 KB |
Output is correct |
26 |
Correct |
110 ms |
52880 KB |
Output is correct |
27 |
Correct |
119 ms |
53488 KB |
Output is correct |
28 |
Correct |
114 ms |
53348 KB |
Output is correct |
29 |
Correct |
122 ms |
53476 KB |
Output is correct |
30 |
Correct |
267 ms |
60640 KB |
Output is correct |
31 |
Correct |
361 ms |
65500 KB |
Output is correct |
32 |
Correct |
491 ms |
69268 KB |
Output is correct |
33 |
Correct |
758 ms |
80368 KB |
Output is correct |
34 |
Correct |
253 ms |
60000 KB |
Output is correct |
35 |
Correct |
751 ms |
80340 KB |
Output is correct |
36 |
Correct |
766 ms |
80212 KB |
Output is correct |
37 |
Correct |
763 ms |
80356 KB |
Output is correct |
38 |
Correct |
749 ms |
80332 KB |
Output is correct |
39 |
Correct |
747 ms |
80212 KB |
Output is correct |
40 |
Correct |
694 ms |
79956 KB |
Output is correct |
41 |
Correct |
756 ms |
80212 KB |
Output is correct |
42 |
Correct |
756 ms |
80504 KB |
Output is correct |
43 |
Correct |
469 ms |
75608 KB |
Output is correct |
44 |
Correct |
466 ms |
75824 KB |
Output is correct |
45 |
Correct |
464 ms |
76628 KB |
Output is correct |
46 |
Correct |
474 ms |
72660 KB |
Output is correct |
47 |
Correct |
420 ms |
69716 KB |
Output is correct |
48 |
Correct |
604 ms |
76116 KB |
Output is correct |
49 |
Correct |
546 ms |
76116 KB |
Output is correct |