#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <unordered_set>
#include <map>
#include <queue>
#include <random>
#include <chrono>
#include <tuple>
#include <random>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double ld;
#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
const ll SIZE = 2e5 + 10, INF = 1e9 * 1e9;
vector<pair<ll, ll>> vec;
vector<ll> vc;
vector<ll> tree[SIZE * 4];
void build(int v, int l, int r) {
if (l == r - 1) {
tree[v].push_back(vc[l]);
}
else {
int mid = (l + r) / 2;
build(v * 2 + 1, l, mid);
build(v * 2 + 2, mid, r);
merge(tree[v * 2 + 1].begin(), tree[v * 2 + 1].end(), tree[v * 2 + 2].begin(), tree[v * 2 + 2].end(), std::back_inserter(tree[v]));
}
}
ll get(int v, int l, int r, int askl, int askr, ll minVal, ll maxVal) {
if (l >= askr || r <= askl) return 0;
if (l >= askl && r <= askr) {
ll mr = upper_bound(tree[v].begin(), tree[v].end(), maxVal) - tree[v].begin();
ll lw = lower_bound(tree[v].begin(), tree[v].end(), minVal) - tree[v].begin();
return mr - lw;
}
int mid = (l + r) / 2;
return get(v * 2 + 1, l, mid, askl, askr, minVal, maxVal) + get(v * 2 + 2, mid, r, askl, askr, minVal, maxVal);
}
ll get2(int v, int l, int r, int askl, int askr, int minVal, int maxVal) {
if (l == r - 1) {
return l;
}
else {
int mid = (l + r) / 2;
ll mr = upper_bound(tree[v * 2 + 2].begin(), tree[v * 2 + 2].end(), maxVal) - tree[v * 2 + 2].begin();
ll lw = lower_bound(tree[v * 2 + 2].begin(), tree[v * 2 + 2].end(), minVal) - tree[v * 2 + 2].begin();
if (mr - lw >= 1) {
return get2(v * 2 + 2, mid, r, askl, askr, minVal, maxVal);
}
else {
return get2(v * 2 + 1, l, mid, askl, askr, minVal, maxVal);
}
}
}
int main() {
fastInp;
ll n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
ll a, b;
cin >> a >> b;
vec.push_back({ a, b });
}
vc.push_back(-1);
for (int i = 0; i < k; i++) {
ll x;
cin >> x;
vc.push_back(x);
}
build(0, 0, vc.size());
ll s = 0;
for (int i = 0; i < n; i++) {
if (vec[i].first == vec[i].second) {
s += vec[i].first;
continue;
}
ll mn = min(vec[i].first, vec[i].second), mx = max(vec[i].first, vec[i].second);
ll ind = get2(0, 0, vc.size(), 0, vc.size(), mn, mx - 1);
if (ind + 1 == vc.size()) {
s += mx;
continue;
}
ll cnt = get(0, 0, vc.size(), ind + 1, vc.size(), mx, INF);
if (ind == 0) {
if (cnt % 2 == 0) {
s += vec[i].first;
}
else {
s += vec[i].second;
}
}
else {
if (cnt % 2 == 0) {
s += mx;
}
else {
s += mn;
}
}
}
cout << s;
return 0;
}
//1 7
//2 9
//1
//4
//5
//9
//0
//12
//15
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:98:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (ind + 1 == vc.size()) {
~~~~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
19200 KB |
Output is correct |
2 |
Correct |
14 ms |
19328 KB |
Output is correct |
3 |
Correct |
15 ms |
19328 KB |
Output is correct |
4 |
Correct |
16 ms |
19328 KB |
Output is correct |
5 |
Correct |
13 ms |
19328 KB |
Output is correct |
6 |
Correct |
15 ms |
19328 KB |
Output is correct |
7 |
Correct |
14 ms |
19328 KB |
Output is correct |
8 |
Correct |
13 ms |
19328 KB |
Output is correct |
9 |
Correct |
16 ms |
19328 KB |
Output is correct |
10 |
Correct |
17 ms |
19328 KB |
Output is correct |
11 |
Correct |
16 ms |
19328 KB |
Output is correct |
12 |
Correct |
13 ms |
19328 KB |
Output is correct |
13 |
Correct |
14 ms |
19328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
19200 KB |
Output is correct |
2 |
Correct |
14 ms |
19328 KB |
Output is correct |
3 |
Correct |
15 ms |
19328 KB |
Output is correct |
4 |
Correct |
16 ms |
19328 KB |
Output is correct |
5 |
Correct |
13 ms |
19328 KB |
Output is correct |
6 |
Correct |
15 ms |
19328 KB |
Output is correct |
7 |
Correct |
14 ms |
19328 KB |
Output is correct |
8 |
Correct |
13 ms |
19328 KB |
Output is correct |
9 |
Correct |
16 ms |
19328 KB |
Output is correct |
10 |
Correct |
17 ms |
19328 KB |
Output is correct |
11 |
Correct |
16 ms |
19328 KB |
Output is correct |
12 |
Correct |
13 ms |
19328 KB |
Output is correct |
13 |
Correct |
14 ms |
19328 KB |
Output is correct |
14 |
Correct |
38 ms |
21760 KB |
Output is correct |
15 |
Correct |
60 ms |
24572 KB |
Output is correct |
16 |
Correct |
86 ms |
26128 KB |
Output is correct |
17 |
Correct |
128 ms |
30320 KB |
Output is correct |
18 |
Correct |
127 ms |
30316 KB |
Output is correct |
19 |
Correct |
114 ms |
30448 KB |
Output is correct |
20 |
Correct |
120 ms |
30320 KB |
Output is correct |
21 |
Correct |
98 ms |
30320 KB |
Output is correct |
22 |
Correct |
74 ms |
29932 KB |
Output is correct |
23 |
Correct |
97 ms |
29936 KB |
Output is correct |
24 |
Correct |
80 ms |
29808 KB |
Output is correct |
25 |
Correct |
72 ms |
29980 KB |
Output is correct |
26 |
Correct |
97 ms |
30192 KB |
Output is correct |
27 |
Correct |
139 ms |
30428 KB |
Output is correct |
28 |
Correct |
104 ms |
30320 KB |
Output is correct |
29 |
Correct |
165 ms |
30320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
19200 KB |
Output is correct |
2 |
Correct |
14 ms |
19328 KB |
Output is correct |
3 |
Correct |
15 ms |
19328 KB |
Output is correct |
4 |
Correct |
16 ms |
19328 KB |
Output is correct |
5 |
Correct |
13 ms |
19328 KB |
Output is correct |
6 |
Correct |
15 ms |
19328 KB |
Output is correct |
7 |
Correct |
14 ms |
19328 KB |
Output is correct |
8 |
Correct |
13 ms |
19328 KB |
Output is correct |
9 |
Correct |
16 ms |
19328 KB |
Output is correct |
10 |
Correct |
17 ms |
19328 KB |
Output is correct |
11 |
Correct |
16 ms |
19328 KB |
Output is correct |
12 |
Correct |
13 ms |
19328 KB |
Output is correct |
13 |
Correct |
14 ms |
19328 KB |
Output is correct |
14 |
Correct |
38 ms |
21760 KB |
Output is correct |
15 |
Correct |
60 ms |
24572 KB |
Output is correct |
16 |
Correct |
86 ms |
26128 KB |
Output is correct |
17 |
Correct |
128 ms |
30320 KB |
Output is correct |
18 |
Correct |
127 ms |
30316 KB |
Output is correct |
19 |
Correct |
114 ms |
30448 KB |
Output is correct |
20 |
Correct |
120 ms |
30320 KB |
Output is correct |
21 |
Correct |
98 ms |
30320 KB |
Output is correct |
22 |
Correct |
74 ms |
29932 KB |
Output is correct |
23 |
Correct |
97 ms |
29936 KB |
Output is correct |
24 |
Correct |
80 ms |
29808 KB |
Output is correct |
25 |
Correct |
72 ms |
29980 KB |
Output is correct |
26 |
Correct |
97 ms |
30192 KB |
Output is correct |
27 |
Correct |
139 ms |
30428 KB |
Output is correct |
28 |
Correct |
104 ms |
30320 KB |
Output is correct |
29 |
Correct |
165 ms |
30320 KB |
Output is correct |
30 |
Correct |
205 ms |
67548 KB |
Output is correct |
31 |
Correct |
313 ms |
69100 KB |
Output is correct |
32 |
Correct |
449 ms |
70884 KB |
Output is correct |
33 |
Correct |
725 ms |
74336 KB |
Output is correct |
34 |
Correct |
169 ms |
67180 KB |
Output is correct |
35 |
Correct |
715 ms |
74208 KB |
Output is correct |
36 |
Correct |
706 ms |
74208 KB |
Output is correct |
37 |
Correct |
722 ms |
74336 KB |
Output is correct |
38 |
Correct |
692 ms |
74444 KB |
Output is correct |
39 |
Correct |
728 ms |
74336 KB |
Output is correct |
40 |
Correct |
542 ms |
74212 KB |
Output is correct |
41 |
Correct |
703 ms |
74208 KB |
Output is correct |
42 |
Correct |
725 ms |
74328 KB |
Output is correct |
43 |
Correct |
353 ms |
73604 KB |
Output is correct |
44 |
Correct |
354 ms |
73568 KB |
Output is correct |
45 |
Correct |
370 ms |
73568 KB |
Output is correct |
46 |
Correct |
358 ms |
72416 KB |
Output is correct |
47 |
Correct |
448 ms |
72288 KB |
Output is correct |
48 |
Correct |
639 ms |
74348 KB |
Output is correct |
49 |
Correct |
518 ms |
74332 KB |
Output is correct |