#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2 * 100100, MAXK = 2 * 100100, MAXA = 1e9 + 1e4;
int n, k, a[MAXN], b[MAXN], t[MAXK];
long long ans = 0;
struct no
{
int cnt;
no *l, *r;
no(int cnt = 0) : cnt(cnt), l(nullptr), r(nullptr)
{}
};
inline int getcnt(no *n) { return (n ? n->cnt : 0); }
no* upd(no* n, int pos, int val = 1, int beg = 1, int end = MAXA)
{
if(beg == end)
return new no(getcnt(n) + val);
no* ans = (n ? new no(*n) : new no());
int mid = (beg + end) >> 1;
if(pos <= mid)
ans->l = upd(ans->l, pos, val, beg, mid);
else
ans->r = upd(ans->r, pos, val, mid + 1, end);
ans->cnt = getcnt(ans->l) + getcnt(ans->r);
return ans;
}
int get(no* n, int pos, int beg = 1, int end = MAXA)
{
if(!n)
return 0;
int mid = (beg + end) >> 1;
if(pos <= mid)
return get(n->l, pos, beg, mid) + getcnt(n->r);
return get(n->r, pos, mid + 1, end);
}
no* pseg[MAXK];
int bs(int x, int y)
{
if(x < y)
swap(x, y);
int beg = 0, end = k, mid, p, q;
while(beg < end)
{
mid = (beg + end + 1) >> 1;
p = get(pseg[end], y - 1) - get(pseg[mid - 1], y - 1);
q = get(pseg[end], x - 1) - get(pseg[mid - 1], x - 1);
if(p - q > 0)
beg = mid;
else
end = mid - 1;
}
return beg;
}
int main()
{
pseg[0] = nullptr;
scanf("%d %d", &n, &k);
for(int i = 1; i <= n; ++i)
scanf("%d %d", &a[i], &b[i]);
for(int i = 1; i <= k; ++i)
scanf("%d", &t[i]), pseg[i] = upd(pseg[i - 1], t[i]);
for(int i = 1, swaps, j; i <= n; ++i)
{
/*for(j = k, swaps = 0; j >= 1; --j)
if(min(a[i], b[i]) <= t[j] && t[j] < max(a[i], b[i]))
break;*/
j = bs(a[i], b[i]), swaps = 0;
if(min(a[i], b[i]) <= t[j] && t[j] < max(a[i], b[i]))
swaps += (a[i] < b[i]);
swaps += get(pseg[k], max(a[i], b[i]) - 1) - get(pseg[j], max(a[i], b[i]) - 1);
ans += (swaps % 2 ? b[i] : a[i]);
}
printf("%lld\n", ans);
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:64:7: 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:66:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a[i], &b[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:68:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &t[i]), pseg[i] = upd(pseg[i - 1], t[i]);
~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1408 KB |
Output is correct |
2 |
Correct |
5 ms |
1408 KB |
Output is correct |
3 |
Correct |
7 ms |
1408 KB |
Output is correct |
4 |
Correct |
7 ms |
1280 KB |
Output is correct |
5 |
Correct |
8 ms |
1408 KB |
Output is correct |
6 |
Correct |
8 ms |
1408 KB |
Output is correct |
7 |
Correct |
9 ms |
1408 KB |
Output is correct |
8 |
Correct |
7 ms |
1536 KB |
Output is correct |
9 |
Correct |
7 ms |
1280 KB |
Output is correct |
10 |
Correct |
4 ms |
1280 KB |
Output is correct |
11 |
Correct |
8 ms |
1408 KB |
Output is correct |
12 |
Correct |
7 ms |
1408 KB |
Output is correct |
13 |
Correct |
8 ms |
1408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1408 KB |
Output is correct |
2 |
Correct |
5 ms |
1408 KB |
Output is correct |
3 |
Correct |
7 ms |
1408 KB |
Output is correct |
4 |
Correct |
7 ms |
1280 KB |
Output is correct |
5 |
Correct |
8 ms |
1408 KB |
Output is correct |
6 |
Correct |
8 ms |
1408 KB |
Output is correct |
7 |
Correct |
9 ms |
1408 KB |
Output is correct |
8 |
Correct |
7 ms |
1536 KB |
Output is correct |
9 |
Correct |
7 ms |
1280 KB |
Output is correct |
10 |
Correct |
4 ms |
1280 KB |
Output is correct |
11 |
Correct |
8 ms |
1408 KB |
Output is correct |
12 |
Correct |
7 ms |
1408 KB |
Output is correct |
13 |
Correct |
8 ms |
1408 KB |
Output is correct |
14 |
Correct |
83 ms |
10360 KB |
Output is correct |
15 |
Correct |
167 ms |
20212 KB |
Output is correct |
16 |
Correct |
299 ms |
30180 KB |
Output is correct |
17 |
Correct |
418 ms |
40056 KB |
Output is correct |
18 |
Correct |
388 ms |
40104 KB |
Output is correct |
19 |
Correct |
362 ms |
39968 KB |
Output is correct |
20 |
Correct |
410 ms |
40056 KB |
Output is correct |
21 |
Correct |
304 ms |
39864 KB |
Output is correct |
22 |
Correct |
355 ms |
39968 KB |
Output is correct |
23 |
Correct |
367 ms |
39972 KB |
Output is correct |
24 |
Correct |
380 ms |
40056 KB |
Output is correct |
25 |
Correct |
378 ms |
39928 KB |
Output is correct |
26 |
Correct |
262 ms |
39856 KB |
Output is correct |
27 |
Correct |
360 ms |
39928 KB |
Output is correct |
28 |
Correct |
313 ms |
39928 KB |
Output is correct |
29 |
Correct |
413 ms |
40056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1408 KB |
Output is correct |
2 |
Correct |
5 ms |
1408 KB |
Output is correct |
3 |
Correct |
7 ms |
1408 KB |
Output is correct |
4 |
Correct |
7 ms |
1280 KB |
Output is correct |
5 |
Correct |
8 ms |
1408 KB |
Output is correct |
6 |
Correct |
8 ms |
1408 KB |
Output is correct |
7 |
Correct |
9 ms |
1408 KB |
Output is correct |
8 |
Correct |
7 ms |
1536 KB |
Output is correct |
9 |
Correct |
7 ms |
1280 KB |
Output is correct |
10 |
Correct |
4 ms |
1280 KB |
Output is correct |
11 |
Correct |
8 ms |
1408 KB |
Output is correct |
12 |
Correct |
7 ms |
1408 KB |
Output is correct |
13 |
Correct |
8 ms |
1408 KB |
Output is correct |
14 |
Correct |
83 ms |
10360 KB |
Output is correct |
15 |
Correct |
167 ms |
20212 KB |
Output is correct |
16 |
Correct |
299 ms |
30180 KB |
Output is correct |
17 |
Correct |
418 ms |
40056 KB |
Output is correct |
18 |
Correct |
388 ms |
40104 KB |
Output is correct |
19 |
Correct |
362 ms |
39968 KB |
Output is correct |
20 |
Correct |
410 ms |
40056 KB |
Output is correct |
21 |
Correct |
304 ms |
39864 KB |
Output is correct |
22 |
Correct |
355 ms |
39968 KB |
Output is correct |
23 |
Correct |
367 ms |
39972 KB |
Output is correct |
24 |
Correct |
380 ms |
40056 KB |
Output is correct |
25 |
Correct |
378 ms |
39928 KB |
Output is correct |
26 |
Correct |
262 ms |
39856 KB |
Output is correct |
27 |
Correct |
360 ms |
39928 KB |
Output is correct |
28 |
Correct |
313 ms |
39928 KB |
Output is correct |
29 |
Correct |
413 ms |
40056 KB |
Output is correct |
30 |
Correct |
769 ms |
198740 KB |
Output is correct |
31 |
Correct |
1464 ms |
199720 KB |
Output is correct |
32 |
Correct |
2140 ms |
201208 KB |
Output is correct |
33 |
Execution timed out |
3116 ms |
203896 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |