#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
const int K=1<<20;
int n,k;
int a[N],b[N],c[N],t[N];
int side[N];
vector<int> vec[N];
long long ans;
struct segtree{
int t[K];
void update(int l,int r,int i,int x,int v){
if(x<l||r<x)return;
t[i]=max(t[i],v);
if(l==r)return;
int m=(l+r)/2;
update(l,m,i*2,x,v);
update(m+1,r,i*2+1,x,v);
}
void update(int x,int v){
update(1,2*n,1,x,v);
}
int query(int l,int r,int i,int x,int y){
if(y<l||r<x)return 0;
if(x<=l&&r<=y)return t[i];
int m=(l+r)/2;
return max(query(l,m,i*2,x,y),query(m+1,r,i*2+1,x,y));
}
int query(int x,int y){
return query(1,2*n,1,x,y);
}
}s;
struct fenwick{
int t[2*N];
void add(int i,int v){
while(i<2*N)t[i]+=v,i+=i&-i;
}
int read(int i){
int res=0;
while(i>0)res+=t[i],i-=i&-i;
return res;
}
}f;
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> k;
for(int i=1;i<=n;i++){
cin >> a[i] >> b[i];
c[i*2-1]=a[i];
c[i*2]=b[i];
}
sort(c+1,c+2*n+1);
for(int i=1;i<=n;i++){
a[i]=lower_bound(c+1,c+2*n+1,a[i])-c;
b[i]=lower_bound(c+1,c+2*n+1,b[i])-c;
}
for(int i=1;i<=k;i++){
cin >> t[i];
t[i]=upper_bound(c+1,c+2*n+1,t[i])-c-1;
s.update(t[i],i);
}
for(int i=1;i<=n;i++){
int pos;
if(a[i]<b[i]){
pos=s.query(a[i],b[i]-1);
side[i]=1;
}else{
pos=s.query(b[i],a[i]-1);
side[i]=0;
}
if(pos==0)side[i]=0;
vec[pos].emplace_back(i);
}
for(int i=k;i>=0;i--){
for(auto j:vec[i])ans+=c[(side[j]+f.read(2*n)-f.read(max(a[j],b[j])-1))%2==0?a[j]:b[j]];
if(t[i])f.add(t[i],1);
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8680 KB |
Output is correct |
4 |
Correct |
2 ms |
8684 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8740 KB |
Output is correct |
7 |
Correct |
2 ms |
8536 KB |
Output is correct |
8 |
Correct |
2 ms |
8536 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8636 KB |
Output is correct |
11 |
Correct |
2 ms |
8712 KB |
Output is correct |
12 |
Correct |
3 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8680 KB |
Output is correct |
4 |
Correct |
2 ms |
8684 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8740 KB |
Output is correct |
7 |
Correct |
2 ms |
8536 KB |
Output is correct |
8 |
Correct |
2 ms |
8536 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8636 KB |
Output is correct |
11 |
Correct |
2 ms |
8712 KB |
Output is correct |
12 |
Correct |
3 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
12 ms |
9292 KB |
Output is correct |
15 |
Correct |
22 ms |
10172 KB |
Output is correct |
16 |
Correct |
33 ms |
10684 KB |
Output is correct |
17 |
Correct |
44 ms |
11600 KB |
Output is correct |
18 |
Correct |
43 ms |
11600 KB |
Output is correct |
19 |
Correct |
49 ms |
11664 KB |
Output is correct |
20 |
Correct |
47 ms |
11604 KB |
Output is correct |
21 |
Correct |
40 ms |
11796 KB |
Output is correct |
22 |
Correct |
31 ms |
11204 KB |
Output is correct |
23 |
Correct |
31 ms |
11092 KB |
Output is correct |
24 |
Correct |
33 ms |
11096 KB |
Output is correct |
25 |
Correct |
29 ms |
11224 KB |
Output is correct |
26 |
Correct |
30 ms |
11084 KB |
Output is correct |
27 |
Correct |
37 ms |
11600 KB |
Output is correct |
28 |
Correct |
31 ms |
11092 KB |
Output is correct |
29 |
Correct |
39 ms |
11856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8680 KB |
Output is correct |
4 |
Correct |
2 ms |
8684 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8740 KB |
Output is correct |
7 |
Correct |
2 ms |
8536 KB |
Output is correct |
8 |
Correct |
2 ms |
8536 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8636 KB |
Output is correct |
11 |
Correct |
2 ms |
8712 KB |
Output is correct |
12 |
Correct |
3 ms |
8540 KB |
Output is correct |
13 |
Correct |
2 ms |
8540 KB |
Output is correct |
14 |
Correct |
12 ms |
9292 KB |
Output is correct |
15 |
Correct |
22 ms |
10172 KB |
Output is correct |
16 |
Correct |
33 ms |
10684 KB |
Output is correct |
17 |
Correct |
44 ms |
11600 KB |
Output is correct |
18 |
Correct |
43 ms |
11600 KB |
Output is correct |
19 |
Correct |
49 ms |
11664 KB |
Output is correct |
20 |
Correct |
47 ms |
11604 KB |
Output is correct |
21 |
Correct |
40 ms |
11796 KB |
Output is correct |
22 |
Correct |
31 ms |
11204 KB |
Output is correct |
23 |
Correct |
31 ms |
11092 KB |
Output is correct |
24 |
Correct |
33 ms |
11096 KB |
Output is correct |
25 |
Correct |
29 ms |
11224 KB |
Output is correct |
26 |
Correct |
30 ms |
11084 KB |
Output is correct |
27 |
Correct |
37 ms |
11600 KB |
Output is correct |
28 |
Correct |
31 ms |
11092 KB |
Output is correct |
29 |
Correct |
39 ms |
11856 KB |
Output is correct |
30 |
Correct |
64 ms |
11088 KB |
Output is correct |
31 |
Correct |
109 ms |
13656 KB |
Output is correct |
32 |
Correct |
153 ms |
16844 KB |
Output is correct |
33 |
Incorrect |
195 ms |
19460 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |