#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef long double ld;
const ll MOD=1e9+7;
const ll N=2e5+5;
const ld pi=3.14159265359;
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(a) (ll)a.size()
struct node{
ll l,r,val,ti;
node *lc,*rc;
}*rt=new node{0,MOD,0,0,0,0};
int getval(node *nd){return (!nd?0:nd->val);}
int getti(node *nd){return (!nd?0:nd->ti);}
void insert(node *nd,ll to,ll id){
if(nd->l==nd->r-1){nd->val=max(nd->val,id);nd->ti++;return;}
ll mid=(nd->l+nd->r)/2;
if(to>=mid){
if(!nd->rc)nd->rc=new node{mid,nd->r,0,0,0,0};
insert(nd->rc,to,id);
}else{
if(!nd->lc)nd->lc=new node{nd->l,mid,0,0,0,0};
insert(nd->lc,to,id);
}
nd->val=max(getval(nd->rc),getval(nd->lc));
nd->ti=getti(nd->lc)+getti(nd->rc);
}
void reset(node *nd){
if(!nd)return;
if(nd->lc)reset(nd->lc);
if(nd->rc)reset(nd->rc);
nd->val=nd->ti=0;
}
ll x[N],a[N],b[N],tt[N],lin[N],n,k;
long long ans;
vector<ll> v[N];
ll q(node *nd,ll ql,ll qr,ll ty){
if(!nd)return 0;
if(nd->l>=ql&&nd->r<=qr){return (ty?nd->ti:nd->val);}
if(nd->l>=qr||nd->r<=ql)return 0;
if(ty){
return q(nd->lc,ql,qr,ty)+q(nd->rc,ql,qr,ty);
}else{
return max(q(nd->lc,ql,qr,ty),q(nd->rc,ql,qr,ty));
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>k;
REP1(i,n)cin>>a[i]>>b[i];
REP1(i,k){
cin>>x[i];
insert(rt,x[i],i);
}
REP1(i,n){
ll tmp=q(rt,min(a[i],b[i]),max(a[i],b[i]),0);
v[tmp].pb(i);
lin[i]=tmp;
}
reset(rt);
for(ll i=k;i>=0;i--){
for(auto j:v[i]){
tt[j]=q(rt,max(a[j],b[j]),MOD,1);
}
insert(rt,x[i],i);
}
REP1(i,n){
if(lin[i]==0){
ans+=(tt[i]%2?b[i]:a[i]);
}else{
ans+=(tt[i]%2?min(a[i],b[i]):max(a[i],b[i]));
}
}
cout<<ans<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
6392 KB |
Output is correct |
2 |
Correct |
10 ms |
6392 KB |
Output is correct |
3 |
Correct |
11 ms |
6392 KB |
Output is correct |
4 |
Correct |
11 ms |
6392 KB |
Output is correct |
5 |
Correct |
11 ms |
6392 KB |
Output is correct |
6 |
Correct |
12 ms |
6392 KB |
Output is correct |
7 |
Correct |
11 ms |
6392 KB |
Output is correct |
8 |
Correct |
11 ms |
6392 KB |
Output is correct |
9 |
Correct |
11 ms |
6392 KB |
Output is correct |
10 |
Correct |
9 ms |
5240 KB |
Output is correct |
11 |
Correct |
10 ms |
6392 KB |
Output is correct |
12 |
Correct |
11 ms |
6392 KB |
Output is correct |
13 |
Correct |
11 ms |
6392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
6392 KB |
Output is correct |
2 |
Correct |
10 ms |
6392 KB |
Output is correct |
3 |
Correct |
11 ms |
6392 KB |
Output is correct |
4 |
Correct |
11 ms |
6392 KB |
Output is correct |
5 |
Correct |
11 ms |
6392 KB |
Output is correct |
6 |
Correct |
12 ms |
6392 KB |
Output is correct |
7 |
Correct |
11 ms |
6392 KB |
Output is correct |
8 |
Correct |
11 ms |
6392 KB |
Output is correct |
9 |
Correct |
11 ms |
6392 KB |
Output is correct |
10 |
Correct |
9 ms |
5240 KB |
Output is correct |
11 |
Correct |
10 ms |
6392 KB |
Output is correct |
12 |
Correct |
11 ms |
6392 KB |
Output is correct |
13 |
Correct |
11 ms |
6392 KB |
Output is correct |
14 |
Correct |
51 ms |
16632 KB |
Output is correct |
15 |
Correct |
109 ms |
27140 KB |
Output is correct |
16 |
Correct |
162 ms |
36856 KB |
Output is correct |
17 |
Correct |
216 ms |
46584 KB |
Output is correct |
18 |
Correct |
221 ms |
46712 KB |
Output is correct |
19 |
Correct |
213 ms |
46584 KB |
Output is correct |
20 |
Correct |
213 ms |
46456 KB |
Output is correct |
21 |
Correct |
203 ms |
46516 KB |
Output is correct |
22 |
Correct |
92 ms |
13044 KB |
Output is correct |
23 |
Correct |
94 ms |
12020 KB |
Output is correct |
24 |
Correct |
83 ms |
11256 KB |
Output is correct |
25 |
Correct |
99 ms |
14192 KB |
Output is correct |
26 |
Correct |
175 ms |
45816 KB |
Output is correct |
27 |
Correct |
185 ms |
46456 KB |
Output is correct |
28 |
Correct |
181 ms |
45176 KB |
Output is correct |
29 |
Correct |
201 ms |
46580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
6392 KB |
Output is correct |
2 |
Correct |
10 ms |
6392 KB |
Output is correct |
3 |
Correct |
11 ms |
6392 KB |
Output is correct |
4 |
Correct |
11 ms |
6392 KB |
Output is correct |
5 |
Correct |
11 ms |
6392 KB |
Output is correct |
6 |
Correct |
12 ms |
6392 KB |
Output is correct |
7 |
Correct |
11 ms |
6392 KB |
Output is correct |
8 |
Correct |
11 ms |
6392 KB |
Output is correct |
9 |
Correct |
11 ms |
6392 KB |
Output is correct |
10 |
Correct |
9 ms |
5240 KB |
Output is correct |
11 |
Correct |
10 ms |
6392 KB |
Output is correct |
12 |
Correct |
11 ms |
6392 KB |
Output is correct |
13 |
Correct |
11 ms |
6392 KB |
Output is correct |
14 |
Correct |
51 ms |
16632 KB |
Output is correct |
15 |
Correct |
109 ms |
27140 KB |
Output is correct |
16 |
Correct |
162 ms |
36856 KB |
Output is correct |
17 |
Correct |
216 ms |
46584 KB |
Output is correct |
18 |
Correct |
221 ms |
46712 KB |
Output is correct |
19 |
Correct |
213 ms |
46584 KB |
Output is correct |
20 |
Correct |
213 ms |
46456 KB |
Output is correct |
21 |
Correct |
203 ms |
46516 KB |
Output is correct |
22 |
Correct |
92 ms |
13044 KB |
Output is correct |
23 |
Correct |
94 ms |
12020 KB |
Output is correct |
24 |
Correct |
83 ms |
11256 KB |
Output is correct |
25 |
Correct |
99 ms |
14192 KB |
Output is correct |
26 |
Correct |
175 ms |
45816 KB |
Output is correct |
27 |
Correct |
185 ms |
46456 KB |
Output is correct |
28 |
Correct |
181 ms |
45176 KB |
Output is correct |
29 |
Correct |
201 ms |
46580 KB |
Output is correct |
30 |
Correct |
760 ms |
175396 KB |
Output is correct |
31 |
Correct |
866 ms |
177012 KB |
Output is correct |
32 |
Correct |
1030 ms |
179056 KB |
Output is correct |
33 |
Correct |
1322 ms |
183028 KB |
Output is correct |
34 |
Correct |
711 ms |
174840 KB |
Output is correct |
35 |
Correct |
1344 ms |
182864 KB |
Output is correct |
36 |
Correct |
1369 ms |
183152 KB |
Output is correct |
37 |
Correct |
1327 ms |
182736 KB |
Output is correct |
38 |
Correct |
1326 ms |
183208 KB |
Output is correct |
39 |
Correct |
1296 ms |
183344 KB |
Output is correct |
40 |
Correct |
1139 ms |
182884 KB |
Output is correct |
41 |
Correct |
1292 ms |
183020 KB |
Output is correct |
42 |
Correct |
1308 ms |
183072 KB |
Output is correct |
43 |
Correct |
853 ms |
178772 KB |
Output is correct |
44 |
Correct |
844 ms |
170216 KB |
Output is correct |
45 |
Correct |
807 ms |
153832 KB |
Output is correct |
46 |
Correct |
601 ms |
40080 KB |
Output is correct |
47 |
Correct |
555 ms |
34596 KB |
Output is correct |
48 |
Correct |
1047 ms |
181344 KB |
Output is correct |
49 |
Correct |
1032 ms |
176100 KB |
Output is correct |