#include <bits/stdc++.h>
using namespace std;
const int mxn=2e5+5,sz=(1<<20);
int n,q,a[mxn],b[mxn],qu[mxn];
long long int ans=0;
struct segment{
vector<int>v[sz];
void build(int id,int L,int R){
if(L+1==R){
v[id].push_back(qu[L]);
return ;
}
int mid=(L+R)/2;
build(id*2,L,mid);
build(id*2+1,mid,R);
for(auto i:v[id*2])
v[id].push_back(i);
for(auto i:v[id*2+1])
v[id].push_back(i);
sort(v[id].begin(),v[id].end());
}
int get(int id,int L,int R,int l,int r){
if(L+1==R)
return L;
int mid=(L+R)/2;
int x=lower_bound(v[id].begin(),v[id].end(),l)-v[id].begin();
if(x==v[id].size() || v[id][x]>=r)
return 0;
x=lower_bound(v[id*2+1].begin(),v[id*2+1].end(),l)-v[id*2+1].begin();
if(x==v[id*2+1].size() || v[id*2+1][x]>=r)
return get(id*2,L,mid,l,r);
else
return get(id*2+1,mid,R,l,r);
}
int pet(int id,int L,int R,int l,int r,int x){
if(L==l && R==r){
int res=lower_bound(v[id].begin(),v[id].end(),x)-v[id].begin();
return v[id].size()-res;
}
int mid=(L+R)/2,res=0;
if(l<mid)
res+=pet(id*2,L,mid,l,min(r,mid),x);
if(r>mid)
res+=pet(id*2+1,mid,R,max(l,mid),r,x);
return res;
}
}seg;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i]>>b[i];
}
for(int i=1;i<=q;i++){
cin>>qu[i];
}
seg.build(1,1,q+1);
for(int i=1;i<=n;i++){
int x=min(a[i],b[i]);
int y=max(a[i],b[i]);
int l=seg.get(1,1,q+1,x,y);
int num=(l!=q)?seg.pet(1,1,q+1,l+1,q+1,y):0;
if(l==0)
ans+=(num&1)?b[i]:a[i];
else{
if(a[i]<b[i])
ans+=(num&1)?a[i]:b[i];
else
ans+=(num&1)?b[i]:a[i];
}
// cout<<ans<<" ";
}
cout<<ans<<endl;
}
Compilation message
fortune_telling2.cpp: In member function 'int segment::get(int, int, int, int, int)':
fortune_telling2.cpp:27:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | if(x==v[id].size() || v[id][x]>=r)
| ~^~~~~~~~~~~~~~
fortune_telling2.cpp:30:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | if(x==v[id*2+1].size() || v[id*2+1][x]>=r)
| ~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
27484 KB |
Output is correct |
2 |
Correct |
8 ms |
27228 KB |
Output is correct |
3 |
Correct |
9 ms |
27316 KB |
Output is correct |
4 |
Correct |
10 ms |
27228 KB |
Output is correct |
5 |
Correct |
9 ms |
27228 KB |
Output is correct |
6 |
Correct |
9 ms |
27228 KB |
Output is correct |
7 |
Correct |
9 ms |
27224 KB |
Output is correct |
8 |
Correct |
9 ms |
27228 KB |
Output is correct |
9 |
Correct |
8 ms |
27224 KB |
Output is correct |
10 |
Correct |
10 ms |
27224 KB |
Output is correct |
11 |
Correct |
10 ms |
27484 KB |
Output is correct |
12 |
Correct |
10 ms |
27312 KB |
Output is correct |
13 |
Correct |
9 ms |
27312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
27484 KB |
Output is correct |
2 |
Correct |
8 ms |
27228 KB |
Output is correct |
3 |
Correct |
9 ms |
27316 KB |
Output is correct |
4 |
Correct |
10 ms |
27228 KB |
Output is correct |
5 |
Correct |
9 ms |
27228 KB |
Output is correct |
6 |
Correct |
9 ms |
27228 KB |
Output is correct |
7 |
Correct |
9 ms |
27224 KB |
Output is correct |
8 |
Correct |
9 ms |
27228 KB |
Output is correct |
9 |
Correct |
8 ms |
27224 KB |
Output is correct |
10 |
Correct |
10 ms |
27224 KB |
Output is correct |
11 |
Correct |
10 ms |
27484 KB |
Output is correct |
12 |
Correct |
10 ms |
27312 KB |
Output is correct |
13 |
Correct |
9 ms |
27312 KB |
Output is correct |
14 |
Correct |
25 ms |
28764 KB |
Output is correct |
15 |
Correct |
47 ms |
30640 KB |
Output is correct |
16 |
Correct |
68 ms |
31572 KB |
Output is correct |
17 |
Correct |
100 ms |
34064 KB |
Output is correct |
18 |
Correct |
93 ms |
34088 KB |
Output is correct |
19 |
Correct |
93 ms |
34000 KB |
Output is correct |
20 |
Correct |
102 ms |
34112 KB |
Output is correct |
21 |
Correct |
87 ms |
34004 KB |
Output is correct |
22 |
Correct |
57 ms |
33548 KB |
Output is correct |
23 |
Correct |
58 ms |
33616 KB |
Output is correct |
24 |
Correct |
60 ms |
33620 KB |
Output is correct |
25 |
Correct |
56 ms |
33512 KB |
Output is correct |
26 |
Correct |
63 ms |
34416 KB |
Output is correct |
27 |
Correct |
100 ms |
34028 KB |
Output is correct |
28 |
Correct |
64 ms |
34036 KB |
Output is correct |
29 |
Correct |
79 ms |
34028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
27484 KB |
Output is correct |
2 |
Correct |
8 ms |
27228 KB |
Output is correct |
3 |
Correct |
9 ms |
27316 KB |
Output is correct |
4 |
Correct |
10 ms |
27228 KB |
Output is correct |
5 |
Correct |
9 ms |
27228 KB |
Output is correct |
6 |
Correct |
9 ms |
27228 KB |
Output is correct |
7 |
Correct |
9 ms |
27224 KB |
Output is correct |
8 |
Correct |
9 ms |
27228 KB |
Output is correct |
9 |
Correct |
8 ms |
27224 KB |
Output is correct |
10 |
Correct |
10 ms |
27224 KB |
Output is correct |
11 |
Correct |
10 ms |
27484 KB |
Output is correct |
12 |
Correct |
10 ms |
27312 KB |
Output is correct |
13 |
Correct |
9 ms |
27312 KB |
Output is correct |
14 |
Correct |
25 ms |
28764 KB |
Output is correct |
15 |
Correct |
47 ms |
30640 KB |
Output is correct |
16 |
Correct |
68 ms |
31572 KB |
Output is correct |
17 |
Correct |
100 ms |
34064 KB |
Output is correct |
18 |
Correct |
93 ms |
34088 KB |
Output is correct |
19 |
Correct |
93 ms |
34000 KB |
Output is correct |
20 |
Correct |
102 ms |
34112 KB |
Output is correct |
21 |
Correct |
87 ms |
34004 KB |
Output is correct |
22 |
Correct |
57 ms |
33548 KB |
Output is correct |
23 |
Correct |
58 ms |
33616 KB |
Output is correct |
24 |
Correct |
60 ms |
33620 KB |
Output is correct |
25 |
Correct |
56 ms |
33512 KB |
Output is correct |
26 |
Correct |
63 ms |
34416 KB |
Output is correct |
27 |
Correct |
100 ms |
34028 KB |
Output is correct |
28 |
Correct |
64 ms |
34036 KB |
Output is correct |
29 |
Correct |
79 ms |
34028 KB |
Output is correct |
30 |
Correct |
210 ms |
56840 KB |
Output is correct |
31 |
Correct |
297 ms |
57548 KB |
Output is correct |
32 |
Correct |
385 ms |
58568 KB |
Output is correct |
33 |
Correct |
584 ms |
60480 KB |
Output is correct |
34 |
Correct |
199 ms |
56572 KB |
Output is correct |
35 |
Correct |
585 ms |
60664 KB |
Output is correct |
36 |
Correct |
577 ms |
60792 KB |
Output is correct |
37 |
Correct |
592 ms |
60620 KB |
Output is correct |
38 |
Correct |
572 ms |
60684 KB |
Output is correct |
39 |
Correct |
622 ms |
60664 KB |
Output is correct |
40 |
Correct |
456 ms |
60596 KB |
Output is correct |
41 |
Correct |
602 ms |
60556 KB |
Output is correct |
42 |
Correct |
636 ms |
60620 KB |
Output is correct |
43 |
Correct |
295 ms |
59928 KB |
Output is correct |
44 |
Correct |
289 ms |
59852 KB |
Output is correct |
45 |
Correct |
290 ms |
59776 KB |
Output is correct |
46 |
Correct |
331 ms |
58636 KB |
Output is correct |
47 |
Correct |
340 ms |
58640 KB |
Output is correct |
48 |
Correct |
336 ms |
60876 KB |
Output is correct |
49 |
Correct |
311 ms |
60812 KB |
Output is correct |