#include <bits/stdc++.h>
using namespace std;
typedef int64_t llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
llo n,k;
llo aa[200001];
llo bb[200001];
llo pp[200001];
vector<llo> tree2[1];
vector<pair<llo,llo>> tree[800001];
llo build2(llo no1,llo no,llo l,llo r){
if(l==r){
tree2[no1][no]=tree[no1][l].b;
}
else{
llo mid=(l+r)/2;
build2(no1,no*2+1,l,mid);
build2(no1,no*2+2,mid+1,r);
tree2[no1][no]=max(tree2[no1][no*2+1],tree2[no1][no*2+2]);
/*if(no1==0){
cout<<tree2[no][no]<<","<<l<<","<<r<<endl;
}*/
}
}
llo query3(llo no1,llo no,llo l,llo r,llo aa,llo bb){
if(r<aa or l>bb){
return -1;
}
//cout<<l<<"..."<<r<<endl;
if(aa<=l and r<=bb){
return tree2[no1][no];
}
else{
llo mid=(l+r)/2;
return max(query3(no1,no*2+1,l,mid,aa,bb),query3(no1,no*2+2,mid+1,r,aa,bb));
}
}
llo build(llo no,llo l,llo r){
if(no==0){
for(llo i=0;i<(r-l+1)*4;i++){
tree2[no].pb(0);
}
}
if(l==r){
tree[no].pb({pp[l],l});
}
else{
llo mid=(l+r)/2;
build(no*2+1,l,mid);
build(no*2+2,mid+1,r);
llo ind=0;
llo ind2=0;
while(ind<tree[no*2+1].size() and ind2<tree[no*2+2].size()){
if(tree[no*2+1][ind].a<tree[no*2+2][ind2].a){
tree[no].pb(tree[no*2+1][ind]);
ind+=1;
}
else{
tree[no].pb(tree[no*2+2][ind2]);
ind2+=1;
}
}
while(ind<tree[no*2+1].size()){
tree[no].pb(tree[no*2+1][ind]);
ind+=1;
}
while(ind2<tree[no*2+2].size()){
tree[no].pb(tree[no*2+2][ind2]);
ind2+=1;
}
}
if(no==0){
build2(no,0,0,r-l);
}
}
llo val1;
llo val2;
llo val3;
llo query(llo no,llo l,llo r,llo aa,llo bb){
if(tree[no].back().a<val1){
return -1;
}
if(tree[no][0].a>=val2){
return -1;
}
// cout<<l<<"::"<<r<<endl;
llo low=0;
llo high=r-l;
while(low<high-1){
llo mid=(low+high)/2;
if(tree[no][mid].a>=val1){
high=mid;
}
else{
low=mid;
}
}
llo ind=high;
if(tree[no][low].a>=val1){
ind=min(ind,low);
}
low=0;
high=r-l;
while(low<high-1){
llo mid=(low+high)/2;
if(tree[no][mid].a<val2){
low=mid;
}
else{
high=mid;
}
}
llo ind2=low;
if(tree[no][high].a<val2){
ind2=max(ind2,high);
}
// cout<<ind<<",,,,,"<<ind2<<endl;
if(ind>ind2){
return -1;
}
return query3(no,0,0,r-l,ind,ind2);
}
llo query2(llo no,llo l,llo r,llo aa,llo bb){
if(r<aa or bb<l){
return 0;
}
if(aa<=l and r<=bb){
if(tree[no].back().a<val3){
return 0;
}
llo low=0;
llo high=r-l;
while(low<high-1){
llo mid=(low+high)/2;
if(tree[no][mid].a>=val3){
high=mid;
}
else{
low=mid;
}
}
llo ind=high;
if(tree[no][low].a>=val3){
ind=min(ind,low);
}
return (llo)(tree[no].size())-ind;
}
else{
llo mid=(l+r)/2;
return query2(no*2+1,l,mid,aa,bb)+query2(no*2+2,mid+1,r,aa,bb);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>k;
for(llo i=0;i<n;i++){
cin>>aa[i];
cin>>bb[i];
}
for(llo i=0;i<k;i++){
cin>>pp[i];
}
build(0,0,k-1);
llo ans=0;
for(llo i=0;i<n;i++){
val1=min(aa[i],bb[i]);
val2=max(aa[i],bb[i]);
// cout<<val1<<"///"<<val2<<endl;
llo indd=query(0,0,k-1,0,k-1);
indd+=1;
// cout<<i<<","<<indd-1<<endl;
if(indd>=k){
if(aa[i]<bb[i]){
ans+=bb[i];
}
else if(bb[i]<aa[i]){
ans+=aa[i];
}
continue;
}
val3=max(val1,val2);
llo co=query2(0,0,k-1,indd,k-1);
//cout<<co<<endl;
llo st;
if(indd==0){
st=0;
}
else{
if(aa[i]<bb[i]){
st=1;
}
else{
st=0;
}
}
st+=co;
st%=2;
if(st==1){
ans+=bb[i];
}
else{
ans+=aa[i];
}
}
cout<<ans<<endl;
return 0;
}
Compilation message
fortune_telling2.cpp: In function 'llo build2(llo, llo, llo, llo)':
fortune_telling2.cpp:29:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
fortune_telling2.cpp: In function 'llo build(llo, llo, llo)':
fortune_telling2.cpp:60:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ind<tree[no*2+1].size() and ind2<tree[no*2+2].size()){
~~~^~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:60:41: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ind<tree[no*2+1].size() and ind2<tree[no*2+2].size()){
~~~~^~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:70:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ind<tree[no*2+1].size()){
~~~^~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:74:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ind2<tree[no*2+2].size()){
~~~~^~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:82:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
19456 KB |
Output is correct |
2 |
Correct |
17 ms |
19456 KB |
Output is correct |
3 |
Correct |
17 ms |
19456 KB |
Output is correct |
4 |
Correct |
17 ms |
19456 KB |
Output is correct |
5 |
Correct |
17 ms |
19456 KB |
Output is correct |
6 |
Correct |
17 ms |
19456 KB |
Output is correct |
7 |
Correct |
17 ms |
19456 KB |
Output is correct |
8 |
Correct |
17 ms |
19456 KB |
Output is correct |
9 |
Correct |
18 ms |
19584 KB |
Output is correct |
10 |
Correct |
17 ms |
19456 KB |
Output is correct |
11 |
Correct |
17 ms |
19456 KB |
Output is correct |
12 |
Correct |
17 ms |
19456 KB |
Output is correct |
13 |
Correct |
17 ms |
19432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
19456 KB |
Output is correct |
2 |
Correct |
17 ms |
19456 KB |
Output is correct |
3 |
Correct |
17 ms |
19456 KB |
Output is correct |
4 |
Correct |
17 ms |
19456 KB |
Output is correct |
5 |
Correct |
17 ms |
19456 KB |
Output is correct |
6 |
Correct |
17 ms |
19456 KB |
Output is correct |
7 |
Correct |
17 ms |
19456 KB |
Output is correct |
8 |
Correct |
17 ms |
19456 KB |
Output is correct |
9 |
Correct |
18 ms |
19584 KB |
Output is correct |
10 |
Correct |
17 ms |
19456 KB |
Output is correct |
11 |
Correct |
17 ms |
19456 KB |
Output is correct |
12 |
Correct |
17 ms |
19456 KB |
Output is correct |
13 |
Correct |
17 ms |
19432 KB |
Output is correct |
14 |
Correct |
34 ms |
23668 KB |
Output is correct |
15 |
Correct |
53 ms |
28404 KB |
Output is correct |
16 |
Correct |
75 ms |
31160 KB |
Output is correct |
17 |
Correct |
93 ms |
38380 KB |
Output is correct |
18 |
Correct |
93 ms |
38416 KB |
Output is correct |
19 |
Correct |
92 ms |
38372 KB |
Output is correct |
20 |
Correct |
95 ms |
38380 KB |
Output is correct |
21 |
Correct |
92 ms |
38380 KB |
Output is correct |
22 |
Correct |
77 ms |
37988 KB |
Output is correct |
23 |
Correct |
77 ms |
37992 KB |
Output is correct |
24 |
Correct |
77 ms |
37996 KB |
Output is correct |
25 |
Correct |
74 ms |
38120 KB |
Output is correct |
26 |
Correct |
85 ms |
38376 KB |
Output is correct |
27 |
Correct |
99 ms |
38380 KB |
Output is correct |
28 |
Correct |
85 ms |
38376 KB |
Output is correct |
29 |
Correct |
97 ms |
38376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
19456 KB |
Output is correct |
2 |
Correct |
17 ms |
19456 KB |
Output is correct |
3 |
Correct |
17 ms |
19456 KB |
Output is correct |
4 |
Correct |
17 ms |
19456 KB |
Output is correct |
5 |
Correct |
17 ms |
19456 KB |
Output is correct |
6 |
Correct |
17 ms |
19456 KB |
Output is correct |
7 |
Correct |
17 ms |
19456 KB |
Output is correct |
8 |
Correct |
17 ms |
19456 KB |
Output is correct |
9 |
Correct |
18 ms |
19584 KB |
Output is correct |
10 |
Correct |
17 ms |
19456 KB |
Output is correct |
11 |
Correct |
17 ms |
19456 KB |
Output is correct |
12 |
Correct |
17 ms |
19456 KB |
Output is correct |
13 |
Correct |
17 ms |
19432 KB |
Output is correct |
14 |
Correct |
34 ms |
23668 KB |
Output is correct |
15 |
Correct |
53 ms |
28404 KB |
Output is correct |
16 |
Correct |
75 ms |
31160 KB |
Output is correct |
17 |
Correct |
93 ms |
38380 KB |
Output is correct |
18 |
Correct |
93 ms |
38416 KB |
Output is correct |
19 |
Correct |
92 ms |
38372 KB |
Output is correct |
20 |
Correct |
95 ms |
38380 KB |
Output is correct |
21 |
Correct |
92 ms |
38380 KB |
Output is correct |
22 |
Correct |
77 ms |
37988 KB |
Output is correct |
23 |
Correct |
77 ms |
37992 KB |
Output is correct |
24 |
Correct |
77 ms |
37996 KB |
Output is correct |
25 |
Correct |
74 ms |
38120 KB |
Output is correct |
26 |
Correct |
85 ms |
38376 KB |
Output is correct |
27 |
Correct |
99 ms |
38380 KB |
Output is correct |
28 |
Correct |
85 ms |
38376 KB |
Output is correct |
29 |
Correct |
97 ms |
38376 KB |
Output is correct |
30 |
Correct |
237 ms |
108180 KB |
Output is correct |
31 |
Correct |
288 ms |
109524 KB |
Output is correct |
32 |
Correct |
364 ms |
111192 KB |
Output is correct |
33 |
Correct |
513 ms |
114768 KB |
Output is correct |
34 |
Correct |
221 ms |
107736 KB |
Output is correct |
35 |
Correct |
500 ms |
114772 KB |
Output is correct |
36 |
Correct |
482 ms |
114900 KB |
Output is correct |
37 |
Correct |
491 ms |
114900 KB |
Output is correct |
38 |
Correct |
485 ms |
114772 KB |
Output is correct |
39 |
Correct |
502 ms |
114904 KB |
Output is correct |
40 |
Correct |
408 ms |
114648 KB |
Output is correct |
41 |
Correct |
494 ms |
114772 KB |
Output is correct |
42 |
Correct |
502 ms |
114844 KB |
Output is correct |
43 |
Correct |
337 ms |
114260 KB |
Output is correct |
44 |
Correct |
326 ms |
114004 KB |
Output is correct |
45 |
Correct |
342 ms |
114132 KB |
Output is correct |
46 |
Correct |
354 ms |
112852 KB |
Output is correct |
47 |
Correct |
364 ms |
112724 KB |
Output is correct |
48 |
Correct |
433 ms |
114772 KB |
Output is correct |
49 |
Correct |
404 ms |
115028 KB |
Output is correct |