#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MX=200010, inf=2e9;
int n, m, k;
int Q[MX];
pii C[MX];
vector<int> X;
struct SEG1{
int tree[2*MX*4], n; // max seg
void init(int sz){
n=sz;
for(int i=1; i<4*sz; i++) tree[i]=0;
}
void upt(int v, int s, int e, int pos, int val){
if(pos<s || e<pos) return;
tree[v]=max(tree[v], val);
if(s==e) return;
upt(v*2, s, (s+e)/2, pos, val);
upt(v*2+1, (s+e)/2+1, e, pos, val);
}
void upt(int pos, int val){
upt(1,1,n,pos,val);
}
int mx(int v, int s, int e, int l, int r){
if(e<l || r<s) return 0;
if(l<=s && e<=r) return tree[v];
return max(mx(v*2, s, (s+e)/2, l, r), mx(v*2+1, (s+e)/2+1, e, l, r));
}
int mx(int l, int r){
return mx(1,1,n,l,r);
}
} MT;
struct SEG2{
int tree[MX], n; // sum BIT
void init(int sz){
n=sz;
for(int i=1; i<=n; i++) tree[i]=0;
}
void upt(int pos){
for(; 0<pos && pos<=n; pos+=pos&(-pos))
tree[pos]++;
}
int sum(int pos){
int ans=0;
for(; pos>0; pos-=pos&(-pos))
ans+=tree[pos];
return ans;
}
int sum(int l, int r){
return sum(r)-sum(l-1);
}
} ST;
int find(int x){
// 이하인 최대 인덱스. 0~m
return upper_bound(X.begin(), X.end(), x)-X.begin();
}
ll solve(){
int idx[MX]; ll ans=0;
iota(idx, idx+k+1, 0);
sort(idx+1, idx+k+1, [](int a, int b){
return Q[a]<Q[b];
});
sort(C+1, C+n+1, [](pii &a, pii &b){
return max(a.first, a.second)>max(b.first, b.second);
});
ST.init(k);
MT.init(X.size());
for(int i=1, bck=k, frt=1; i<=n; i++){
int x,y; tie(x,y)=C[i];
if(x==y){ ans+=x; continue; }
if(x>y) swap(x,y);
while(bck>0 && Q[idx[bck]]>=y) ST.upt(idx[bck]), bck--;
while(frt<=k && Q[idx[frt]]<y) MT.upt(find(Q[idx[frt]]), idx[frt]), frt++;
int pos=MT.mx(find(x), find(y-1));
// printf("(%d, %d), frt: %d, bck: %d, cnt: %d\n", x,y,frt,bck,ST.sum(pos+1, k));
if(pos==0){
ans+= (ST.sum(pos+1, k)%2==1 ? C[i].second : C[i].first);
}
else{
ans+= (ST.sum(pos+1, k)%2==1 ? x : y);
}
}
return ans;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
cin>>n>>k;
for(int i=1, a, b; i<=n; i++){
cin>>a>>b;
C[i]={a,b};
X.push_back(a);
X.push_back(b);
}
sort(X.begin(), X.end());
X.resize(unique(X.begin(), X.end())-X.begin());
for(int i=1; i<=k; i++) cin>>Q[i];
cout<<solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
492 KB |
Output is correct |
3 |
Correct |
3 ms |
492 KB |
Output is correct |
4 |
Correct |
4 ms |
544 KB |
Output is correct |
5 |
Correct |
3 ms |
544 KB |
Output is correct |
6 |
Correct |
3 ms |
544 KB |
Output is correct |
7 |
Correct |
3 ms |
544 KB |
Output is correct |
8 |
Correct |
3 ms |
544 KB |
Output is correct |
9 |
Correct |
3 ms |
544 KB |
Output is correct |
10 |
Correct |
4 ms |
640 KB |
Output is correct |
11 |
Correct |
3 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
684 KB |
Output is correct |
13 |
Correct |
3 ms |
684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
492 KB |
Output is correct |
3 |
Correct |
3 ms |
492 KB |
Output is correct |
4 |
Correct |
4 ms |
544 KB |
Output is correct |
5 |
Correct |
3 ms |
544 KB |
Output is correct |
6 |
Correct |
3 ms |
544 KB |
Output is correct |
7 |
Correct |
3 ms |
544 KB |
Output is correct |
8 |
Correct |
3 ms |
544 KB |
Output is correct |
9 |
Correct |
3 ms |
544 KB |
Output is correct |
10 |
Correct |
4 ms |
640 KB |
Output is correct |
11 |
Correct |
3 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
684 KB |
Output is correct |
13 |
Correct |
3 ms |
684 KB |
Output is correct |
14 |
Correct |
18 ms |
1280 KB |
Output is correct |
15 |
Correct |
36 ms |
1880 KB |
Output is correct |
16 |
Correct |
52 ms |
2468 KB |
Output is correct |
17 |
Correct |
70 ms |
3024 KB |
Output is correct |
18 |
Correct |
79 ms |
3024 KB |
Output is correct |
19 |
Correct |
65 ms |
3120 KB |
Output is correct |
20 |
Correct |
63 ms |
3120 KB |
Output is correct |
21 |
Correct |
74 ms |
3120 KB |
Output is correct |
22 |
Correct |
50 ms |
3120 KB |
Output is correct |
23 |
Correct |
52 ms |
3120 KB |
Output is correct |
24 |
Correct |
53 ms |
3120 KB |
Output is correct |
25 |
Correct |
49 ms |
3120 KB |
Output is correct |
26 |
Correct |
47 ms |
3120 KB |
Output is correct |
27 |
Correct |
51 ms |
3120 KB |
Output is correct |
28 |
Correct |
50 ms |
3120 KB |
Output is correct |
29 |
Correct |
55 ms |
3120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
492 KB |
Output is correct |
3 |
Correct |
3 ms |
492 KB |
Output is correct |
4 |
Correct |
4 ms |
544 KB |
Output is correct |
5 |
Correct |
3 ms |
544 KB |
Output is correct |
6 |
Correct |
3 ms |
544 KB |
Output is correct |
7 |
Correct |
3 ms |
544 KB |
Output is correct |
8 |
Correct |
3 ms |
544 KB |
Output is correct |
9 |
Correct |
3 ms |
544 KB |
Output is correct |
10 |
Correct |
4 ms |
640 KB |
Output is correct |
11 |
Correct |
3 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
684 KB |
Output is correct |
13 |
Correct |
3 ms |
684 KB |
Output is correct |
14 |
Correct |
18 ms |
1280 KB |
Output is correct |
15 |
Correct |
36 ms |
1880 KB |
Output is correct |
16 |
Correct |
52 ms |
2468 KB |
Output is correct |
17 |
Correct |
70 ms |
3024 KB |
Output is correct |
18 |
Correct |
79 ms |
3024 KB |
Output is correct |
19 |
Correct |
65 ms |
3120 KB |
Output is correct |
20 |
Correct |
63 ms |
3120 KB |
Output is correct |
21 |
Correct |
74 ms |
3120 KB |
Output is correct |
22 |
Correct |
50 ms |
3120 KB |
Output is correct |
23 |
Correct |
52 ms |
3120 KB |
Output is correct |
24 |
Correct |
53 ms |
3120 KB |
Output is correct |
25 |
Correct |
49 ms |
3120 KB |
Output is correct |
26 |
Correct |
47 ms |
3120 KB |
Output is correct |
27 |
Correct |
51 ms |
3120 KB |
Output is correct |
28 |
Correct |
50 ms |
3120 KB |
Output is correct |
29 |
Correct |
55 ms |
3120 KB |
Output is correct |
30 |
Correct |
97 ms |
3564 KB |
Output is correct |
31 |
Correct |
170 ms |
5436 KB |
Output is correct |
32 |
Correct |
243 ms |
7780 KB |
Output is correct |
33 |
Correct |
395 ms |
12508 KB |
Output is correct |
34 |
Correct |
58 ms |
12508 KB |
Output is correct |
35 |
Correct |
417 ms |
12508 KB |
Output is correct |
36 |
Correct |
392 ms |
12508 KB |
Output is correct |
37 |
Correct |
401 ms |
12508 KB |
Output is correct |
38 |
Correct |
387 ms |
12636 KB |
Output is correct |
39 |
Correct |
357 ms |
12636 KB |
Output is correct |
40 |
Correct |
359 ms |
12636 KB |
Output is correct |
41 |
Correct |
361 ms |
12636 KB |
Output is correct |
42 |
Correct |
360 ms |
12636 KB |
Output is correct |
43 |
Correct |
319 ms |
12636 KB |
Output is correct |
44 |
Correct |
261 ms |
12636 KB |
Output is correct |
45 |
Correct |
260 ms |
12636 KB |
Output is correct |
46 |
Correct |
286 ms |
12636 KB |
Output is correct |
47 |
Correct |
263 ms |
12636 KB |
Output is correct |
48 |
Correct |
320 ms |
12636 KB |
Output is correct |
49 |
Correct |
265 ms |
12636 KB |
Output is correct |