#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
int n,k,s[1<<19],q[200001],fen[600010];
long long ans;
map<int,int> mp;
priority_queue<pii> pq;
vector<int> v[200010];
struct node {
int a,b;
bool operator < (const node &o) {
return max(a,b)>max(o.a,o.b);
}
} c[200001];
void update(int l, int r, int idx, int x, int val) {
if (x>r || x<l) return;
if (l==r) s[idx]=val;
else {
int mid=(l+r)/2;
update(l,mid,2*idx,x,val);
update(mid+1,r,2*idx+1,x,val);
s[idx]=max(s[2*idx],s[2*idx+1]);
}
}
int query(int l, int r, int idx, int val) { //find the first element from left that's more than val
if (l==r) return l;
else {
int mid=(l+r)/2;
if (s[2*idx+1]>=val) return query(mid+1,r,2*idx+1,val);
else return query(l,mid,2*idx,val);
}
}
int f(int x) {
if (s[1]<x) return 0;
else return query(1,k,1,x);
}
int find(int x) {
int sum=0;
while (x) {
sum+=fen[x];
x-=x&-x;
}
return sum;
}
void upd(int x) {
while (x<=600005) {
++fen[x];
x+=x&-x;
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>k;
for (int i=1; i<=n; ++i) {
cin>>c[i].a>>c[i].b;
mp[c[i].a]=0; mp[c[i].b]=0;
}
sort(c+1,c+n+1);
for (int i=1; i<=k; ++i) {
cin>>q[i];
pq.push(pii(q[i],i));
update(1,k,1,i,q[i]);
mp[q[i]]=0;
}
int idx=0;
for (auto it=mp.begin(); it!=mp.end(); ++it) (*it).second=++idx;
for (int i=1; i<=n; ++i) {
while (!pq.empty() && pq.top().first>=max(c[i].a,c[i].b)) {
update(1,k,1,pq.top().second,0);
pq.pop();
}
v[f(min(c[i].a,c[i].b))].push_back(i);
}
int cnt=0;
for (int i=k; i>0; --i) {
for (auto x : v[i]) {
if ((cnt-find(mp[max(c[x].a,c[x].b)]-1))%2==0) ans+=max(c[x].a,c[x].b);
else ans+=min(c[x].a,c[x].b);
}
upd(mp[q[i]]); ++cnt;
}
for (auto x : v[0]) {
if ((cnt-find(mp[max(c[x].a,c[x].b)]-1))%2==0) ans+=c[x].a;
else ans+=c[x].b;
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
4 ms |
5204 KB |
Output is correct |
3 |
Correct |
4 ms |
5204 KB |
Output is correct |
4 |
Correct |
4 ms |
5208 KB |
Output is correct |
5 |
Correct |
4 ms |
5204 KB |
Output is correct |
6 |
Correct |
4 ms |
5204 KB |
Output is correct |
7 |
Correct |
4 ms |
5204 KB |
Output is correct |
8 |
Correct |
4 ms |
5204 KB |
Output is correct |
9 |
Correct |
4 ms |
5204 KB |
Output is correct |
10 |
Correct |
4 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
4 ms |
5172 KB |
Output is correct |
13 |
Correct |
6 ms |
5224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
4 ms |
5204 KB |
Output is correct |
3 |
Correct |
4 ms |
5204 KB |
Output is correct |
4 |
Correct |
4 ms |
5208 KB |
Output is correct |
5 |
Correct |
4 ms |
5204 KB |
Output is correct |
6 |
Correct |
4 ms |
5204 KB |
Output is correct |
7 |
Correct |
4 ms |
5204 KB |
Output is correct |
8 |
Correct |
4 ms |
5204 KB |
Output is correct |
9 |
Correct |
4 ms |
5204 KB |
Output is correct |
10 |
Correct |
4 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
4 ms |
5172 KB |
Output is correct |
13 |
Correct |
6 ms |
5224 KB |
Output is correct |
14 |
Correct |
23 ms |
7216 KB |
Output is correct |
15 |
Correct |
44 ms |
9348 KB |
Output is correct |
16 |
Correct |
71 ms |
11508 KB |
Output is correct |
17 |
Correct |
100 ms |
13744 KB |
Output is correct |
18 |
Correct |
102 ms |
13740 KB |
Output is correct |
19 |
Correct |
104 ms |
13840 KB |
Output is correct |
20 |
Correct |
110 ms |
13872 KB |
Output is correct |
21 |
Correct |
94 ms |
13908 KB |
Output is correct |
22 |
Correct |
79 ms |
11376 KB |
Output is correct |
23 |
Correct |
64 ms |
10324 KB |
Output is correct |
24 |
Correct |
59 ms |
9476 KB |
Output is correct |
25 |
Correct |
78 ms |
12136 KB |
Output is correct |
26 |
Correct |
67 ms |
11152 KB |
Output is correct |
27 |
Correct |
79 ms |
11800 KB |
Output is correct |
28 |
Correct |
77 ms |
11712 KB |
Output is correct |
29 |
Correct |
88 ms |
12908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
4 ms |
5204 KB |
Output is correct |
3 |
Correct |
4 ms |
5204 KB |
Output is correct |
4 |
Correct |
4 ms |
5208 KB |
Output is correct |
5 |
Correct |
4 ms |
5204 KB |
Output is correct |
6 |
Correct |
4 ms |
5204 KB |
Output is correct |
7 |
Correct |
4 ms |
5204 KB |
Output is correct |
8 |
Correct |
4 ms |
5204 KB |
Output is correct |
9 |
Correct |
4 ms |
5204 KB |
Output is correct |
10 |
Correct |
4 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
4 ms |
5172 KB |
Output is correct |
13 |
Correct |
6 ms |
5224 KB |
Output is correct |
14 |
Correct |
23 ms |
7216 KB |
Output is correct |
15 |
Correct |
44 ms |
9348 KB |
Output is correct |
16 |
Correct |
71 ms |
11508 KB |
Output is correct |
17 |
Correct |
100 ms |
13744 KB |
Output is correct |
18 |
Correct |
102 ms |
13740 KB |
Output is correct |
19 |
Correct |
104 ms |
13840 KB |
Output is correct |
20 |
Correct |
110 ms |
13872 KB |
Output is correct |
21 |
Correct |
94 ms |
13908 KB |
Output is correct |
22 |
Correct |
79 ms |
11376 KB |
Output is correct |
23 |
Correct |
64 ms |
10324 KB |
Output is correct |
24 |
Correct |
59 ms |
9476 KB |
Output is correct |
25 |
Correct |
78 ms |
12136 KB |
Output is correct |
26 |
Correct |
67 ms |
11152 KB |
Output is correct |
27 |
Correct |
79 ms |
11800 KB |
Output is correct |
28 |
Correct |
77 ms |
11712 KB |
Output is correct |
29 |
Correct |
88 ms |
12908 KB |
Output is correct |
30 |
Correct |
366 ms |
22888 KB |
Output is correct |
31 |
Correct |
477 ms |
28228 KB |
Output is correct |
32 |
Correct |
602 ms |
34936 KB |
Output is correct |
33 |
Correct |
915 ms |
48360 KB |
Output is correct |
34 |
Correct |
282 ms |
21496 KB |
Output is correct |
35 |
Correct |
902 ms |
48420 KB |
Output is correct |
36 |
Correct |
921 ms |
48380 KB |
Output is correct |
37 |
Correct |
917 ms |
48192 KB |
Output is correct |
38 |
Correct |
853 ms |
48624 KB |
Output is correct |
39 |
Correct |
863 ms |
48272 KB |
Output is correct |
40 |
Correct |
666 ms |
48840 KB |
Output is correct |
41 |
Correct |
848 ms |
48292 KB |
Output is correct |
42 |
Correct |
893 ms |
48444 KB |
Output is correct |
43 |
Correct |
463 ms |
47636 KB |
Output is correct |
44 |
Correct |
449 ms |
47824 KB |
Output is correct |
45 |
Correct |
467 ms |
47968 KB |
Output is correct |
46 |
Correct |
487 ms |
31552 KB |
Output is correct |
47 |
Correct |
429 ms |
25948 KB |
Output is correct |
48 |
Correct |
583 ms |
38332 KB |
Output is correct |
49 |
Correct |
573 ms |
38456 KB |
Output is correct |