#include<bits/stdc++.h>
using namespace std;
struct SegTreeMax{
int tree[1<<21];
int sz;
void init(int n){
sz=1;
while(sz<n)sz*=2;
memset(tree,-1,2*sz*sizeof(tree[0]));
}
void update(int l,int r,int p,int idx,int val){
if(l==r){
tree[p]=val;
return;
}
int md=(l+r)>>1;
if(md>=idx)update(l,md,p*2+1,idx,val);
else update(md+1,r,p*2+2,idx,val);
tree[p]=max(tree[p*2+1],tree[p*2+2]);
}
void update(int idx,int val){
update(0,sz-1,0,idx,val);
}
int query(int l,int r,int p,int l_q,int r_q){
if(l>r_q||r<l_q)return -1;
if(l>=l_q&&r<=r_q)return tree[p];
int md=(l+r)>>1;
int ch1=query(l,md,p*2+1,l_q,r_q);
int ch2=query(md+1,r,p*2+2,l_q,r_q);
return max(ch1,ch2);
}
int query(int l,int r){
return query(0,sz-1,0,l,r);
}
}treeMax;
struct SegTreeSum{
int tree[1<<21];
int sz;
void init(int n){
sz=1;
while(sz<n)sz*=2;
memset(tree,0,2*sz*sizeof(tree[0]));
}
void update(int l,int r,int p,int idx,int val){
if(l==r){
tree[p]+=val;
return;
}
int md=(l+r)>>1;
if(md>=idx)update(l,md,p*2+1,idx,val);
else update(md+1,r,p*2+2,idx,val);
tree[p]=(tree[p*2+1]+tree[p*2+2]);
}
void update(int idx,int val){
update(0,sz-1,0,idx,val);
}
int query(int l,int r,int p,int l_q,int r_q){
if(l>r_q||r<l_q)return 0;
if(l>=l_q&&r<=r_q)return tree[p];
int md=(l+r)>>1;
int ch1=query(l,md,p*2+1,l_q,r_q);
int ch2=query(md+1,r,p*2+2,l_q,r_q);
return (ch1+ch2);
}
int query(int l,int r){
return query(0,sz-1,0,l,r);
}
}treeSum;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,k;
cin>>n>>k;
int a[2][n],b[2][n],q[2][k],c[n];
map<int,int>mp;
for(int i=0;i<n;i++)cin>>a[0][i]>>b[0][i],mp[a[0][i]]=1,mp[b[0][i]]=1;
for(int i=0;i<k;i++)cin>>q[0][i],mp[q[0][i]]=1;
int id=500;
for(auto&x:mp)x.second=id++;
for(int i=0;i<n;i++){
a[1][i]=mp[a[0][i]];
b[1][i]=mp[b[0][i]];
}
for(int i=0;i<k;i++)q[1][i]=mp[q[0][i]];
vector<vector<int>>v(k+1);
treeMax.init(id);
treeSum.init(id);
for(int i=0;i<k;i++)treeMax.update(q[1][i],i);
for(int i=0;i<n;i++){
int l=a[1][i],r=b[1][i];
if(l>r)swap(l,r);
int mx=-1;
if(l<r)mx=treeMax.query(l,r-1);
c[i]=mx;
v[mx+1].push_back(i);
}
long long sum=0;
for(int i=k;i>=0;i--){
if(i<k)treeSum.update(q[1][i],1);
for(auto&x:v[i]){
int cnt=treeSum.query(max(a[1][x],b[1][x]),id);
if(c[x]==-1){
if(cnt&1)sum+=b[0][x];
else sum+=a[0][x];
}else{
if(cnt&1)sum+=min(a[0][x],b[0][x]);
else sum+=max(a[0][x],b[0][x]);
}
}
}
cout<<sum;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
3 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
3 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
24 ms |
3068 KB |
Output is correct |
15 |
Correct |
52 ms |
5900 KB |
Output is correct |
16 |
Correct |
82 ms |
9184 KB |
Output is correct |
17 |
Correct |
113 ms |
11340 KB |
Output is correct |
18 |
Correct |
137 ms |
11348 KB |
Output is correct |
19 |
Correct |
120 ms |
11476 KB |
Output is correct |
20 |
Correct |
109 ms |
11400 KB |
Output is correct |
21 |
Correct |
113 ms |
11516 KB |
Output is correct |
22 |
Correct |
75 ms |
9052 KB |
Output is correct |
23 |
Correct |
83 ms |
7084 KB |
Output is correct |
24 |
Correct |
65 ms |
6320 KB |
Output is correct |
25 |
Correct |
95 ms |
9804 KB |
Output is correct |
26 |
Correct |
79 ms |
8948 KB |
Output is correct |
27 |
Correct |
93 ms |
9532 KB |
Output is correct |
28 |
Correct |
84 ms |
9600 KB |
Output is correct |
29 |
Correct |
108 ms |
10632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
3 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
24 ms |
3068 KB |
Output is correct |
15 |
Correct |
52 ms |
5900 KB |
Output is correct |
16 |
Correct |
82 ms |
9184 KB |
Output is correct |
17 |
Correct |
113 ms |
11340 KB |
Output is correct |
18 |
Correct |
137 ms |
11348 KB |
Output is correct |
19 |
Correct |
120 ms |
11476 KB |
Output is correct |
20 |
Correct |
109 ms |
11400 KB |
Output is correct |
21 |
Correct |
113 ms |
11516 KB |
Output is correct |
22 |
Correct |
75 ms |
9052 KB |
Output is correct |
23 |
Correct |
83 ms |
7084 KB |
Output is correct |
24 |
Correct |
65 ms |
6320 KB |
Output is correct |
25 |
Correct |
95 ms |
9804 KB |
Output is correct |
26 |
Correct |
79 ms |
8948 KB |
Output is correct |
27 |
Correct |
93 ms |
9532 KB |
Output is correct |
28 |
Correct |
84 ms |
9600 KB |
Output is correct |
29 |
Correct |
108 ms |
10632 KB |
Output is correct |
30 |
Correct |
284 ms |
23364 KB |
Output is correct |
31 |
Correct |
455 ms |
33080 KB |
Output is correct |
32 |
Correct |
664 ms |
39948 KB |
Output is correct |
33 |
Correct |
1186 ms |
61900 KB |
Output is correct |
34 |
Correct |
268 ms |
21964 KB |
Output is correct |
35 |
Correct |
1230 ms |
62160 KB |
Output is correct |
36 |
Correct |
1188 ms |
61772 KB |
Output is correct |
37 |
Correct |
1082 ms |
61908 KB |
Output is correct |
38 |
Correct |
1140 ms |
61904 KB |
Output is correct |
39 |
Correct |
1122 ms |
62116 KB |
Output is correct |
40 |
Correct |
943 ms |
62020 KB |
Output is correct |
41 |
Correct |
1132 ms |
61960 KB |
Output is correct |
42 |
Correct |
1075 ms |
61960 KB |
Output is correct |
43 |
Correct |
613 ms |
61276 KB |
Output is correct |
44 |
Correct |
602 ms |
61400 KB |
Output is correct |
45 |
Correct |
586 ms |
61240 KB |
Output is correct |
46 |
Correct |
502 ms |
37940 KB |
Output is correct |
47 |
Correct |
401 ms |
28824 KB |
Output is correct |
48 |
Correct |
593 ms |
44536 KB |
Output is correct |
49 |
Correct |
581 ms |
44440 KB |
Output is correct |