#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define bit(n,i) ((n>>i)&1)
#define all(x) x.begin(),x.end()
#define int long long
typedef long long ll;
typedef pair<int,int> pii;
typedef double ld;
typedef pair<ld,ld> pdd;
typedef pair<ll,ll> pll;
const ll maxn=2e5+5;
const ll offset=2e5;
const ll inf=1e18;
const int base=350;
const ll mod=998244353;
int n,k;
int a[maxn],b[maxn],t[maxn];
pii c[maxn];
int rmq[maxn][19];
vector<pair<int,pii>> qr;
bool dd[maxn];
int st[maxn];
void Update(int u)
{
for(;u<=n;u+=u&-u) st[u]+=1;
}
int Get1(int u)
{
int r=0;
for(;u>0;u-=u&-u) r+=st[u];
return r;
}
int Get(int l,int r)
{
return Get1(r)-Get1(l-1);
}
int Get_max(int l,int r)
{
int lg=__lg(r-l+1);
return max(rmq[l][lg],rmq[r-(1<<lg)+1][lg]);
}
void sol()
{
cin >> n>>k;
for1(i,1,n) cin >> a[i]>> b[i];
for1(i,1,k)
{
cin >> t[i];
c[i]={t[i],i};
}
sort(c+1,c+1+k);
for1(i,1,n) rmq[i][0]=c[i].se;
for1(j,1,18) for1(i,1,n-(1<<j)+1) rmq[i][j]=max(rmq[i][j-1],rmq[i+(1<<(j-1))][j-1]);
for1(i,1,n)
{
int l=min(a[i],b[i]);
int r=max(a[i],b[i]);
int cl= upper_bound(c+1,c+1+n,make_pair(l,-1LL))-c;
int cr=upper_bound(c+1,c+1+n,make_pair(r,-1LL))-c-1;
int j=0;
if (cl<=cr) j=Get_max(cl,cr);
if (j!=0 && a[i]<b[i]) swap(a[i],b[i]);
qr.pb({b[i],{i,j}});
// cerr<< i<<' '<<j<<'\n';
}
sort(all(qr));
int j=0;
while (j<qr.size() && qr[j].fi>=c[k].fi)
{
dd[qr[j].se.fi]=0;
j++;
}
for2(i,k,1)
{
// cerr<< c[i].se<<'\n';
Update(c[i].se);
while (j<qr.size() && qr[j].fi>=c[i-1].fi)
{
dd[qr[j].se.fi]=Get(qr[j].se.se+1,n)&1;
j++;
}
// cerr<<i<<'\n';
}
// return;
int res=0;
for1(i,1,n)
{
if (dd[i]) res+=a[i];
else res+=b[i];
}
cout << res;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("1017G.inp","r",stdin);
// freopen("1017G.out","w",stdout);
int t=1;//cin >> t;
while (t--) {
sol();
}
}
/*
4 12
1 2 3
1 3
2 3
2 1
1 2
1 2
1 4
1 1
1 1
2 4
2 3
1 1
3 4
*/
Compilation message
fortune_telling2.cpp: In function 'void sol()':
fortune_telling2.cpp:79:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | while (j<qr.size() && qr[j].fi>=c[k].fi)
| ~^~~~~~~~~~
fortune_telling2.cpp:88:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | while (j<qr.size() && qr[j].fi>=c[i-1].fi)
| ~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
10576 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
10576 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
10576 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |