QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#391927 | #2697. Exhausting Errands | Diu | AC ✓ | 9ms | 24416kb | C++14 | 2.7kb | 2024-04-16 22:03:23 | 2024-04-16 22:03:24 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e6+10;
const ll Inf=1e17;
int ID,n,m;
ll L,s[N],t[N];
struct que{
ll k;int x;
bool operator<(const que h)const{return k<h.k;}
}q[N];
ll ans[N];
struct line{
ll l,r;
bool operator<(const line h)const{return l<h.l;}
}a[N],b[N],c[N];
ll f[N],g[N],rr[N];
void tomin(ll &x,ll y){if(x>y)x=y;}
set<pair<ll,int> > ss;
void work(){
ll mn=L,mx=0;
int n1=0,n2=1;
for(int i=1;i<=n;i++){
mn=min(mn,min(s[i],t[i]));
mx=max(mx,max(s[i],t[i]));
if(t[i]<s[i])a[++n1]={t[i],s[i]},rr[n1]=s[i];
c[i]={s[i],t[i]};
}
sort(rr+1,rr+n1+1);
sort(c+1,c+n+1);
sort(a+1,a+n1+1);
b[1].l=b[1].r=mn;
for(int i=1;i<=n1;i++){
if(b[n2].r<=a[i].l)b[++n2]=a[i];
else b[n2].r=max(b[n2].r,a[i].r);
}
b[n2+1].l=b[n2+1].r=mx,f[n2+1]=0;
++n2;
b[n2+1].l=b[n2+1].r=mx,f[n2+1]=0;
for(int i=n2;i>=1;i--){
f[i]=min(2*b[i].r-3*b[i].l+b[i+1].l+f[i+1],2*(mx-b[i].l));
}
g[n1+1]=Inf;
ll mc=mx;
int t=n,p=n2;
for(int i=n1;i>=1;i--){
while(t>=1&&c[t].l>rr[i])mc=min(mc,c[t].r),--t;
mc=min(mc,rr[i]);
while(p>1&&b[p-1].r>rr[i])--p;
if(mc>=b[p].l)g[i]=min(g[i+1],2*rr[i]-2*mn+mc+min(2*b[p].r-3*mc+b[p+1].l+f[p+1],2*(mx-mc)));
else g[i]=min(g[i+1],2*rr[i]-2*mn+b[p].l+f[p]);
// if(mc>=b[p].l)printf("%lld %lld %lld %lld %lld\n",rr[i],mc,b[p].l,b[p].r,rr[i]-2*mn+mc+min(2*b[p].r-3*mc+b[p+1].l+f[p+1],2*(mx-mc)));
// else printf("%lld %lld %lld %lld %lld\n",rr[i],mc,b[p].l,b[p].r,rr[i]-2*mn+b[p].l+f[p]);
}
// printf("%d %d\n",n1,n2);
// puts("");
ss.clear();
for(int i=1;i<=n;i++)if(c[i].l>c[i].r)ss.insert(make_pair(c[i].r,i));
ss.insert(make_pair(mx,0));
t=1,p=1;
int tt=1;
sort(q+1,q+m+1);
for(int i=1;i<=m;i++){
if(q[i].k>=mx)break;
if(q[i].k<=mn){
tomin(ans[q[i].x],mn-q[i].k+f[1]);
continue;
}
while(b[p].r<q[i].k)++p;
while(t<=n&&c[t].l<=q[i].k){
if(c[t].l>c[t].r)ss.erase(make_pair(c[t].r,t));
++t;
}
while(tt<=n1&&rr[tt]<q[i].k)++tt;
tomin(ans[q[i].x],-q[i].k+g[tt]);
ll lp=ss.begin()->first;
if(lp<=b[p].r)tomin(ans[q[i].x],lp+q[i].k-2*mn+min(2*b[p].r-3*lp+b[p+1].l+f[p+1],2*(mx-lp)));
// printf("%lld %lld\n",g[tt]-q[i].k,lp+q[i].k-2*mn+min(2*b[p].r-3*lp+b[p+1].l+f[p+1],2*(mx-lp)));
}
}
signed main(){
// freopen("bus.in","r",stdin);
// freopen("bus.out","w",stdout);
scanf("%lld%d",&L,&n);m=n;
for(int i=1;i<=n;i++)scanf("%lld%lld",&s[i],&t[i]);
for(int i=1;i<=m;i++)q[i].k=s[i],q[i].x=i,ans[i]=Inf;
work();
for(int i=1;i<=n;i++)s[i]=L-s[i]+1,t[i]=L-t[i]+1;
for(int i=1;i<=m;i++)q[i].k=L-q[i].k+1;
work();
ll Ans=ans[1];
for(int i=1;i<=m;i++)Ans=min(Ans,ans[i]);
printf("%lld\n",Ans);
}
/*
1 5 1 10
6 4
5 10
6 1
5 8
1 5
5
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 22320kb
input:
100 2 1 100 100 1
output:
198
result:
ok single line: '198'
Test #2:
score: 0
Accepted
time: 0ms
memory: 22240kb
input:
100 6 10 6 20 15 50 54 100 98 92 87 90 89
output:
102
result:
ok single line: '102'
Test #3:
score: 0
Accepted
time: 0ms
memory: 22236kb
input:
100 5 12 5 20 15 16 70 72 65 90 87
output:
117
result:
ok single line: '117'
Test #4:
score: 0
Accepted
time: 0ms
memory: 22396kb
input:
100 3 2 99 3 100 2 1
output:
100
result:
ok single line: '100'
Test #5:
score: 0
Accepted
time: 2ms
memory: 22216kb
input:
100 5 1 50 12 11 22 21 32 31 42 41
output:
57
result:
ok single line: '57'
Test #6:
score: 0
Accepted
time: 3ms
memory: 22200kb
input:
100 6 50 51 53 48 46 56 60 42 37 65 71 31
output:
74
result:
ok single line: '74'
Test #7:
score: 0
Accepted
time: 0ms
memory: 22392kb
input:
100 5 20 25 25 5 5 1 60 70 100 90
output:
129
result:
ok single line: '129'
Test #8:
score: 0
Accepted
time: 0ms
memory: 22308kb
input:
1000 5 300 100 800 810 850 880 860 859 820 819
output:
830
result:
ok single line: '830'
Test #9:
score: 0
Accepted
time: 3ms
memory: 22396kb
input:
100 4 10 90 40 30 38 32 36 34
output:
100
result:
ok single line: '100'
Test #10:
score: 0
Accepted
time: 0ms
memory: 22236kb
input:
100 7 30 35 50 56 80 82 76 72 60 56 32 28 26 25
output:
80
result:
ok single line: '80'
Test #11:
score: 0
Accepted
time: 0ms
memory: 22456kb
input:
100 1000 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6 16 6...
output:
10
result:
ok single line: '10'
Test #12:
score: 0
Accepted
time: 0ms
memory: 22400kb
input:
1000 195 30 40 915 925 145 155 815 825 310 320 710 720 885 895 245 255 795 805 690 700 675 685 805 815 135 145 200 210 100 110 185 195 905 915 495 505 280 290 265 275 865 875 350 360 560 570 655 665 500 510 440 450 535 545 255 265 680 690 445 455 705 715 980 990 340 350 25 35 125 135 275 285 845 855...
output:
980
result:
ok single line: '980'
Test #13:
score: 0
Accepted
time: 0ms
memory: 22316kb
input:
1000 195 40 30 915 925 145 155 815 825 310 320 710 720 885 895 245 255 795 805 690 700 685 675 805 815 135 145 200 210 100 110 185 195 905 915 495 505 280 290 265 275 875 865 350 360 560 570 655 665 500 510 440 450 535 545 255 265 680 690 445 455 715 705 980 990 340 350 25 35 125 135 275 285 845 855...
output:
1340
result:
ok single line: '1340'
Test #14:
score: 0
Accepted
time: 0ms
memory: 22256kb
input:
100 10 5 6 5 7 5 8 5 9 6 7 6 8 6 9 7 8 7 9 8 9
output:
4
result:
ok single line: '4'
Test #15:
score: 0
Accepted
time: 2ms
memory: 22396kb
input:
100 10 6 5 7 5 8 5 9 5 7 6 8 6 9 6 8 7 9 7 9 8
output:
4
result:
ok single line: '4'
Test #16:
score: 0
Accepted
time: 0ms
memory: 22196kb
input:
100 20 5 6 5 7 5 8 5 9 6 7 6 8 6 9 7 8 7 9 8 9 6 5 7 5 8 5 9 5 7 6 8 6 9 6 8 7 9 7 9 8
output:
8
result:
ok single line: '8'
Test #17:
score: 0
Accepted
time: 3ms
memory: 22324kb
input:
100 20 15 16 15 17 15 18 15 19 16 17 16 18 16 19 17 18 17 19 18 19 6 5 7 5 8 5 9 5 7 6 8 6 9 6 8 7 9 7 9 8
output:
18
result:
ok single line: '18'
Test #18:
score: 0
Accepted
time: 0ms
memory: 22332kb
input:
100 20 5 6 5 7 5 8 5 9 6 7 6 8 6 9 7 8 7 9 8 9 16 15 17 15 18 15 19 15 17 16 18 16 19 16 18 17 19 17 19 18
output:
18
result:
ok single line: '18'
Test #19:
score: 0
Accepted
time: 3ms
memory: 22736kb
input:
1000000000 10000 209348967 209348968 229937124 229937125 444539823 444539824 834193586 834193587 753724297 753724298 69731199 69731200 847194618 847194619 182537687 182537686 547230986 547230985 625700633 625700632 694773911 694773910 673553370 673553371 548011928 548011929 575352319 575352320 79658...
output:
999870208
result:
ok single line: '999870208'
Test #20:
score: 0
Accepted
time: 7ms
memory: 22676kb
input:
1000000000 10000 316337267 316337328 601315045 601315059 532181349 532181372 414591349 414591433 466115096 466115058 975372982 975372998 295310234 295310169 159781558 159781467 223845529 223845508 264790491 264790530 670386459 670386498 361735296 361735274 706122398 706122376 485946770 485946834 894...
output:
1000196604
result:
ok single line: '1000196604'
Test #21:
score: 0
Accepted
time: 0ms
memory: 22356kb
input:
10000 1000 1068 1060 1000 1009 1021 1014 1079 1088 1045 1052 1029 1037 1033 1027 1017 1015 1049 1046 1067 1071 1083 1089 1047 1056 1049 1052 1097 1106 1047 1048 1014 1006 1093 1095 1020 1027 1091 1094 1083 1076 1050 1056 1004 1013 1071 1064 1041 1039 1069 1075 1082 1087 1040 1035 1021 1013 1004 1008...
output:
224
result:
ok single line: '224'
Test #22:
score: 0
Accepted
time: 0ms
memory: 24416kb
input:
100000000 1000 20421147 20421146 55048636 55048635 73963675 73963674 4816305 4816304 76848739 76848738 97540466 97540465 89999401 89999402 41106139 41106138 88221372 88221373 41337588 41337587 5376965 5376966 88580691 88580692 15869586 15869585 81562089 81562088 62150475 62150476 48744989 48744990 1...
output:
99892329
result:
ok single line: '99892329'
Test #23:
score: 0
Accepted
time: 0ms
memory: 22460kb
input:
100000000 2000 14701499 14701498 33945296 33945297 88932681 88932682 23027294 23027293 75633747 75633746 53592756 53592755 27274503 27274504 3151839 3151840 65721952 65721951 60878217 60878216 82306884 82306885 20181216 20181215 55409757 55409758 86431508 86431507 91101189 91101190 62329363 62329364...
output:
99702793
result:
ok single line: '99702793'
Test #24:
score: 0
Accepted
time: 0ms
memory: 22368kb
input:
100000000 4000 79628643 79628644 79215193 79215194 40936090 40936091 15553634 15553635 50633493 50633494 87511539 87511540 61051247 61051246 98294975 98294974 68747010 68747011 34921612 34921611 99394195 99394196 48706506 48706505 33166543 33166542 26008861 26008862 15846417 15846418 829920 829919 7...
output:
99867443
result:
ok single line: '99867443'
Test #25:
score: 0
Accepted
time: 0ms
memory: 22200kb
input:
1000 98 10 990 15 985 20 980 25 975 30 970 35 965 40 960 45 955 50 950 55 945 60 940 65 935 70 930 75 925 80 920 85 915 90 910 95 905 100 900 105 895 110 890 115 885 120 880 125 875 130 870 135 865 140 860 145 855 150 850 155 845 160 840 165 835 170 830 175 825 180 820 185 815 190 810 195 805 200 80...
output:
980
result:
ok single line: '980'
Test #26:
score: 0
Accepted
time: 0ms
memory: 22392kb
input:
1000 98 10 990 15 985 20 980 25 975 30 970 35 965 40 960 45 955 50 950 55 945 60 940 65 935 70 930 75 925 80 920 85 915 90 910 95 905 100 900 105 895 110 890 115 885 120 880 125 875 130 870 135 865 140 860 145 855 150 850 155 845 160 840 165 835 170 830 175 825 180 820 185 815 190 810 195 805 200 80...
output:
1200
result:
ok single line: '1200'
Test #27:
score: 0
Accepted
time: 3ms
memory: 22236kb
input:
1000 92 50 40 960 950 50 950 55 945 60 940 65 935 70 930 75 925 80 920 85 915 90 910 95 905 100 900 105 895 110 890 115 885 120 880 125 875 130 870 135 865 140 860 145 855 150 850 155 845 160 840 165 835 170 830 175 825 180 820 185 815 190 810 195 805 200 800 205 795 210 790 215 785 220 780 225 775 ...
output:
940
result:
ok single line: '940'
Test #28:
score: 0
Accepted
time: 0ms
memory: 22304kb
input:
1000 92 50 40 960 950 50 950 55 945 60 940 65 935 70 930 75 925 80 920 85 915 90 910 95 905 100 900 105 895 110 890 115 885 120 880 125 875 130 870 135 865 140 860 145 855 150 850 155 845 160 840 165 835 170 830 175 825 180 820 185 815 190 810 195 805 200 800 205 795 210 790 215 785 220 780 225 775 ...
output:
1160
result:
ok single line: '1160'
Test #29:
score: 0
Accepted
time: 3ms
memory: 22396kb
input:
100 12 60 56 58 52 42 43 52 78 53 91 95 64 16 19 45 24 65 97 92 84 79 66 39 53
output:
142
result:
ok single line: '142'
Test #30:
score: 0
Accepted
time: 0ms
memory: 22240kb
input:
10000 10 4147 4146 7371 7370 3410 3411 9793 9792 6589 6588 6897 6898 8034 8033 4478 4477 1558 1557 824 825
output:
8974
result:
ok single line: '8974'
Test #31:
score: 0
Accepted
time: 0ms
memory: 22200kb
input:
100 10 16 15 11 10 23 22 49 48 34 33 32 31 58 59 19 18 84 85 50 51
output:
80
result:
ok single line: '80'
Test #32:
score: 0
Accepted
time: 0ms
memory: 22320kb
input:
100 7 16 15 11 10 23 22 49 48 34 33 32 31 58 59
output:
50
result:
ok single line: '50'
Test #33:
score: 0
Accepted
time: 0ms
memory: 22316kb
input:
10 6 1 4 3 5 6 7 2 1 9 4 8 5
output:
14
result:
ok single line: '14'
Test #34:
score: 0
Accepted
time: 0ms
memory: 22324kb
input:
100 3 11 50 50 49 36 35
output:
42
result:
ok single line: '42'
Test #35:
score: 0
Accepted
time: 0ms
memory: 22340kb
input:
1000000000 1000 106800000 106000000 100000000 100900000 102100000 101400000 107900000 108800000 104500000 105200000 102900000 103700000 103300000 102700000 101700000 101500000 104900000 104600000 106700000 107100000 108300000 108900000 104700000 105600000 104900000 105200000 109700000 110600000 1047...
output:
22400000
result:
ok single line: '22400000'
Test #36:
score: 0
Accepted
time: 0ms
memory: 22328kb
input:
1000000000 12 600000000 560000000 580000000 520000000 420000000 430000000 520000000 780000000 530000000 910000000 950000000 640000000 160000000 190000000 450000000 240000000 650000000 970000000 920000000 840000000 790000000 660000000 390000000 530000000
output:
1420000000
result:
ok single line: '1420000000'
Test #37:
score: 0
Accepted
time: 3ms
memory: 22248kb
input:
100000000 10 41474147 41464146 73717371 73707370 34103410 34113411 97939793 97929792 65896589 65886588 68976897 68986898 80348034 80338033 44784478 44774477 15581558 15571557 8240824 8250825
output:
89748974
result:
ok single line: '89748974'
Test #38:
score: 0
Accepted
time: 0ms
memory: 22248kb
input:
1000000000 10 524574766 467829835 788760015 749931008 683561222 688458529 490326602 466294773 275555931 196523978 440746441 464654142 548562021 630916391 411231136 387178580 821334137 849367674 87887936 100019122
output:
1023963217
result:
ok single line: '1023963217'
Test #39:
score: 0
Accepted
time: 9ms
memory: 22604kb
input:
1000000 10000 355032 355580 184745 184959 87644 88493 83931 84638 513415 514343 557120 557346 477755 478077 116652 115805 220660 220989 747101 746839 674063 674752 459344 458556 718257 717393 854296 854755 334181 333409 445981 446979 140393 139994 66404 65570 111308 111692 956080 955110 146575 14704...
output:
1997935
result:
ok single line: '1997935'
Test #40:
score: 0
Accepted
time: 3ms
memory: 22396kb
input:
150 9 40 38 30 29 25 10 8 1 50 60 68 64 89 88 128 110 150 149
output:
169
result:
ok single line: '169'
Test #41:
score: 0
Accepted
time: 0ms
memory: 22240kb
input:
1150 9 40 38 30 29 25 10 8 1 100 900 1068 1064 1089 1088 1128 1110 1150 1149
output:
1226
result:
ok single line: '1226'
Test #42:
score: 0
Accepted
time: 0ms
memory: 22396kb
input:
1150 5 40 38 30 29 25 10 8 1 100 900
output:
929
result:
ok single line: '929'
Test #43:
score: 0
Accepted
time: 0ms
memory: 22308kb
input:
1150 5 100 900 1068 1064 1089 1088 1128 1110 1150 1149
output:
1097
result:
ok single line: '1097'