#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 |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
548 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
532 KB |
Output is correct |
9 |
Correct |
1 ms |
472 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 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 |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
548 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
532 KB |
Output is correct |
9 |
Correct |
1 ms |
472 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
13 ms |
2356 KB |
Output is correct |
15 |
Correct |
27 ms |
4192 KB |
Output is correct |
16 |
Correct |
50 ms |
6612 KB |
Output is correct |
17 |
Correct |
60 ms |
8044 KB |
Output is correct |
18 |
Correct |
55 ms |
8008 KB |
Output is correct |
19 |
Correct |
54 ms |
7248 KB |
Output is correct |
20 |
Correct |
62 ms |
8076 KB |
Output is correct |
21 |
Correct |
52 ms |
7084 KB |
Output is correct |
22 |
Correct |
42 ms |
6692 KB |
Output is correct |
23 |
Correct |
40 ms |
6708 KB |
Output is correct |
24 |
Correct |
41 ms |
6972 KB |
Output is correct |
25 |
Correct |
44 ms |
6724 KB |
Output is correct |
26 |
Correct |
42 ms |
6764 KB |
Output is correct |
27 |
Correct |
51 ms |
8032 KB |
Output is correct |
28 |
Correct |
43 ms |
7088 KB |
Output is correct |
29 |
Correct |
49 ms |
8012 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 |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
548 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
532 KB |
Output is correct |
9 |
Correct |
1 ms |
472 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
13 ms |
2356 KB |
Output is correct |
15 |
Correct |
27 ms |
4192 KB |
Output is correct |
16 |
Correct |
50 ms |
6612 KB |
Output is correct |
17 |
Correct |
60 ms |
8044 KB |
Output is correct |
18 |
Correct |
55 ms |
8008 KB |
Output is correct |
19 |
Correct |
54 ms |
7248 KB |
Output is correct |
20 |
Correct |
62 ms |
8076 KB |
Output is correct |
21 |
Correct |
52 ms |
7084 KB |
Output is correct |
22 |
Correct |
42 ms |
6692 KB |
Output is correct |
23 |
Correct |
40 ms |
6708 KB |
Output is correct |
24 |
Correct |
41 ms |
6972 KB |
Output is correct |
25 |
Correct |
44 ms |
6724 KB |
Output is correct |
26 |
Correct |
42 ms |
6764 KB |
Output is correct |
27 |
Correct |
51 ms |
8032 KB |
Output is correct |
28 |
Correct |
43 ms |
7088 KB |
Output is correct |
29 |
Correct |
49 ms |
8012 KB |
Output is correct |
30 |
Correct |
112 ms |
13728 KB |
Output is correct |
31 |
Correct |
167 ms |
21720 KB |
Output is correct |
32 |
Correct |
267 ms |
26600 KB |
Output is correct |
33 |
Correct |
363 ms |
44564 KB |
Output is correct |
34 |
Correct |
93 ms |
11168 KB |
Output is correct |
35 |
Correct |
357 ms |
44476 KB |
Output is correct |
36 |
Correct |
361 ms |
44572 KB |
Output is correct |
37 |
Correct |
379 ms |
44480 KB |
Output is correct |
38 |
Correct |
370 ms |
39992 KB |
Output is correct |
39 |
Correct |
365 ms |
44536 KB |
Output is correct |
40 |
Correct |
328 ms |
39516 KB |
Output is correct |
41 |
Correct |
345 ms |
40296 KB |
Output is correct |
42 |
Correct |
353 ms |
41500 KB |
Output is correct |
43 |
Correct |
221 ms |
30080 KB |
Output is correct |
44 |
Correct |
237 ms |
30488 KB |
Output is correct |
45 |
Correct |
223 ms |
32184 KB |
Output is correct |
46 |
Correct |
287 ms |
38068 KB |
Output is correct |
47 |
Correct |
256 ms |
42680 KB |
Output is correct |
48 |
Correct |
323 ms |
38068 KB |
Output is correct |
49 |
Correct |
257 ms |
37640 KB |
Output is correct |