#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fileio(name) if (fopen(name".inp", "r")) freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout)
const int maxn = 2e5+4, inf = 1e18;
int n, q, a[maxn], b[maxn], t[maxn];
int ac[maxn], bc[maxn], tc[maxn];
int compress(int &x, vector<int> &v) {
return lower_bound(v.begin(), v.end(), x) - v.begin() + 1;
}
int st[maxn*12];
void update(int id, int l, int r, int i, int v) {
if (i < l || i > r) return;
if (l == r) {
st[id] = max(st[id], v);
return;
}
int mid = (l+r)>>1;
update(id*2, l, mid, i, v);
update(id*2+1, mid+1, r, i, v);
st[id] = max(st[id*2], st[id*2+1]);
}
int get(int id, int l, int r, int a, int b) {
if (r < a || b < l) return -inf;
if (a <= l && r <= b) {
return st[id];
}
int mid = (l+r)>>1;
return max(get(id*2, l, mid, a, b), get(id*2+1, mid+1, r, a, b));
}
int bit[maxn*3];
void add(int i, int v) {
while (i <= n*2+q) {
bit[i] += v;
i += (i & (-i));
}
}
int qq(int i) {
int res = 0;
while (i > 0) {
res += bit[i];
i -= (i & (-i));
}
return res;
}
int getr(int l, int r) {
return qq(r) - qq(l-1);
}
struct query {
int idx, x;
bool operator <(const query &A) const {
return x > A.x;
}
} Q[maxn];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
fileio("");
// freopen("debug.txt", "w", stderr);
cin >> n >> q;
vector<int> sech;
for (int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
sech.push_back(a[i]);
sech.push_back(b[i]);
}
for (int i = 1; i <= q; i++) {
cin >> t[i];
sech.push_back(t[i]);
}
sort(sech.begin(), sech.end());
for (int i = 1; i <= n; i++) {
ac[i] = compress(a[i], sech);
bc[i] = compress(b[i], sech);
}
for (int i = 1; i <= q; i++) {
tc[i] = compress(t[i], sech);
update(1, 1, n*2+q, tc[i], i);
}
for (int i = 1; i <= n; i++) {
Q[i] = {i, get(1, 1, n*2+q, min(ac[i], bc[i]), max(ac[i], bc[i])-1)};
}
sort(Q+1, Q+1+n);
int j = q, res = 0;
for (int i = 1; i <= n; i++) {
while (j > 0 && j > Q[i].x) {
add(tc[j], 1);
j--;
}
int id = Q[i].idx;
if (Q[i].x > 0) {
int p = (1 + getr(max(ac[id], bc[id]), n*2+q)) % 2;
int y[2] = {min(a[id], b[id]), max(a[id], b[id])};
res += y[p];
} else {
int p = getr(max(ac[id], bc[id]), n*2+q) % 2;
int y[2] = {a[id], b[id]};
res += y[p];
}
}
cout << res;
return 0 ^ 0;
}
Compilation message
fortune_telling2.cpp: In function 'int32_t main()':
fortune_telling2.cpp:5:57: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | #define fileio(name) if (fopen(name".inp", "r")) freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:70:5: note: in expansion of macro 'fileio'
70 | fileio("");
| ^~~~~~
fortune_telling2.cpp:5:90: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
5 | #define fileio(name) if (fopen(name".inp", "r")) freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:70:5: note: in expansion of macro 'fileio'
70 | fileio("");
| ^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10832 KB |
Output is correct |
2 |
Correct |
3 ms |
10832 KB |
Output is correct |
3 |
Correct |
3 ms |
10832 KB |
Output is correct |
4 |
Correct |
3 ms |
10736 KB |
Output is correct |
5 |
Correct |
3 ms |
10980 KB |
Output is correct |
6 |
Correct |
3 ms |
11000 KB |
Output is correct |
7 |
Correct |
3 ms |
10832 KB |
Output is correct |
8 |
Correct |
2 ms |
10832 KB |
Output is correct |
9 |
Correct |
3 ms |
10832 KB |
Output is correct |
10 |
Correct |
2 ms |
10832 KB |
Output is correct |
11 |
Correct |
2 ms |
12880 KB |
Output is correct |
12 |
Correct |
3 ms |
12880 KB |
Output is correct |
13 |
Correct |
3 ms |
12880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10832 KB |
Output is correct |
2 |
Correct |
3 ms |
10832 KB |
Output is correct |
3 |
Correct |
3 ms |
10832 KB |
Output is correct |
4 |
Correct |
3 ms |
10736 KB |
Output is correct |
5 |
Correct |
3 ms |
10980 KB |
Output is correct |
6 |
Correct |
3 ms |
11000 KB |
Output is correct |
7 |
Correct |
3 ms |
10832 KB |
Output is correct |
8 |
Correct |
2 ms |
10832 KB |
Output is correct |
9 |
Correct |
3 ms |
10832 KB |
Output is correct |
10 |
Correct |
2 ms |
10832 KB |
Output is correct |
11 |
Correct |
2 ms |
12880 KB |
Output is correct |
12 |
Correct |
3 ms |
12880 KB |
Output is correct |
13 |
Correct |
3 ms |
12880 KB |
Output is correct |
14 |
Correct |
14 ms |
12244 KB |
Output is correct |
15 |
Correct |
26 ms |
15592 KB |
Output is correct |
16 |
Correct |
40 ms |
18884 KB |
Output is correct |
17 |
Correct |
58 ms |
19428 KB |
Output is correct |
18 |
Correct |
68 ms |
19432 KB |
Output is correct |
19 |
Correct |
53 ms |
18664 KB |
Output is correct |
20 |
Correct |
54 ms |
19652 KB |
Output is correct |
21 |
Correct |
52 ms |
18628 KB |
Output is correct |
22 |
Correct |
47 ms |
18084 KB |
Output is correct |
23 |
Correct |
56 ms |
18428 KB |
Output is correct |
24 |
Correct |
55 ms |
18620 KB |
Output is correct |
25 |
Correct |
58 ms |
18144 KB |
Output is correct |
26 |
Correct |
47 ms |
20012 KB |
Output is correct |
27 |
Correct |
68 ms |
20780 KB |
Output is correct |
28 |
Correct |
53 ms |
20284 KB |
Output is correct |
29 |
Correct |
56 ms |
20608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10832 KB |
Output is correct |
2 |
Correct |
3 ms |
10832 KB |
Output is correct |
3 |
Correct |
3 ms |
10832 KB |
Output is correct |
4 |
Correct |
3 ms |
10736 KB |
Output is correct |
5 |
Correct |
3 ms |
10980 KB |
Output is correct |
6 |
Correct |
3 ms |
11000 KB |
Output is correct |
7 |
Correct |
3 ms |
10832 KB |
Output is correct |
8 |
Correct |
2 ms |
10832 KB |
Output is correct |
9 |
Correct |
3 ms |
10832 KB |
Output is correct |
10 |
Correct |
2 ms |
10832 KB |
Output is correct |
11 |
Correct |
2 ms |
12880 KB |
Output is correct |
12 |
Correct |
3 ms |
12880 KB |
Output is correct |
13 |
Correct |
3 ms |
12880 KB |
Output is correct |
14 |
Correct |
14 ms |
12244 KB |
Output is correct |
15 |
Correct |
26 ms |
15592 KB |
Output is correct |
16 |
Correct |
40 ms |
18884 KB |
Output is correct |
17 |
Correct |
58 ms |
19428 KB |
Output is correct |
18 |
Correct |
68 ms |
19432 KB |
Output is correct |
19 |
Correct |
53 ms |
18664 KB |
Output is correct |
20 |
Correct |
54 ms |
19652 KB |
Output is correct |
21 |
Correct |
52 ms |
18628 KB |
Output is correct |
22 |
Correct |
47 ms |
18084 KB |
Output is correct |
23 |
Correct |
56 ms |
18428 KB |
Output is correct |
24 |
Correct |
55 ms |
18620 KB |
Output is correct |
25 |
Correct |
58 ms |
18144 KB |
Output is correct |
26 |
Correct |
47 ms |
20012 KB |
Output is correct |
27 |
Correct |
68 ms |
20780 KB |
Output is correct |
28 |
Correct |
53 ms |
20284 KB |
Output is correct |
29 |
Correct |
56 ms |
20608 KB |
Output is correct |
30 |
Correct |
124 ms |
22452 KB |
Output is correct |
31 |
Correct |
162 ms |
31936 KB |
Output is correct |
32 |
Correct |
257 ms |
33976 KB |
Output is correct |
33 |
Correct |
422 ms |
47808 KB |
Output is correct |
34 |
Correct |
120 ms |
20324 KB |
Output is correct |
35 |
Correct |
471 ms |
47812 KB |
Output is correct |
36 |
Correct |
524 ms |
47820 KB |
Output is correct |
37 |
Correct |
456 ms |
47996 KB |
Output is correct |
38 |
Correct |
387 ms |
46000 KB |
Output is correct |
39 |
Correct |
424 ms |
47872 KB |
Output is correct |
40 |
Correct |
355 ms |
45588 KB |
Output is correct |
41 |
Correct |
339 ms |
46008 KB |
Output is correct |
42 |
Correct |
333 ms |
47888 KB |
Output is correct |
43 |
Correct |
244 ms |
37808 KB |
Output is correct |
44 |
Correct |
220 ms |
39628 KB |
Output is correct |
45 |
Correct |
216 ms |
38844 KB |
Output is correct |
46 |
Correct |
232 ms |
44004 KB |
Output is correct |
47 |
Correct |
226 ms |
45748 KB |
Output is correct |
48 |
Correct |
266 ms |
44996 KB |
Output is correct |
49 |
Correct |
251 ms |
44396 KB |
Output is correct |