QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#335419#2697. Exhausting ErrandsrageOfThunderAC ✓2ms4268kbC++143.8kb2024-02-23 12:39:342024-02-23 12:39:35

Judging History

你现在查看的是最新测评结果

  • [2024-02-23 12:39:35]
  • 评测
  • 测评结果:AC
  • 用时:2ms
  • 内存:4268kb
  • [2024-02-23 12:39:34]
  • 提交

answer

#include<bits/stdc++.h>

#define ll long long
#define mk make_pair
#define fi first
#define se second
#define int long long

using namespace std;

inline ll read(){
	ll x=0,f=1;char c=getchar();
	for(;(c<'0'||c>'9');c=getchar()){if(c=='-')f=-1;}
	for(;(c>='0'&&c<='9');c=getchar())x=x*10+(c&15);
	return x*f;
}

const int mod=998244353;
int ksm(int x,int y,int p=mod){
	int ans=1;
	for(int i=y;i;i>>=1,x=1ll*x*x%p)if(i&1)ans=1ll*ans*x%p;
	return ans%p;
}
int inv(int x,int p=mod){return ksm(x,p-2,p)%p;}
mt19937 rnd(time(0));
int randint(int l,int r){return rnd()%(r-l+1)+l;}
void add(int &x,int v){x+=v;if(x>=mod)x-=mod;}
void Mod(int &x){if(x>=mod)x-=mod;}
int cmod(int x){if(x>=mod)x-=mod;return x;}

void cmax(ll &x,ll v){x=max(x,v);}
void cmin(ll &x,ll v){x=min(x,v);}

const int N=1e6+5;
int n,pre[N],pos[N],op[N],m,len;
int L[N],R[N],a[N],b[N],c[N],d[N];
vector<pair<int,int> >A,B;

signed main(void){

#ifndef ONLINE_JUDGE
	freopen("game.in","r",stdin);
#endif
	// freopen("sample_game2.in","r",stdin);
	// freopen("game.out","w",stdout);

	len=read(),n=read();
	// for(int i=1;i<=n;i++){
		// pos[i]=read();
		// op[i]=read();
		// if(op[i]==2)pre[i]=read();
	// }
	// for(int i=1;i<=n;i++)if(op[i]==1)L[i]=R[i]=pos[i];
	// for(int i=1;i<=n;i++)if(op[i]==2)cmin(L[pre[i]],pos[i]),cmax(R[pre[i]],pos[i]);

	// for(int i=1;i<=n;i++)if(op[i]==1){
	// 	// cout<<"i = "<<i<<" L,R = "<<L[i]<<" "<<R[i]<<endl;
	// 	A.emplace_back(mk(pos[i],R[i]));
	// 	if(L[i]<pos[i])B.emplace_back(mk(L[i],pos[i]));
	// }

    for(int i=1;i<=n;i++){
        int l=read(),r=read();
        if(l<r)A.emplace_back(mk(l,r));
        else B.emplace_back(mk(r,l));
    }

	sort(A.begin(),A.end()),sort(B.begin(),B.end());
	auto Rec=[&](vector<pair<int,int> >&C){
		int tl=-1,tr=-1;
		vector<pair<int,int> >R;
		for(auto [l,r]:C){
			// cout<<"l,r = "<<l<<" "<<r<<" tl,tr = "<<tl<<" "<<tr<<endl;
			if(l>tr){
				if(tl!=-1)R.emplace_back(mk(tl,tr));
				tl=l,tr=r;
			}
			else cmax(tr,r);
		}
		if(tl!=-1)R.emplace_back(mk(tl,tr));
		C=R;
	};
	Rec(A),Rec(B);

	// cout<<"A = ";for(auto [l,r]:A)cout<<"["<<l<<","<<r<<"] ";puts("");
	// cout<<"B = ";for(auto [l,r]:B)cout<<"["<<l<<","<<r<<"] ";puts("");

	if(A.size()==0){
		cout<<B.back().se-B[0].fi<<endl;
		return 0;
	}
	if(B.size()==0){
		cout<<A.back().se-A[0].fi<<endl;
		return 0;
	}

	n=A.size(),m=B.size();
	for(int i=1;i<=n;i++)a[i]=A[i-1].fi,b[i]=A[i-1].se;
	for(int i=1;i<=m;i++)c[i]=B[i-1].fi,d[i]=B[i-1].se;

	// puts("ok");

	ll ans=1e18;
	vector<ll>sum(max(n,m)+5),vx(max(n,m)+5),vy(max(n,m)+5);

	for(int i=1;i<=m;i++)sum[i]=sum[i-1]+d[i]-c[i];
	for(int i=1;i<=m;i++)sum[i]*=2;
	for(int i=1;i<=m;i++)vx[i]=d[i]-c[1]+abs(c[1]-a[1]),vy[i]=d[m]-c[i]+abs(d[m]-b[n]);
	if(c[1]<a[1])vx[0]=1e14;
	if(d[m]>b[n])vy[m+1]=1e14;
	int mn=vx[0]-sum[0];
	for(int i=1;i<=m+1;i++){
		ans=min(ans,b[n]-a[1]+vy[i]+sum[i-1]+mn);
		// cout<<"i = "<<i<<" vx = "<<vx[i]<<" vy = "<<vy[i]<<" -> ans = "<<ans<<endl;
		// cout<<"mn = "<<mn<<" sum = "<<sum[i-1]<<endl;
		mn=min(mn,vx[i]-sum[i]);
	}
	// cout<<"now ans = "<<ans<<endl;
	// for(int i=0;i<=m;i++)for(int j=i+1;j<=m+1;j++)ans=min(ans,b[n]-a[1]+vx[i]+vy[j]+sum[j-1]-sum[i]);

	fill(sum.begin(),sum.end(),0),fill(vx.begin(),vx.end(),0),fill(vy.begin(),vy.end(),0);
	for(int i=1;i<=n;i++)sum[i]=sum[i-1]+b[i]-a[i];
	for(int i=1;i<=n;i++)sum[i]*=2;
	for(int i=1;i<=n;i++)vx[i]=b[i]-a[1]+abs(a[1]-c[1]),vy[i]=b[n]-a[i]+abs(b[n]-d[m]);
	if(a[1]<c[1])vx[0]=1e14;
	if(b[n]>d[m])vy[n+1]=1e14;
	mn=vx[0]-sum[0];
	for(int i=1;i<=n+1;i++){
		ans=min(ans,d[m]-c[1]+vy[i]+sum[i-1]+mn);
		mn=min(mn,vx[i]-sum[i]);
	}
	// for(int i=0;i<=n;i++)for(int j=i+1;j<=n+1;j++)ans=min(ans,d[m]-c[1]+vx[i]+vy[j]+sum[j-1]-sum[i]);

	cout<<ans<<endl;

	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3900kb

input:

100 2
1 100
100 1

output:

198

result:

ok single line: '198'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3676kb

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: 1ms
memory: 3736kb

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: 1ms
memory: 3676kb

input:

100 3
2 99
3 100
2 1

output:

100

result:

ok single line: '100'

Test #5:

score: 0
Accepted
time: 1ms
memory: 3628kb

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: 0ms
memory: 3668kb

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: 3744kb

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: 3680kb

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: 0ms
memory: 3912kb

input:

100 4
10 90
40 30
38 32
36 34

output:

100

result:

ok single line: '100'

Test #10:

score: 0
Accepted
time: 1ms
memory: 3908kb

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: 3756kb

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: 3664kb

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: 3736kb

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: 3620kb

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: 0ms
memory: 3728kb

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: 1ms
memory: 3668kb

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: 0ms
memory: 3672kb

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: 3820kb

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: 2ms
memory: 4216kb

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: 0ms
memory: 4268kb

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: 1ms
memory: 3696kb

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: 1ms
memory: 3724kb

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: 1ms
memory: 4036kb

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: 1ms
memory: 4116kb

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: 3732kb

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: 1ms
memory: 3632kb

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: 0ms
memory: 3656kb

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: 3660kb

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: 1ms
memory: 3624kb

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: 3668kb

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: 3696kb

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: 3672kb

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: 1ms
memory: 3632kb

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: 1ms
memory: 3616kb

input:

100 3
11 50
50 49
36 35

output:

42

result:

ok single line: '42'

Test #35:

score: 0
Accepted
time: 1ms
memory: 3920kb

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: 3652kb

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: 0ms
memory: 3680kb

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: 3744kb

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: 0ms
memory: 3952kb

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: 1ms
memory: 3692kb

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: 3896kb

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: 1ms
memory: 3896kb

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: 3668kb

input:

1150 5
100 900
1068 1064
1089 1088
1128 1110
1150 1149

output:

1097

result:

ok single line: '1097'