#include <bits/stdc++.h>
#include <string>
#define ll long long
#define pb push_back
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mid (l+r)/2
#define left 2*i+1
#define righ 2*i+2
using namespace std;
const ll mod=1e9+7;
const ll maxn=2e5+100;
ll n,k;
ll a[maxn],b[maxn];
vector<ll> v[4*maxn];
pair<ll,ll>t[maxn];
void build(ll i,ll l,ll r){
if(l==r){
v[i].pb(t[r].second);
return;
}
build(left,l,mid);
build(righ,mid+1,r);
ll lp=0,rp=0;
while(lp<v[left].size()&&rp<v[righ].size()){
if(v[left][lp]<v[righ][rp])v[i].pb(v[left][lp++]);
else v[i].pb(v[righ][rp++]);
}
while(lp<v[left].size())v[i].pb(v[left][lp++]);
while(rp<v[righ].size())v[i].pb(v[righ][rp++]);
}
ll getmax(ll i,ll l,ll r,ll ql,ll qr){
if(ql>qr)return -1;
if(r<ql||l>qr)return -1;
if(l>=ql&&r<=qr)return v[i].back();
return max(getmax(left,l,mid,ql,qr),getmax(righ,mid+1,r,ql,qr));
}
ll cnt(ll i,ll l,ll r,ll ql,ll qr,ll val){
if(ql>qr)return 0;
if(r<ql||l>qr)return 0;
if(l>=ql&&r<=qr){
ll x=upper_bound(v[i].begin(),v[i].end(),val)-v[i].begin();
return v[i].size()-x;
}
return cnt(left,l,mid,ql,qr,val)+cnt(righ,mid+1,r,ql,qr,val);
}
ll c[maxn];
int main()
{
IOS
cin>>n>>k;
for(ll i=0;i<n;i++)cin>>a[i]>>b[i];
for(ll i=0;i<k;i++){
cin>>t[i].first;
t[i].second=i;
}
sort(t,t+k);
for(ll i=0;i<k;i++)c[i]=t[i].first;
build(0,0,k-1);
ll ans=0;
for(ll i=0;i<n;i++){
ll l=lower_bound(c,c+k,min(a[i],b[i]))-c;
ll r=lower_bound(c,c+k,max(a[i],b[i]))-c-1;
ll id=getmax(0,0,k-1,l,r);
l=lower_bound(c,c+k,max(a[i],b[i]))-c;
r=k-1;
ll turn=cnt(0,0,k-1,l,r,id);
if(id==-1){
if(turn%2==0){ans+=a[i];continue;}
ans+=b[i];
continue;
}
if(a[i]<b[i])swap(a[i],b[i]);
if(turn%2==0)ans+=a[i];
else ans+=b[i];
}
cout<<ans<<endl;
return 0;
}
Compilation message
fortune_telling2.cpp: In function 'void build(long long int, long long int, long long int)':
fortune_telling2.cpp:26:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(lp<v[left].size()&&rp<v[righ].size()){
~~^~~~~~~~~~~~~~~
fortune_telling2.cpp:26:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(lp<v[left].size()&&rp<v[righ].size()){
~~^~~~~~~~~~~~~~~
fortune_telling2.cpp:30:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(lp<v[left].size())v[i].pb(v[left][lp++]);
~~^~~~~~~~~~~~~~~
fortune_telling2.cpp:31:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(rp<v[righ].size())v[i].pb(v[righ][rp++]);
~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
19320 KB |
Output is correct |
2 |
Correct |
16 ms |
19320 KB |
Output is correct |
3 |
Correct |
17 ms |
19320 KB |
Output is correct |
4 |
Correct |
17 ms |
19320 KB |
Output is correct |
5 |
Correct |
17 ms |
19320 KB |
Output is correct |
6 |
Correct |
17 ms |
19320 KB |
Output is correct |
7 |
Correct |
18 ms |
19320 KB |
Output is correct |
8 |
Correct |
17 ms |
19320 KB |
Output is correct |
9 |
Correct |
16 ms |
19320 KB |
Output is correct |
10 |
Correct |
17 ms |
19324 KB |
Output is correct |
11 |
Correct |
17 ms |
19320 KB |
Output is correct |
12 |
Correct |
16 ms |
19320 KB |
Output is correct |
13 |
Correct |
17 ms |
19320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
19320 KB |
Output is correct |
2 |
Correct |
16 ms |
19320 KB |
Output is correct |
3 |
Correct |
17 ms |
19320 KB |
Output is correct |
4 |
Correct |
17 ms |
19320 KB |
Output is correct |
5 |
Correct |
17 ms |
19320 KB |
Output is correct |
6 |
Correct |
17 ms |
19320 KB |
Output is correct |
7 |
Correct |
18 ms |
19320 KB |
Output is correct |
8 |
Correct |
17 ms |
19320 KB |
Output is correct |
9 |
Correct |
16 ms |
19320 KB |
Output is correct |
10 |
Correct |
17 ms |
19324 KB |
Output is correct |
11 |
Correct |
17 ms |
19320 KB |
Output is correct |
12 |
Correct |
16 ms |
19320 KB |
Output is correct |
13 |
Correct |
17 ms |
19320 KB |
Output is correct |
14 |
Correct |
35 ms |
21624 KB |
Output is correct |
15 |
Correct |
63 ms |
24312 KB |
Output is correct |
16 |
Correct |
90 ms |
25720 KB |
Output is correct |
17 |
Correct |
115 ms |
29684 KB |
Output is correct |
18 |
Correct |
119 ms |
29816 KB |
Output is correct |
19 |
Correct |
120 ms |
29688 KB |
Output is correct |
20 |
Correct |
115 ms |
29684 KB |
Output is correct |
21 |
Correct |
97 ms |
29684 KB |
Output is correct |
22 |
Correct |
84 ms |
29688 KB |
Output is correct |
23 |
Correct |
84 ms |
29812 KB |
Output is correct |
24 |
Correct |
83 ms |
29812 KB |
Output is correct |
25 |
Correct |
82 ms |
29684 KB |
Output is correct |
26 |
Correct |
95 ms |
29684 KB |
Output is correct |
27 |
Correct |
116 ms |
29684 KB |
Output is correct |
28 |
Correct |
97 ms |
29684 KB |
Output is correct |
29 |
Correct |
103 ms |
29684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
19320 KB |
Output is correct |
2 |
Correct |
16 ms |
19320 KB |
Output is correct |
3 |
Correct |
17 ms |
19320 KB |
Output is correct |
4 |
Correct |
17 ms |
19320 KB |
Output is correct |
5 |
Correct |
17 ms |
19320 KB |
Output is correct |
6 |
Correct |
17 ms |
19320 KB |
Output is correct |
7 |
Correct |
18 ms |
19320 KB |
Output is correct |
8 |
Correct |
17 ms |
19320 KB |
Output is correct |
9 |
Correct |
16 ms |
19320 KB |
Output is correct |
10 |
Correct |
17 ms |
19324 KB |
Output is correct |
11 |
Correct |
17 ms |
19320 KB |
Output is correct |
12 |
Correct |
16 ms |
19320 KB |
Output is correct |
13 |
Correct |
17 ms |
19320 KB |
Output is correct |
14 |
Correct |
35 ms |
21624 KB |
Output is correct |
15 |
Correct |
63 ms |
24312 KB |
Output is correct |
16 |
Correct |
90 ms |
25720 KB |
Output is correct |
17 |
Correct |
115 ms |
29684 KB |
Output is correct |
18 |
Correct |
119 ms |
29816 KB |
Output is correct |
19 |
Correct |
120 ms |
29688 KB |
Output is correct |
20 |
Correct |
115 ms |
29684 KB |
Output is correct |
21 |
Correct |
97 ms |
29684 KB |
Output is correct |
22 |
Correct |
84 ms |
29688 KB |
Output is correct |
23 |
Correct |
84 ms |
29812 KB |
Output is correct |
24 |
Correct |
83 ms |
29812 KB |
Output is correct |
25 |
Correct |
82 ms |
29684 KB |
Output is correct |
26 |
Correct |
95 ms |
29684 KB |
Output is correct |
27 |
Correct |
116 ms |
29684 KB |
Output is correct |
28 |
Correct |
97 ms |
29684 KB |
Output is correct |
29 |
Correct |
103 ms |
29684 KB |
Output is correct |
30 |
Correct |
234 ms |
68128 KB |
Output is correct |
31 |
Correct |
329 ms |
68716 KB |
Output is correct |
32 |
Correct |
465 ms |
69484 KB |
Output is correct |
33 |
Correct |
704 ms |
71024 KB |
Output is correct |
34 |
Correct |
203 ms |
68076 KB |
Output is correct |
35 |
Correct |
742 ms |
71020 KB |
Output is correct |
36 |
Correct |
719 ms |
70988 KB |
Output is correct |
37 |
Correct |
717 ms |
71020 KB |
Output is correct |
38 |
Correct |
693 ms |
71100 KB |
Output is correct |
39 |
Correct |
719 ms |
71148 KB |
Output is correct |
40 |
Correct |
509 ms |
71020 KB |
Output is correct |
41 |
Correct |
725 ms |
71020 KB |
Output is correct |
42 |
Correct |
743 ms |
71020 KB |
Output is correct |
43 |
Correct |
335 ms |
71148 KB |
Output is correct |
44 |
Correct |
330 ms |
71020 KB |
Output is correct |
45 |
Correct |
341 ms |
71044 KB |
Output is correct |
46 |
Correct |
388 ms |
71148 KB |
Output is correct |
47 |
Correct |
391 ms |
71048 KB |
Output is correct |
48 |
Correct |
588 ms |
71020 KB |
Output is correct |
49 |
Correct |
559 ms |
71140 KB |
Output is correct |