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>
#define ll long long
#define mp make_pair
#define pb push_back
#define ld long double
#define ordered_set tree<pii, null_type,less<pii>, rb_tree_tag,tree_order_statistics_node_update>
#define pii pair<int,int>
#define sz(x) (int)x.size()
#define piii pair<pii,pii>
#define precise cout<<fixed<<setprecision(10)
#define st first
#define nd second
#define ins insert
#define vi vector<int>
#define BOOST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
const int MAX=6e5+5;
const int inf=(int)1e9+9;
int l[MAX],r[MAX],c[MAX];
map<int,int>compress;
set<int>pom;
int tl[MAX],tr[MAX];
int t[MAX*2];
void update(int u,int ce){
u+=MAX;
for (t[u]=max(t[u],ce);u>1;u>>=1){
t[u>>1]=max(t[u],t[u^1]);
}
}
int query(int u,int v){
int maxi=-inf;
for (u+=MAX,v+=MAX+1;u<v;u>>=1,v>>=1){
if (u&1)maxi=max(maxi,t[u++]);
if (v&1)maxi=max(maxi,t[--v]);
}
return maxi;
}
int start[MAX];
vector<pii>rak;
int para[MAX],dara[MAX];
inline void readUI(int *n) //tylko dodatnie
{
register char c = 0;
while (c < 33) c=getc_unlocked(stdin);
(*n) = 0;
while (c>32) {(*n)=(*n)*10LL + (c-48); c=getc_unlocked(stdin);}
}
ordered_set secik;
int32_t main(){
//BOOST;
for (int i=0;i<2*MAX;i++)t[i]=-inf;
int n,k;
readUI(&n);
readUI(&k);
for (int i=1;i<=n;i++){
readUI(&l[i]);
readUI(&r[i]);
}
for (int i=1;i<=n;i++){
para[i]=l[i];
dara[i]=r[i];
}
for (int i=1;i<=n;i++){
if (l[i]>r[i])swap(l[i],r[i]);
}
for (int i=1;i<=n;i++){
tl[i]=l[i];
tr[i]=r[i]-1;
if (l[i]==r[i])continue;
pom.ins(tl[i]);
pom.ins(tr[i]);
}
for (int i=1;i<=k;i++)readUI(&c[i]),pom.ins(c[i]);
int licznik=1;
for (auto it:pom){
compress[it]=licznik++;
}
for (int i=1;i<=k;i++){
update(compress[c[i]],i);
}
ll ans=0;
for (int i=1;i<=n;i++){
if (l[i]==r[i])ans+=l[i];
else{
start[i]=query(compress[tl[i]],compress[tr[i]])+1;
rak.pb(mp(start[i],i));
}
}
sort(rak.begin(),rak.end());
// ll ans=0;
int akt=k;
int cnt=0;
while (!rak.empty()){
pii kot=rak.back();
rak.pop_back();
int id=kot.nd;
int doilu=kot.st;
while (akt>=doilu && akt>=1){
secik.ins(mp(c[akt],cnt++));
akt--;
}
int wykonuje=sz(secik)-(int)secik.order_of_key(mp(l[id],-inf));
if (doilu<0){
if (wykonuje&1)ans+=dara[id];
else ans+=para[id];
}
else{
if (wykonuje&1)ans+=l[id];
else ans+=r[id];
}
}
printf("%lld",ans);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |