#pragma GCC optimize("O3")
#include<bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<61);
const int inf=(1<<30);
const int nmax=1e6+50;
const int mod=1e9+7;
using namespace std;
int a[nmax],b[nmax],q[nmax],n,m,i,j,t,sp[20][nmax],fw[nmax],sz,ans[nmax],cur,l,r;
ll sum;
vector<int>v;
vector<pair<int,int> >vc[nmax];
void upd(int i)
{
for(;i>=1;i-=i&(-i))fw[i]++;
}
int qry(int i)
{
int rs=0;
for(;i<=sz;i+=i&(-i))rs+=fw[i];
return rs;
}
int main()
{
//freopen("sol.in","r",stdin);
//freopen("sol.out","w",stdout);
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
cin>>n>>m;
for(i=1;i<=n;i++)
{
cin>>a[i]>>b[i];
v.pb(a[i]);
v.pb(b[i]);
}
for(i=1;i<=m;i++)
{
cin>>q[i];
v.pb(q[i]);
}
sort(v.begin(),v.end());
v.resize(unique(v.begin(),v.end())-v.begin());
sz=v.size();
for(i=1;i<=m;i++)
{
q[i]=lower_bound(v.begin(),v.end(),q[i])-v.begin()+1;
sp[0][q[i]]=++t;
}
for(i=1;sz>>i;++i)for(j=1;j+(1<<i)<=sz+1;++j)sp[i][j]=max(sp[i-1][j],sp[i-1][j+(1<<i)]);
for(i=1;i<=n;i++)
{
l=min(a[i],b[i]);
r=max(a[i],b[i]);
l=lower_bound(v.begin(),v.end(),l)-v.begin()+1;
r=lower_bound(v.begin(),v.end(),r)-v.begin()+1;
cur=0;
for(j=20;j>=0;--j)if(l+(1<<j)<=r)cur=max(cur,sp[j][l]),l+=(1<<j);
if(cur!=0)
{
ans[i]=1;
if(a[i]>b[i])swap(a[i],b[i]);
}
vc[cur].pb({i,r});
}
for(i=m;i>=0;i--)
{
for(j=0;j<vc[i].size();j++)ans[vc[i][j].fr]^=qry(vc[i][j].sc)&1;
if(i)upd(q[i]);
}
for(i=1;i<=n;i++)
{
if(ans[i])sum+=b[i];
else sum+=a[i];
}
cout<<sum<<endl;
return 0;
}
/*
5 3
4 6
9 1
8 8
4 2
3 7
8
2
9
*/
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:75:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0;j<vc[i].size();j++)ans[vc[i][j].fr]^=qry(vc[i][j].sc)&1;
~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
23972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
23972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
23972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |