#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 int mod=1e9+7;
const int maxn=2e5+100;
int n,k;
int a[maxn],b[maxn];
vector<int> v[4*maxn];
pair<int,int>t[maxn];
void build(int i,int l,int r){
if(l==r){
v[i].pb(t[r].second);
return;
}
build(left,l,mid);
build(righ,mid+1,r);
int 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++]);
}
int getmax(int i,int l,int r,int ql,int 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));
}
int cnt(int i,int l,int r,int ql,int qr,int val){
if(ql>qr)return 0;
if(r<ql||l>qr)return 0;
if(l>=ql&&r<=qr){
int 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);
}
int c[maxn];
int main()
{
IOS
cin>>n>>k;
for(int i=0;i<n;i++)cin>>a[i]>>b[i];
for(int i=0;i<k;i++){
cin>>t[i].first;
t[i].second=i;
}
sort(t,t+k);
for(int i=0;i<k;i++)c[i]=t[i].first;
build(0,0,k-1);
int ans=0;
for(int i=0;i<n;i++){
int l=lower_bound(c,c+k,min(a[i],b[i]))-c;
int r=lower_bound(c,c+k,max(a[i],b[i]))-c-1;
int id=getmax(0,0,k-1,l,r);
l=lower_bound(c,c+k,max(a[i],b[i]))-c;
r=k-1;
int 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(int, int, 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++]);
~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
19192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
19192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
17 ms |
19192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |