QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#101672#6178. 区间子集问题themoon100 ✓56ms43968kbC++144.0kb2023-04-30 18:19:372023-04-30 18:19:40

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-30 18:19:40]
  • 评测
  • 测评结果:100
  • 用时:56ms
  • 内存:43968kb
  • [2023-04-30 18:19:37]
  • 提交

answer

#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
const int N=5005;
const int Z=2;
const int INF=1e9;
int n,ll[N],rr[N];
int fa[N];
vector<int>son[N];
inline int Read(){
	char ch;
	int f=1;
	while((ch=getchar())<'0'||ch>'9')
		if(ch=='-') f=-1;
	int x=ch^48;
	while((ch=getchar())>='0'&&ch<='9')
		x=(x<<3)+(x<<1)+(ch^48);
	return x*f;
}
inline void print(int x){
	if(x>=10) print(x/10);
	putchar(x%10+48);
	return ;
}
inline void Print(int x,char ch='\n'){
	if(x<0){
		putchar('-');
		print(-x);
	}
	else print(x);
	putchar(ch);
	return ;
}
inline bool Cmp(int x,int y){
	return ll[x]<ll[y];
}
inline void Init(){
	n=Read();
	for(int i=1;i<=n;i++){
		ll[i]=Read();
		rr[i]=Read();
	}
	ll[0]=-1,rr[0]=INF+1;
	for(int i=1;i<=n;i++){
		fa[i]=0;
		for(int j=1;j<=n;j++){
			if(ll[j]>=ll[i]) continue ;
			if(rr[j]<=rr[i]) continue ;
			if(rr[j]-ll[j]<rr[fa[i]]-ll[fa[i]]) fa[i]=j;
		}
		if(fa[i]) son[fa[i]].push_back(i);
	}
	for(int i=1;i<=n;i++)
		sort(son[i].begin(),son[i].end(),Cmp);
	return ;
}
inline int Max(int x,int y){
	return x>y?x:y;
}
inline int Min(int x,int y){
	return x<y?x:y;
}
inline void Cmax(int&x,int y){
	if(y>x) x=y;
	return ;
}
inline void Cmin(int&x,int y){
	if(y<x) x=y;
	return ;
}
int f[N][N][Z][Z];
int mx[N];
inline void Dfs(int u){
	if(!son[u].size()){
		mx[u]=0;
		for(int i=0;i<=1;i++)
			for(int j=0;j<=1;j++)
				f[u][0][i][j]=-INF;
		f[u][0][0][0]=rr[u]-ll[u];
		return ;
	}
	for(int i=0;i<son[u].size();i++){
		int v=son[u][i];
		Dfs(v);
	}
	mx[u]=1;
	for(int i=0;i<2;i++)
		for(int j=0;j<2;j++)
			f[u][0][i][j]=f[u][1][i][j]=-INF;
	f[u][0][0][0]=0;
	f[u][1][1][1]=ll[son[u].front()]-ll[u];
	for(int ii=0;ii<son[u].size();ii++){
		int v=son[u][ii];
		for(int i=0;i<=mx[u]+mx[v]+1&&i<=n;i++)
			for(int ul=0;ul<=1;ul++)
				for(int ur=0;ur<=1;ur++)
					f[0][i][ul][ur]=-INF;
		for(int i=0;i<=mx[u];i++)
			for(int ul=0;ul<=1;ul++)
				for(int ur=0;ur<=1;ur++){
					if(f[u][i][ul][ur]<0) continue ;
					for(int j=0;j<=mx[v]&&i+j-1<=n;j++)
						for(int vl=0;vl<=1;vl++)
							for(int vr=0;vr<=1;vr++){
								if(f[v][j][vl][vr]<0) continue ;
								Cmax(f[0][i+j-(ur&&vl)][ul][0],f[u][i][ul][ur]+f[v][j][vl][vr]);
								int len;
								if(ii==son[u].size()-1) len=rr[u]-rr[v];
								else len=ll[son[u][ii+1]]-rr[v];
								Cmax(f[0][i+j-(ur&&vl)+(!vr)][ul][1],f[u][i][ul][ur]+f[v][j][vl][vr]+len);
							}
				}
		mx[u]+=mx[v]+1;
		if(mx[u]>n) mx[u]=n;
		for(int i=0;i<=mx[u];i++)
			for(int ul=0;ul<=1;ul++)
				for(int ur=0;ur<=1;ur++)
					f[u][i][ul][ur]=f[0][i][ul][ur];
	}
	/*for(int i=0;i<=mx[u];i++)
		for(int ul=0;ul<=1;ul++)
			for(int ur=0;ur<=1;ur++)
				if(f[u][i][ul][ur]>=0)
					printf("before u=%d i=%d ul=%d ur=%d %d\n",u,i,ul,ur,f[u][i][ul][ur]);*/
	for(int i=0;i<mx[u];i++){
		for(int ul=0;ul<=1;ul++)
			for(int ur=0;ur<=1;ur++)
				f[u][i][ul][ur]=-INF;
		f[u][i][0][0]=Max(f[u][i+1][0][0],Max(f[u][i+1][0][1],f[u][i+1][1][0]));
		if(i>=1) f[u][i][0][1]=Max(f[u][i+1][0][1],f[u][i+1][1][1]);
		if(i>=1) f[u][i][1][0]=Max(f[u][i+1][1][0],f[u][i+1][1][1]);
		if(i>=2) f[u][i][1][1]=f[u][i+1][1][1];
	}
	mx[u]--;
	return ;
}
int ans;
inline bool Solve1(){
	for(int i=1;i<n;i++)
		if(ll[i]>=rr[i+1]||rr[i]<=rr[i+1]) return 0;
	ans=rr[1]-ll[1]-Min(ll[2]-ll[1],rr[1]-rr[2]);
	Print(ans);
	return 1;
}
inline void Solve(){
	if(n>400&&Solve1()) return ;
	for(int i=1;i<=n;i++)
		if(!fa[i]){
			Dfs(i);
			ans+=f[i][0][0][0];
		}
	/*for(int u=1;u<=n;u++)
		for(int i=0;i<=mx[u];i++)
			for(int ul=0;ul<=1;ul++)
				for(int ur=0;ur<=1;ur++)
					if(f[u][i][ul][ur]>=0)
						printf("u=%d i=%d ul=%d ur=%d %d\n",u,i,ul,ur,f[u][i][ul][ur]);*/
	return Print(ans);
}
#include<ctime>
int main(){
	//freopen("segment.in","r",stdin);
	//freopen("segment.out","w",stdout);
	//#define LOCAL
	#ifdef LOCAL
	int st=clock();
	#endif
	Init();
	Solve();
	#ifdef LOCAL
	int en=clock();
	printf("cost %d ms\n",en-st);
	#endif
	return 0;
}

詳細信息

Test #1:

score: 5
Accepted
time: 1ms
memory: 5044kb

input:

9
31 62
97 98
53 59
17 79
3 87
40 60
72 77
22 29
7 11

output:

69

result:

ok 1 number(s): "69"

Test #2:

score: 5
Accepted
time: 0ms
memory: 4944kb

input:

10
77 78
43 49
60 75
11 32
81 94
64 67
17 19
52 53
40 76
46 48

output:

57

result:

ok 1 number(s): "57"

Test #3:

score: 5
Accepted
time: 1ms
memory: 7028kb

input:

69
2356 2546
7498 7527
2114 2172
2866 4692
6101 6104
2607 2662
619 949
5495 5513
8013 8166
2704 2765
306 567
1192 1292
5241 6149
7468 7556
9455 9480
1926 1965
427 542
3033 3087
2218 2234
9423 9485
6312 6584
65 2857
1165 1341
4247 4615
7068 7241
8853 8971
9049 9061
7621 7747
6920 6925
2975 3405
8874 ...

output:

8167

result:

ok 1 number(s): "8167"

Test #4:

score: 5
Accepted
time: 1ms
memory: 7824kb

input:

226
907689292 910731765
637315418 640270766
649957861 656645930
330108609 332693231
327790388 341052210
452058987 452711364
164678004 227157125
701219833 702883478
35857149 36022443
652977298 655883456
200127263 204937919
873141333 894247545
964266670 986217229
168568045 174180280
743434879 76941787...

output:

848026569

result:

ok 1 number(s): "848026569"

Test #5:

score: 5
Accepted
time: 5ms
memory: 6892kb

input:

476
714654653 727563662
429167515 431143191
842243203 848961893
700045985 702768740
342430157 343401134
573738448 575113786
646109543 646481599
129101080 130854863
819944089 827765544
487460829 488975983
75470140 76077173
996513423 999545857
828006214 829269130
313030891 314257417
2556612 72746358
8...

output:

841569322

result:

ok 1 number(s): "841569322"

Test #6:

score: 5
Accepted
time: 3ms
memory: 7804kb

input:

244
195504221 200903817
183982018 184267455
886052909 887397280
774138753 778393959
885144456 885660700
773293500 780988242
480097924 558064110
207189240 208497911
802453825 812658707
706209219 722799069
688092845 691466627
285159759 290428263
482152317 482429364
359239303 365087642
982083965 983237...

output:

795087927

result:

ok 1 number(s): "795087927"

Test #7:

score: 5
Accepted
time: 0ms
memory: 5124kb

input:

61
4857 4890
2474 2581
7705 7729
2151 2212
3831 4045
675 680
6791 7772
132 1181
5859 6029
1861 1948
6183 6258
5673 5708
9728 9892
5025 5451
8361 8431
1200 2605
5033 5221
5016 5503
9102 9122
6453 6475
6286 6307
3645 3828
7285 7658
3198 3203
6692 8039
9948 9974
9136 9140
7866 7970
784 888
4761 4836
13...

output:

6957

result:

ok 1 number(s): "6957"

Test #8:

score: 5
Accepted
time: 2ms
memory: 5072kb

input:

48
4735 4834
5477 5835
3971 4180
907 1845
5552 5819
410 1872
9909 9983
1116 1449
6544 7083
1368 1433
5077 5143
122 8492
6059 6129
1481 1580
3423 3492
3918 4391
7724 7754
7174 7352
6735 6767
8592 9365
86 96
3356 3419
4441 4499
331 2433
926 1069
9453 9761
2004 2122
8523 9817
7902 8314
9834 9843
2860 3...

output:

7585

result:

ok 1 number(s): "7585"

Test #9:

score: 5
Accepted
time: 3ms
memory: 2860kb

input:

2000
999996000 999999999
999996001 999999998
999996002 999999997
999996003 999999996
999996004 999999995
999996005 999999994
999996006 999999993
999996007 999999992
999996008 999999991
999996009 999999990
999996010 999999989
999996011 999999988
999996012 999999987
999996013 999999986
999996014 99999...

output:

3998

result:

ok 1 number(s): "3998"

Test #10:

score: 5
Accepted
time: 9ms
memory: 12932kb

input:

2000
999996000 999996001
999996002 999996003
999996004 999996005
999996006 999996007
999996008 999996009
999996010 999996011
999996012 999996013
999996014 999996015
999996016 999996017
999996018 999996019
999996020 999996021
999996022 999996023
999996024 999996025
999996026 999996027
999996028 99999...

output:

2000

result:

ok 1 number(s): "2000"

Test #11:

score: 5
Accepted
time: 26ms
memory: 12928kb

input:

2000
186292029 186381429
39531755 39997885
715829714 715943849
89593082 89975740
501618789 502142452
524895573 525177152
239867002 239989666
90634704 90840196
347118449 347622111
937915910 938117133
37592276 37754952
148576316 148874708
9347683 9627715
946069019 946148305
192522660 192836061
1006072...

output:

518221954

result:

ok 1 number(s): "518221954"

Test #12:

score: 5
Accepted
time: 48ms
memory: 43968kb

input:

