This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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+69;
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<pii> qr[maxn];
int dd[maxn];
int st[maxn];
void Update(int u)
{
for(;u<=k;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,k) rmq[i][0]=c[i].se;
for1(j,1,18) for1(i,1,k-(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+k,make_pair(l,-1LL))-c;
int cr=upper_bound(c+1,c+1+k,make_pair(r,-1LL))-c-1;
int j=0;
if (cl<=cr)
{
j=Get_max(cl,cr);
if (a[i]<b[i]) swap(a[i],b[i]);
}
qr[cr+1].pb({i,j});
// cerr<< i<<' '<<j<<" c "<<a[i]<<' '<<b[i]<<'\n';
}
for2(i,k,1)
{
// cerr<< c[i].se<<'\n';
Update(c[i].se);
for(pii x: qr[i])
{
dd[x.fi]=Get(x.se+1,n)&1;
// cerr<< x.fi<<' '<<dd[x.fi]<<'\n';
}
// 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
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |