#include <bits/stdc++.h>
#define ll long long
#define X first
#define Y second
#define MP make_pair
using namespace std;
const int N = 2e5 + 12;
int n, k, b[N], p[N], f[N];
pair<int, int> a[N];
void upd_f(int v, int val){
for(;v <= k;v |= v + 1)
f[v] += val;
}
int get_f(int v){
int val = 0;
for(;v >= 0;v = (v & (v + 1)) - 1)
val += f[v];
return val;
}
int t[N * 4];
void build(int v, int tl, int tr){
if(tl == tr){
t[v] = b[tl];
return;
}
int tm = (tl + tr) / 2;
build(v * 2, tl, tm), build(v * 2 + 1, tm + 1, tr);
t[v] = max(t[v * 2], t[v * 2 + 1]);
}
void upd(int v, int tl, int tr, int pos, int val){
if(tl > pos || pos > tr)
return;
if(tl == tr){
t[v] = val;
return;
}
int tm = (tl + tr) / 2;
upd(v * 2, tl, tm, pos, val), upd(v * 2 + 1, tm + 1, tr, pos, val);
t[v] = max(t[v * 2], t[v * 2 + 1]);
}
int rec(int v, int tl, int tr, int val){
if(tl == tr)
return tl;
int tm = (tl + tr) / 2;
if(t[v * 2 + 1] >= val)
return rec(v * 2 + 1, tm + 1, tr, val);
else if(t[v * 2] >= val)
return rec(v * 2, tl, tm, val);
return 0;
}
int main () {
scanf("%d %d", &n, &k);
for(int i = 1;i <= n;i++)
scanf("%d %d", &a[i].X, &a[i].Y);
for(int i = 1;i <= k;i++)
scanf("%d", &b[i]), p[i] = i;
sort(a + 1, a + n + 1, [](pair<int, int> x, pair<int, int> y){
return max(x.X, x.Y) > max(y.X, y.Y);
});
sort(p + 1, p + k + 1, [](int x, int y){
return b[x] > b[y];
});
build(1, 1, k);
int ptr = 1;
ll ans = 0;
for(int i = 1;i <= n;i++){
while(ptr <= k && b[p[ptr]] >= max(a[i].X, a[i].Y))
upd(1, 1, k, p[ptr], 0), upd_f(k - p[ptr] + 1, 1), ptr++;
int tmp = rec(1, 1, k, min(a[i].X, a[i].Y));
int cnt = get_f(k - tmp);
if(tmp != 0){
if(a[i].X < a[i].Y)
swap(a[i].X, a[i].Y);
}
if(cnt & 1)
ans += a[i].Y;
else
ans += a[i].X;
}
printf("%lld", ans);
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:56:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
56 | scanf("%d %d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~~
fortune_telling2.cpp:58:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
58 | scanf("%d %d", &a[i].X, &a[i].Y);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:60:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
60 | scanf("%d", &b[i]), p[i] = i;
| ~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
9 ms |
1004 KB |
Output is correct |
15 |
Correct |
18 ms |
1644 KB |
Output is correct |
16 |
Correct |
28 ms |
2028 KB |
Output is correct |
17 |
Correct |
37 ms |
2796 KB |
Output is correct |
18 |
Correct |
38 ms |
2816 KB |
Output is correct |
19 |
Correct |
36 ms |
2796 KB |
Output is correct |
20 |
Correct |
38 ms |
2796 KB |
Output is correct |
21 |
Correct |
29 ms |
2796 KB |
Output is correct |
22 |
Correct |
26 ms |
2284 KB |
Output is correct |
23 |
Correct |
28 ms |
2284 KB |
Output is correct |
24 |
Correct |
32 ms |
2284 KB |
Output is correct |
25 |
Correct |
24 ms |
2412 KB |
Output is correct |
26 |
Correct |
35 ms |
2816 KB |
Output is correct |
27 |
Correct |
39 ms |
2796 KB |
Output is correct |
28 |
Correct |
37 ms |
2796 KB |
Output is correct |
29 |
Correct |
38 ms |
2796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
9 ms |
1004 KB |
Output is correct |
15 |
Correct |
18 ms |
1644 KB |
Output is correct |
16 |
Correct |
28 ms |
2028 KB |
Output is correct |
17 |
Correct |
37 ms |
2796 KB |
Output is correct |
18 |
Correct |
38 ms |
2816 KB |
Output is correct |
19 |
Correct |
36 ms |
2796 KB |
Output is correct |
20 |
Correct |
38 ms |
2796 KB |
Output is correct |
21 |
Correct |
29 ms |
2796 KB |
Output is correct |
22 |
Correct |
26 ms |
2284 KB |
Output is correct |
23 |
Correct |
28 ms |
2284 KB |
Output is correct |
24 |
Correct |
32 ms |
2284 KB |
Output is correct |
25 |
Correct |
24 ms |
2412 KB |
Output is correct |
26 |
Correct |
35 ms |
2816 KB |
Output is correct |
27 |
Correct |
39 ms |
2796 KB |
Output is correct |
28 |
Correct |
37 ms |
2796 KB |
Output is correct |
29 |
Correct |
38 ms |
2796 KB |
Output is correct |
30 |
Correct |
138 ms |
6892 KB |
Output is correct |
31 |
Correct |
147 ms |
8064 KB |
Output is correct |
32 |
Correct |
174 ms |
9452 KB |
Output is correct |
33 |
Correct |
206 ms |
12140 KB |
Output is correct |
34 |
Correct |
67 ms |
6636 KB |
Output is correct |
35 |
Correct |
210 ms |
12268 KB |
Output is correct |
36 |
Correct |
209 ms |
12140 KB |
Output is correct |
37 |
Correct |
226 ms |
12268 KB |
Output is correct |
38 |
Correct |
187 ms |
12140 KB |
Output is correct |
39 |
Correct |
267 ms |
12228 KB |
Output is correct |
40 |
Correct |
148 ms |
11884 KB |
Output is correct |
41 |
Correct |
225 ms |
12140 KB |
Output is correct |
42 |
Correct |
206 ms |
12268 KB |
Output is correct |
43 |
Correct |
117 ms |
11116 KB |
Output is correct |
44 |
Correct |
136 ms |
11116 KB |
Output is correct |
45 |
Correct |
114 ms |
11244 KB |
Output is correct |
46 |
Correct |
161 ms |
10220 KB |
Output is correct |
47 |
Correct |
184 ms |
10092 KB |
Output is correct |
48 |
Correct |
256 ms |
12140 KB |
Output is correct |
49 |
Correct |
202 ms |
12268 KB |
Output is correct |