#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 2;
const int inf = 2e9;
int st[12 * N];
void Add(int node, int l, int r, int x, int y) {
smax(st[node], y);
if (l == r) return;
int mid = l + r >> 1;
if (x <= mid) Add(2 * node, l, mid, x, y);
else Add(2 * node + 1, mid + 1, r, x, y);
}
int Get(int node, int l, int r, int ql, int qr) {
if (r < ql || qr < l || ql > qr) return -1;
if (ql <= l && r <= qr) return st[node];
int mid = l + r >> 1;
return max(Get(2 * node, l, mid, ql, qr), Get(2 * node + 1, mid + 1, r, ql, qr));
}
int bit[3 * N];
void AddBit(int x) {
x += 1;
for (; x > 0; x -= x & (-x)) bit[x]++;
}
int GetBit(int x) {
x += 1; int ans = 0;
for (; x < 3 * N; x += x & (-x)) ans += bit[x];
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n), b(n), c(k);
vector<int> all;
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
all.push_back(a[i]);
all.push_back(b[i]);
}
for (int i = 0; i < k; i++) {
cin >> c[i];
all.push_back(c[i]);
}
sort(all.begin(), all.end());
all.erase(unique(all.begin(), all.end()), all.end());
map<int, int> mp;
int sz = all.size();
for (int i = 0; i < all.size(); i++) {
mp[all[i]] = i;
}
for (int i = 0; i < n; i++) {
a[i] = mp[a[i]];
b[i] = mp[b[i]];
}
for (int i = 0; i < k; i++) {
c[i] = mp[c[i]];
}
for (int i = 1; i <= 12 * n; i++) st[i] = -1;
for (int i = 0; i < k; i++) Add(1, 0, sz - 1, c[i], i);
vector<int> v(n);
vector<array<int, 3>> svi;
for (int i = 0; i < n; i++) {
int A = min(a[i], b[i]); int B = max(a[i], b[i]);
v[i] = Get(1, 0, sz - 1, A, B - 1);
svi.push_back({v[i], 1, i});
}
for (int i = 0; i < k; i++) svi.push_back({i, 0, i});
sort(svi.begin(), svi.end(), [&] (array<int, 3> c1, array<int, 3> c2) {
if (c1[0] != c2[0]) return c1[0] > c2[0];
return c1[1] < c2[1];
});
ll fina = 0;
for (auto u : svi) {
if (u[1] == 0) AddBit(c[u[2]]);
else {
int i = u[2];
int A = min(a[i], b[i]);
int B = max(a[i], b[i]);
int x = GetBit(B) % 2;
if (v[i] == -1) {
if (x) fina += all[b[i]];
else fina += all[a[i]];
continue;
}
if (x) fina += all[A];
else fina += all[B];
}
}
for (int i = 0; i < n; i++) {
int cur = a[i];
for (int j = 0; j < k; j++) {
if (cur <= c[j]) cur = a[i] + b[i] - cur;
}
}
cout << fina << en;
return 0;
}
/*
5 5
2 6
3 8
7 2
5 3
4 3
3
2
7
9
6*/
Compilation message
fortune_telling2.cpp: In function 'void Add(int, int, int, int, int)':
fortune_telling2.cpp:15:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
15 | int mid = l + r >> 1;
| ~~^~~
fortune_telling2.cpp: In function 'int Get(int, int, int, int, int)':
fortune_telling2.cpp:22:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
22 | int mid = l + r >> 1;
| ~~^~~
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:56:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | for (int i = 0; i < all.size(); i++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
632 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
508 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
632 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
508 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
508 KB |
Output is correct |
14 |
Correct |
23 ms |
3244 KB |
Output is correct |
15 |
Correct |
44 ms |
6100 KB |
Output is correct |
16 |
Correct |
69 ms |
9024 KB |
Output is correct |
17 |
Correct |
111 ms |
12040 KB |
Output is correct |
18 |
Correct |
97 ms |
12036 KB |
Output is correct |
19 |
Correct |
96 ms |
11980 KB |
Output is correct |
20 |
Correct |
104 ms |
12044 KB |
Output is correct |
21 |
Correct |
96 ms |
11924 KB |
Output is correct |
22 |
Correct |
67 ms |
9696 KB |
Output is correct |
23 |
Correct |
59 ms |
8712 KB |
Output is correct |
24 |
Correct |
52 ms |
7992 KB |
Output is correct |
25 |
Correct |
64 ms |
10192 KB |
Output is correct |
26 |
Correct |
62 ms |
9192 KB |
Output is correct |
27 |
Correct |
76 ms |
10164 KB |
Output is correct |
28 |
Correct |
68 ms |
10056 KB |
Output is correct |
29 |
Correct |
80 ms |
11036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
632 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
508 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
508 KB |
Output is correct |
14 |
Correct |
23 ms |
3244 KB |
Output is correct |
15 |
Correct |
44 ms |
6100 KB |
Output is correct |
16 |
Correct |
69 ms |
9024 KB |
Output is correct |
17 |
Correct |
111 ms |
12040 KB |
Output is correct |
18 |
Correct |
97 ms |
12036 KB |
Output is correct |
19 |
Correct |
96 ms |
11980 KB |
Output is correct |
20 |
Correct |
104 ms |
12044 KB |
Output is correct |
21 |
Correct |
96 ms |
11924 KB |
Output is correct |
22 |
Correct |
67 ms |
9696 KB |
Output is correct |
23 |
Correct |
59 ms |
8712 KB |
Output is correct |
24 |
Correct |
52 ms |
7992 KB |
Output is correct |
25 |
Correct |
64 ms |
10192 KB |
Output is correct |
26 |
Correct |
62 ms |
9192 KB |
Output is correct |
27 |
Correct |
76 ms |
10164 KB |
Output is correct |
28 |
Correct |
68 ms |
10056 KB |
Output is correct |
29 |
Correct |
80 ms |
11036 KB |
Output is correct |
30 |
Correct |
205 ms |
20680 KB |
Output is correct |
31 |
Correct |
314 ms |
29528 KB |
Output is correct |
32 |
Correct |
482 ms |
38832 KB |
Output is correct |
33 |
Correct |
815 ms |
59220 KB |
Output is correct |
34 |
Correct |
170 ms |
19084 KB |
Output is correct |
35 |
Correct |
803 ms |
59252 KB |
Output is correct |
36 |
Correct |
812 ms |
59288 KB |
Output is correct |
37 |
Correct |
837 ms |
59224 KB |
Output is correct |
38 |
Correct |
803 ms |
59340 KB |
Output is correct |
39 |
Correct |
795 ms |
59188 KB |
Output is correct |
40 |
Correct |
677 ms |
59116 KB |
Output is correct |
41 |
Correct |
839 ms |
59404 KB |
Output is correct |
42 |
Correct |
837 ms |
59268 KB |
Output is correct |
43 |
Correct |
416 ms |
57816 KB |
Output is correct |
44 |
Correct |
411 ms |
57948 KB |
Output is correct |
45 |
Correct |
412 ms |
58224 KB |
Output is correct |
46 |
Correct |
386 ms |
42428 KB |
Output is correct |
47 |
Correct |
306 ms |
37648 KB |
Output is correct |
48 |
Correct |
497 ms |
49316 KB |
Output is correct |
49 |
Correct |
473 ms |
49204 KB |
Output is correct |