2000
26277805 971703271
441822414 547285226
22630689 976323424
177503973 838783634
359289289 622892772
336566267 653471488
468554386 471234224
211952703 802002437
275616745 718960728
488074800 488251685
270761751 724659408
98076671 917014793
188142828 828559658
33718424 968949516
414639447 570469725...

output:

999481159

result:

ok 1 number(s): "999481159"

Test #13:

score: 5
Accepted
time: 52ms
memory: 36452kb

input:

2000
106612522 904126667
454199953 459183353
518266011 521309001
100152739 912058268
118630668 891166021
73939844 935218444
184402560 818639601
275980248 281176406
640956183 643120656
576495602 577327144
302302907 306330636
46420140 957275508
86493782 923649549
190816371 816086677
59971696 946574467...

output:

999768008

result:

ok 1 number(s): "999768008"

Test #14:

score: 5
Accepted
time: 27ms
memory: 20912kb

input:

2000
834317527 837483207
871389010 877611186
251418042 251768616
692807140 695715240
980698227 980757849
860246405 869437586
315175804 318104161
297132323 297762460
697999996 698512693
921473187 943059724
172530643 173152426
29249487 29496853
668322217 668577462
409724653 410213113
662520150 6631430...

output:

834266055

result:

ok 1 number(s): "834266055"

Test #15:

score: 5
Accepted
time: 52ms
memory: 36628kb

input:

2000
22260436 983513300
44838411 959813215
178325390 819236924
191801218 805837790
686893042 687006983
366369393 366889609
588266579 629664794
717449062 718633671
448280421 449080467
434924925 446983549
488707390 489188828
242289139 746330221
47463138 957512142
7739035 994947857
489705763 492780357
...

output:

998764362

result:

ok 1 number(s): "998764362"

Test #16:

score: 5
Accepted
time: 46ms
memory: 40284kb

input:

2000
93109583 914905690
321520881 324064593
293243664 715774698
82783636 927527844
265688192 739625011
32757327 974206351
237459822 769556241
655287073 655726696
202305001 801303875
383923155 383928230
259669745 745121842
107036497 902898650
302132562 704877392
151976811 853539335
626692562 62765552...

output:

999353069

result:

ok 1 number(s): "999353069"

Test #17:

score: 5
Accepted
time: 11ms
memory: 13016kb

input:

2000
490101141 491331623
493416297 493544197
250031938 255484592
169345113 170934038
358634879 359110770
275923033 276381509
816955174 817466221
868403211 868930384
429367760 429946920
966828782 966998727
275548906 279511969
718471635 718949435
823236205 823940122
475357447 476832667
106562982 10828...

output:

771318558

result:

ok 1 number(s): "771318558"

Test #18:

score: 5
Accepted
time: 56ms
memory: 35900kb

input:

2000
400419022 400515434
311805468 312563722
513848101 514429488
557006776 557077877
53675155 954215429
70688309 933142221
116626359 884974190
728863542 728996879
462652370 463341088
57964317 948157377
279739587 279927638
70204877 935018658
564513118 564686325
182175743 813676272
70731735 932948734
...

output:

999760084

result:

ok 1 number(s): "999760084"

Test #19:

score: 5
Accepted
time: 52ms
memory: 38208kb

input:

2000
409036366 416844473
618790087 619213533
7403331 994487672
163952573 829019701
215041441 781602949
217762165 779732629
43881986 953602305
684439518 687102112
240342797 761725553
78486686 918019439
200034282 795877377
253486418 743160520
673597366 673693533
272648301 723654511
55221277 944002628
...

output:

999494881

result:

ok 1 number(s): "999494881"

Test #20:

score: 5
Accepted
time: 47ms
memory: 38932kb

input:

2000
301098910 301463273
172899058 822678193
303061223 304250391
455852994 457327795
342099842 351024966
353859985 360223624
80309261 916568803
100842570 899492109
62985101 937484158
183972063 807953314
665766799 665922782
379373982 379758598
145991794 855328796
653383586 691457465
436578769 4385278...

output:

998998410

result:

ok 1 number(s): "998998410"