#include<bits/stdc++.h>
using namespace std;
#define int long long
#define maxn 200005
#define pii pair<int,int>
int n,k,a[maxn],b[maxn],bit[2*maxn],t[maxn],f[maxn],cnt[maxn],ans;
pii p[maxn];
vector<pii> que[maxn],l[maxn];
vector<int> com;
int query(int x){
if(x==0) return 0;
int res=0;
for(int i=x;i>=1;i-=(i&(-i))) res+=bit[i];
return res;
}
void update(int x){
for(int i=x;i<=n+k;i+=(i&(-i))) bit[i]++;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
cin >> n >> k;
for(int i=1;i<=n;i++) cin >> a[i] >> b[i];
for(int i=1;i<=k;i++){cin >> t[i];p[i]={t[i],i};com.push_back(t[i]);}
sort(p+1,p+k+1);
for(int i=1;i<=n;i++){
int mn=min(a[i],b[i]),mx=max(a[i],b[i]);
int pos=lower_bound(p+1,p+k+1,make_pair(mn,0LL))-p;
que[pos].push_back({mx-1,i});com.push_back(mx);
}
sort(com.begin(),com.end());
com.erase(unique(com.begin(),com.end()),com.end());
deque<pii> v;
for(int i=k;i>=1;i--){
while(!v.empty() && v.front().second<=p[i].second) v.pop_front();
v.push_front(p[i]);
for(pii x:que[i]){
int pos=upper_bound(v.begin(),v.end(),make_pair(x.first,k))-v.begin()-1;
if(pos>=0) f[x.second]=v[pos].second;
}
}
for(int i=1;i<=n;i++) l[f[i]+1].push_back({i,max(a[i],b[i])});
for(int i=k;i>=1;i--){
int pos=lower_bound(com.begin(),com.end(),t[i])-com.begin()+1;
update(pos);
for(pii x:l[i]){
int pos2=lower_bound(com.begin(),com.end(),x.second)-com.begin()+1;
cnt[x.first]=k-i+1-query(pos2-1);
}
}
for(int i=1;i<=n;i++){
//cout << f[i] << ' ' << cnt[i] << '\n';
if(f[i]==0){
if(cnt[i]%2==0) ans+=a[i];
else ans+=b[i];
}
else{
if(cnt[i]%2==0) ans+=max(a[i],b[i]);
else ans+=min(a[i],b[i]);
}
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9812 KB |
Output is correct |
3 |
Correct |
5 ms |
9812 KB |
Output is correct |
4 |
Correct |
6 ms |
9812 KB |
Output is correct |
5 |
Correct |
5 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9812 KB |
Output is correct |
8 |
Correct |
7 ms |
9812 KB |
Output is correct |
9 |
Correct |
5 ms |
9812 KB |
Output is correct |
10 |
Correct |
6 ms |
9812 KB |
Output is correct |
11 |
Correct |
6 ms |
9812 KB |
Output is correct |
12 |
Correct |
8 ms |
9812 KB |
Output is correct |
13 |
Correct |
6 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9812 KB |
Output is correct |
3 |
Correct |
5 ms |
9812 KB |
Output is correct |
4 |
Correct |
6 ms |
9812 KB |
Output is correct |
5 |
Correct |
5 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9812 KB |
Output is correct |
8 |
Correct |
7 ms |
9812 KB |
Output is correct |
9 |
Correct |
5 ms |
9812 KB |
Output is correct |
10 |
Correct |
6 ms |
9812 KB |
Output is correct |
11 |
Correct |
6 ms |
9812 KB |
Output is correct |
12 |
Correct |
8 ms |
9812 KB |
Output is correct |
13 |
Correct |
6 ms |
9812 KB |
Output is correct |
14 |
Correct |
14 ms |
11092 KB |
Output is correct |
15 |
Correct |
25 ms |
12456 KB |
Output is correct |
16 |
Correct |
33 ms |
13684 KB |
Output is correct |
17 |
Correct |
45 ms |
15120 KB |
Output is correct |
18 |
Correct |
44 ms |
15116 KB |
Output is correct |
19 |
Correct |
45 ms |
15172 KB |
Output is correct |
20 |
Correct |
49 ms |
15144 KB |
Output is correct |
21 |
Correct |
41 ms |
15688 KB |
Output is correct |
22 |
Correct |
33 ms |
14696 KB |
Output is correct |
23 |
Correct |
34 ms |
14800 KB |
Output is correct |
24 |
Correct |
36 ms |
14788 KB |
Output is correct |
25 |
Correct |
32 ms |
14664 KB |
Output is correct |
26 |
Correct |
35 ms |
14216 KB |
Output is correct |
27 |
Correct |
40 ms |
14968 KB |
Output is correct |
28 |
Correct |
38 ms |
15124 KB |
Output is correct |
29 |
Correct |
47 ms |
15176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9812 KB |
Output is correct |
3 |
Correct |
5 ms |
9812 KB |
Output is correct |
4 |
Correct |
6 ms |
9812 KB |
Output is correct |
5 |
Correct |
5 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
5 ms |
9812 KB |
Output is correct |
8 |
Correct |
7 ms |
9812 KB |
Output is correct |
9 |
Correct |
5 ms |
9812 KB |
Output is correct |
10 |
Correct |
6 ms |
9812 KB |
Output is correct |
11 |
Correct |
6 ms |
9812 KB |
Output is correct |
12 |
Correct |
8 ms |
9812 KB |
Output is correct |
13 |
Correct |
6 ms |
9812 KB |
Output is correct |
14 |
Correct |
14 ms |
11092 KB |
Output is correct |
15 |
Correct |
25 ms |
12456 KB |
Output is correct |
16 |
Correct |
33 ms |
13684 KB |
Output is correct |
17 |
Correct |
45 ms |
15120 KB |
Output is correct |
18 |
Correct |
44 ms |
15116 KB |
Output is correct |
19 |
Correct |
45 ms |
15172 KB |
Output is correct |
20 |
Correct |
49 ms |
15144 KB |
Output is correct |
21 |
Correct |
41 ms |
15688 KB |
Output is correct |
22 |
Correct |
33 ms |
14696 KB |
Output is correct |
23 |
Correct |
34 ms |
14800 KB |
Output is correct |
24 |
Correct |
36 ms |
14788 KB |
Output is correct |
25 |
Correct |
32 ms |
14664 KB |
Output is correct |
26 |
Correct |
35 ms |
14216 KB |
Output is correct |
27 |
Correct |
40 ms |
14968 KB |
Output is correct |
28 |
Correct |
38 ms |
15124 KB |
Output is correct |
29 |
Correct |
47 ms |
15176 KB |
Output is correct |
30 |
Correct |
107 ms |
18644 KB |
Output is correct |
31 |
Correct |
136 ms |
22564 KB |
Output is correct |
32 |
Correct |
165 ms |
27184 KB |
Output is correct |
33 |
Correct |
236 ms |
36668 KB |
Output is correct |
34 |
Correct |
91 ms |
17680 KB |
Output is correct |
35 |
Correct |
269 ms |
36920 KB |
Output is correct |
36 |
Correct |
239 ms |
36548 KB |
Output is correct |
37 |
Correct |
246 ms |
36352 KB |
Output is correct |
38 |
Correct |
239 ms |
36724 KB |
Output is correct |
39 |
Correct |
252 ms |
36304 KB |
Output is correct |
40 |
Correct |
184 ms |
33220 KB |
Output is correct |
41 |
Correct |
251 ms |
36240 KB |
Output is correct |
42 |
Correct |
242 ms |
35792 KB |
Output is correct |
43 |
Correct |
158 ms |
35312 KB |
Output is correct |
44 |
Correct |
157 ms |
35388 KB |
Output is correct |
45 |
Correct |
165 ms |
32276 KB |
Output is correct |
46 |
Correct |
166 ms |
34792 KB |
Output is correct |
47 |
Correct |
174 ms |
34692 KB |
Output is correct |
48 |
Correct |
184 ms |
34872 KB |
Output is correct |
49 |
Correct |
190 ms |
35588 KB |
Output is correct |