#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define long long long
#define pii pair<int, int>
#define x first
#define y second
const int N = 2e5+5;
int n, k;
long ans;
vector<pii> que;
vector<int> coor;
struct item {
int v;
item *l, *r;
item() {}
item(int v, item *l, item *r) : v(v), l(l), r(r) {}
};
using pitem = item*;
#define vari pitem p, int l = 0, int r = coor.size()
#define mid ((l + r) >> 1)
#define lb p->l, l, mid
#define rb p->r, mid+1, r
pitem newleaf(int v) { return new item(v, nullptr, nullptr); }
pitem newpar(pitem l, pitem r) { return new item(l->v + r->v, l, r); }
pitem build(int l = 0, int r = coor.size()) {
if(l == r) return newleaf(0);
return newpar(build(l, mid), build(mid+1, r));
}
pitem update(int x, vari) {
if(l == r) return newleaf(p->v + 1);
if(x <= mid) return newpar(update(x, lb), p->r);
else return newpar(p->l, update(x, rb));
}
int query(int x, vari) {
if(x <= l) return p->v;
if(x > mid) return query(x, rb);
return query(x, lb) + query(x, rb);
}
int A[N];
pitem ver[N];
int f(int m, int v) {
return query(lower_bound(all(coor), v) - coor.begin(), ver[m]);
}
int main() {
scanf("%d %d", &n, &k);
for(int i = 0, a, b; i < n; ++i) {
scanf("%d %d", &a, &b);
que.emplace_back(a, b);
}
coor.resize(1);
for(int i = 1; i <= k; ++i) {
scanf("%d", A+i);
coor.emplace_back(A[i]);
}
sort(all(coor));
coor.resize(unique(all(coor)) - coor.begin());
ver[k+1] = build();
for(int i = k; ~i; --i) ver[i] = update(lower_bound(all(coor), A[i]) - coor.begin(), ver[i+1]);
for(auto x : que) {
int a, b; tie(a, b) = x;
int l = 0, r = k;
while(l < r) {
int m = (l + r + 1) >> 1;
if(f(m, a) != f(m, b)) l = m;
else r = m-1;
}
if(l == 0) ans += f(0, a) & 1 ? b : a;
else ans += f(l, max(a, b)) & 1 ? min(a, b) : max(a, b);
}
printf("%lld\n", ans);
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:60:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~~
fortune_telling2.cpp:62:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~~
fortune_telling2.cpp:67:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", A+i);
~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
760 KB |
Output is correct |
2 |
Correct |
4 ms |
896 KB |
Output is correct |
3 |
Correct |
5 ms |
964 KB |
Output is correct |
4 |
Correct |
5 ms |
964 KB |
Output is correct |
5 |
Correct |
5 ms |
1088 KB |
Output is correct |
6 |
Correct |
5 ms |
1088 KB |
Output is correct |
7 |
Correct |
5 ms |
1100 KB |
Output is correct |
8 |
Correct |
5 ms |
1132 KB |
Output is correct |
9 |
Correct |
4 ms |
1212 KB |
Output is correct |
10 |
Correct |
5 ms |
1416 KB |
Output is correct |
11 |
Correct |
7 ms |
1416 KB |
Output is correct |
12 |
Correct |
5 ms |
1416 KB |
Output is correct |
13 |
Correct |
5 ms |
1416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
760 KB |
Output is correct |
2 |
Correct |
4 ms |
896 KB |
Output is correct |
3 |
Correct |
5 ms |
964 KB |
Output is correct |
4 |
Correct |
5 ms |
964 KB |
Output is correct |
5 |
Correct |
5 ms |
1088 KB |
Output is correct |
6 |
Correct |
5 ms |
1088 KB |
Output is correct |
7 |
Correct |
5 ms |
1100 KB |
Output is correct |
8 |
Correct |
5 ms |
1132 KB |
Output is correct |
9 |
Correct |
4 ms |
1212 KB |
Output is correct |
10 |
Correct |
5 ms |
1416 KB |
Output is correct |
11 |
Correct |
7 ms |
1416 KB |
Output is correct |
12 |
Correct |
5 ms |
1416 KB |
Output is correct |
13 |
Correct |
5 ms |
1416 KB |
Output is correct |
14 |
Correct |
52 ms |
6660 KB |
Output is correct |
15 |
Correct |
121 ms |
13268 KB |
Output is correct |
16 |
Correct |
198 ms |
20260 KB |
Output is correct |
17 |
Correct |
300 ms |
26680 KB |
Output is correct |
18 |
Correct |
321 ms |
26688 KB |
Output is correct |
19 |
Correct |
304 ms |
26688 KB |
Output is correct |
20 |
Correct |
301 ms |
26688 KB |
Output is correct |
21 |
Correct |
251 ms |
26688 KB |
Output is correct |
22 |
Correct |
175 ms |
26688 KB |
Output is correct |
23 |
Correct |
213 ms |
26688 KB |
Output is correct |
24 |
Correct |
196 ms |
26688 KB |
Output is correct |
25 |
Correct |
174 ms |
26688 KB |
Output is correct |
26 |
Correct |
233 ms |
26688 KB |
Output is correct |
27 |
Correct |
260 ms |
26688 KB |
Output is correct |
28 |
Correct |
236 ms |
26688 KB |
Output is correct |
29 |
Correct |
311 ms |
26688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
760 KB |
Output is correct |
2 |
Correct |
4 ms |
896 KB |
Output is correct |
3 |
Correct |
5 ms |
964 KB |
Output is correct |
4 |
Correct |
5 ms |
964 KB |
Output is correct |
5 |
Correct |
5 ms |
1088 KB |
Output is correct |
6 |
Correct |
5 ms |
1088 KB |
Output is correct |
7 |
Correct |
5 ms |
1100 KB |
Output is correct |
8 |
Correct |
5 ms |
1132 KB |
Output is correct |
9 |
Correct |
4 ms |
1212 KB |
Output is correct |
10 |
Correct |
5 ms |
1416 KB |
Output is correct |
11 |
Correct |
7 ms |
1416 KB |
Output is correct |
12 |
Correct |
5 ms |
1416 KB |
Output is correct |
13 |
Correct |
5 ms |
1416 KB |
Output is correct |
14 |
Correct |
52 ms |
6660 KB |
Output is correct |
15 |
Correct |
121 ms |
13268 KB |
Output is correct |
16 |
Correct |
198 ms |
20260 KB |
Output is correct |
17 |
Correct |
300 ms |
26680 KB |
Output is correct |
18 |
Correct |
321 ms |
26688 KB |
Output is correct |
19 |
Correct |
304 ms |
26688 KB |
Output is correct |
20 |
Correct |
301 ms |
26688 KB |
Output is correct |
21 |
Correct |
251 ms |
26688 KB |
Output is correct |
22 |
Correct |
175 ms |
26688 KB |
Output is correct |
23 |
Correct |
213 ms |
26688 KB |
Output is correct |
24 |
Correct |
196 ms |
26688 KB |
Output is correct |
25 |
Correct |
174 ms |
26688 KB |
Output is correct |
26 |
Correct |
233 ms |
26688 KB |
Output is correct |
27 |
Correct |
260 ms |
26688 KB |
Output is correct |
28 |
Correct |
236 ms |
26688 KB |
Output is correct |
29 |
Correct |
311 ms |
26688 KB |
Output is correct |
30 |
Correct |
665 ms |
135520 KB |
Output is correct |
31 |
Correct |
1092 ms |
135816 KB |
Output is correct |
32 |
Correct |
1643 ms |
136212 KB |
Output is correct |
33 |
Correct |
2668 ms |
136992 KB |
Output is correct |
34 |
Correct |
546 ms |
136992 KB |
Output is correct |
35 |
Correct |
2814 ms |
137016 KB |
Output is correct |
36 |
Correct |
2719 ms |
137016 KB |
Output is correct |
37 |
Correct |
2662 ms |
137672 KB |
Output is correct |
38 |
Correct |
2567 ms |
137672 KB |
Output is correct |
39 |
Correct |
2751 ms |
137700 KB |
Output is correct |
40 |
Correct |
1971 ms |
137700 KB |
Output is correct |
41 |
Correct |
2651 ms |
137700 KB |
Output is correct |
42 |
Correct |
2720 ms |
137700 KB |
Output is correct |
43 |
Correct |
1328 ms |
137700 KB |
Output is correct |
44 |
Correct |
1319 ms |
137700 KB |
Output is correct |
45 |
Correct |
1303 ms |
137748 KB |
Output is correct |
46 |
Correct |
1263 ms |
137748 KB |
Output is correct |
47 |
Correct |
1167 ms |
137748 KB |
Output is correct |
48 |
Correct |
1947 ms |
137748 KB |
Output is correct |
49 |
Correct |
1834 ms |
139760 KB |
Output is correct |