// be name khoda
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
//#define mp make_pair
typedef long long ll;
#pragma GCC optimize("Ofast")
const int maxn = 2e5+10;
//const int mod = 1e9+7;
//const ll inf = 1e9+10;
int seg[maxn*20], L[maxn*20], R[maxn*20], n;
int root[maxn], lst;
void up(int v, int nw, int pos, int tl = 1, int tr = n)
{
seg[nw] = seg[v] + 1;
if(tl == tr) return;
int tm = (tl + tr) >> 1;
if(pos <= tm)
{
L[nw] = ++lst; R[nw] = R[v];
up(L[v], L[nw], pos, tl, tm);
}
else
{
R[nw] = ++lst; L[nw] = L[v];
up(R[v], R[nw], pos, tm+1, tr);
}
}
int qu(int v, int l, int r, int tl = 1, int tr = n)
{
if(l > r) return 0;
if(tl == l && tr == r) return seg[v];
int tm = (tl + tr) >> 1;
return qu(L[v], l, min(tm,r), tl, tm) +
qu(R[v], max(tm+1,l), r, tm+1, tr);
}
int qu2(int v1, int v2, int tl = 1, int tr = n)
{
if(tl == tr)
{
if(seg[v1] == seg[v2]) return tl;
else return tl+1;
}
int tm = (tl + tr) >> 1;
if(seg[R[v1]] == seg[R[v2]])
return qu2(L[v1], L[v2], tl, tm);
else
return qu2(R[v1], R[v2], tm+1, tr);
}
vector<pair<int,int>> ve;
int QU(int x, int l, int r)
{
int X = lower_bound(ve.begin(), ve.end(), make_pair(x,0)) - ve.begin();
return qu(root[X], l, r);
}
int A[maxn], B[maxn];
signed main()
{
//ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int m; scanf("%d%d", &m, &n);
for(int i = 1; i <= m; i++) scanf("%d%d", A+i, B+i);
for(int i = 1, t; i <= n; i++)
{scanf("%d", &t); ve.push_back({t,i});}
sort(ve.begin(), ve.end());
for(int i = ve.size()-1; i >= 0; i--)
{
int pos = ve[i].S;
root[i] = ++lst;
up(root[i+1], root[i], pos);
}
ll ans = 0;
for(int i = 1, a, b; i <= m; i++)
{
a = A[i], b = B[i];
int lo = 0, hi = n+1;
int Xa = lower_bound(ve.begin(), ve.end(), make_pair(a,0)) - ve.begin();
int Xb = lower_bound(ve.begin(), ve.end(), make_pair(b,0)) - ve.begin();
hi = qu2(root[Xa], root[Xb]);
int X = qu(root[Xa], hi, n);
if(QU(min(a,b), 1, hi-1) == 0)
{
if(X&1) ans += b;
else ans += a;
}
else
{
if(X&1) ans += min(a,b);
else ans += max(a,b);
}
}
printf("%lld", ans);
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:90:13: warning: unused variable 'lo' [-Wunused-variable]
90 | int lo = 0, hi = n+1;
| ^~
fortune_telling2.cpp:73:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
73 | int m; scanf("%d%d", &m, &n);
| ~~~~~^~~~~~~~~~~~~~~~
fortune_telling2.cpp:74:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
74 | for(int i = 1; i <= m; i++) scanf("%d%d", A+i, B+i);
| ~~~~~^~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:76:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
76 | {scanf("%d", &t); ve.push_back({t,i});}
| ~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
640 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
516 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
640 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
516 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
15 ms |
2720 KB |
Output is correct |
15 |
Correct |
33 ms |
5116 KB |
Output is correct |
16 |
Correct |
52 ms |
7548 KB |
Output is correct |
17 |
Correct |
80 ms |
10096 KB |
Output is correct |
18 |
Correct |
79 ms |
10096 KB |
Output is correct |
19 |
Correct |
78 ms |
10096 KB |
Output is correct |
20 |
Correct |
94 ms |
10096 KB |
Output is correct |
21 |
Correct |
57 ms |
10096 KB |
Output is correct |
22 |
Correct |
41 ms |
9592 KB |
Output is correct |
23 |
Correct |
44 ms |
9592 KB |
Output is correct |
24 |
Correct |
47 ms |
9584 KB |
Output is correct |
25 |
Correct |
39 ms |
9732 KB |
Output is correct |
26 |
Correct |
56 ms |
9976 KB |
Output is correct |
27 |
Correct |
72 ms |
10096 KB |
Output is correct |
28 |
Correct |
62 ms |
10176 KB |
Output is correct |
29 |
Correct |
77 ms |
10232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
640 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
516 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
15 ms |
2720 KB |
Output is correct |
15 |
Correct |
33 ms |
5116 KB |
Output is correct |
16 |
Correct |
52 ms |
7548 KB |
Output is correct |
17 |
Correct |
80 ms |
10096 KB |
Output is correct |
18 |
Correct |
79 ms |
10096 KB |
Output is correct |
19 |
Correct |
78 ms |
10096 KB |
Output is correct |
20 |
Correct |
94 ms |
10096 KB |
Output is correct |
21 |
Correct |
57 ms |
10096 KB |
Output is correct |
22 |
Correct |
41 ms |
9592 KB |
Output is correct |
23 |
Correct |
44 ms |
9592 KB |
Output is correct |
24 |
Correct |
47 ms |
9584 KB |
Output is correct |
25 |
Correct |
39 ms |
9732 KB |
Output is correct |
26 |
Correct |
56 ms |
9976 KB |
Output is correct |
27 |
Correct |
72 ms |
10096 KB |
Output is correct |
28 |
Correct |
62 ms |
10176 KB |
Output is correct |
29 |
Correct |
77 ms |
10232 KB |
Output is correct |
30 |
Correct |
176 ms |
48900 KB |
Output is correct |
31 |
Correct |
263 ms |
50024 KB |
Output is correct |
32 |
Correct |
357 ms |
51428 KB |
Output is correct |
33 |
Correct |
618 ms |
54116 KB |
Output is correct |
34 |
Correct |
159 ms |
48740 KB |
Output is correct |
35 |
Correct |
608 ms |
54120 KB |
Output is correct |
36 |
Correct |
563 ms |
54316 KB |
Output is correct |
37 |
Correct |
603 ms |
54116 KB |
Output is correct |
38 |
Correct |
581 ms |
54120 KB |
Output is correct |
39 |
Correct |
611 ms |
54352 KB |
Output is correct |
40 |
Correct |
349 ms |
53860 KB |
Output is correct |
41 |
Correct |
585 ms |
54116 KB |
Output is correct |
42 |
Correct |
607 ms |
54100 KB |
Output is correct |
43 |
Correct |
236 ms |
53404 KB |
Output is correct |
44 |
Correct |
237 ms |
53500 KB |
Output is correct |
45 |
Correct |
233 ms |
53348 KB |
Output is correct |
46 |
Correct |
280 ms |
52196 KB |
Output is correct |
47 |
Correct |
299 ms |
52068 KB |
Output is correct |
48 |
Correct |
421 ms |
54116 KB |
Output is correct |
49 |
Correct |
378 ms |
54220 KB |
Output is correct |