Logo Wy Online Judge

WyOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#270#7. 「WyOJ Round 1」归 · 星穗垂野lgvc1005611ms127120kbC++233.2kb2025-04-18 15:09:462025-04-18 20:49:28

answer

#include <bits/stdc++.h>
#define LL long long
int T,N,C,qq,a[200009],b[200009],aa[44],bb[44],la[200009];
struct n_t{
	int l,r,va;
} tp[200009];
LL dp[200009],s[200009];
bool cmp(n_t x,n_t y) {
	if(x.r-x.l!=y.r-y.l) return x.r-x.l<y.r-y.l;
	return x.l<y.l;
}
#define md ((l+r)>>1)
int rt[800009],kk;
struct o_t{
	int ls,rs;LL mi;
} sg[50000009];
inline int nw() {
	++kk;
	sg[kk].ls=sg[kk].rs=0;sg[kk].mi=0x3f3f3f3f3f3f3f3f;
	return kk;
}
void up2(int n,int l,int r,int p,LL v) {
	if(!rt[n]) rt[n]=nw();
	n=rt[n];
	while(l<r) {
		sg[n].mi=std::min(sg[n].mi,v);
		if(p<=md) {
			if(!sg[n].ls) sg[n].ls=nw();
			n=sg[n].ls;r=md;
		} else {
			if(!sg[n].rs) sg[n].rs=nw();
			n=sg[n].rs;l=md+1;			
		}
	}
	sg[n].mi=std::min(sg[n].mi,v);
}
LL qr2(int n,int l,int r,int p) {
	n=rt[n];
	LL as=0x3f3f3f3f3f3f3f3f;
	while(n&&l<r) {
		if(p<=md) {
			n=sg[n].ls;
			r=md;
		} else {
			as=std::min(as,sg[sg[n].ls].mi);
			n=sg[n].rs;
			l=md+1;
		}
	}
	return std::min(as,sg[n].mi);
}
#define ls (n<<1)
#define rs ((n<<1)|1)
void build(int n,int l,int r) {
	rt[n]=0;
	if(l==r) return;
	build(ls,l,md),build(rs,md+1,r);
}
void up(int n,int l,int r,int p,int p2,LL v) {
	up2(n,0,N,p2,v);
	if(l==r) return;
	if(p<=md) up(ls,l,md,p,p2,v);else up(rs,md+1,r,p,p2,v);
}
LL qr(int n,int l,int r,int L,int R,int p2) {
	if(r<L||R<l) return 0x3f3f3f3f3f3f3f3f;
	if(L<=l&&r<=R) {
		return qr2(n,0,N,p2);
	}
	return std::min(qr(ls,l,md,L,R,p2),qr(rs,md+1,r,L,R,p2));
}
signed main(void) {
	sg[0].mi=0x3f3f3f3f3f3f3f3f;
	scanf("%d",&T);
	while(T--) {
		scanf("%d %d",&N,&C);
		int w=0;
		dp[0]=0;
		for(int i=1;i<=N;i++) {
			scanf("%d",&a[i]);
			dp[i]=0;
		}
		for(int i=1;i<=N;i++) {
			scanf("%d",&b[i]);
			s[i]=s[i-1]+b[i];
			la[i]=-1;
		}
		int qq=0,id=0;
		for(int i=1;i<=N;i++) {
			aa[++id]=i;
			bb[id]=0;
			for(int j=1;j<=id;j++) {
				bb[j]=std::__gcd(bb[j],a[i]);
			}
			for(int j=id;j>=2;j--) {
				if(bb[j]==bb[j-1]) {
					for(int k=j-1;k<id;k++) {
						aa[k]=aa[k+1];
						bb[k]=bb[k+1];
					}
					id--;
					j=id+1;
				}
			}
			for(int j=1;j<=id;j++) {
				if(bb[j]<=i-aa[j]+1) {
					if(la[aa[j]]!=bb[j]) {
						tp[++qq]=(n_t){aa[j],i,bb[j]};
						la[aa[j]]=bb[j];
					}
				}
				if(i-bb[j]+1<=aa[j]&&i-bb[j]+1>aa[j-1]) {
					tp[++qq]=(n_t){i-bb[j]+1,i,bb[j]};
					la[i-bb[j]+1]=bb[j];
				}
			}
/*			for(int j=i;j<=N;j++) {
				gg=std::__gcd(gg,a[j]);
				if(gg<=j-i+1) {
					if(la!=gg) {
						tp[++qq]=(n_t){i,j,gg};						
					}
					la=gg;
				}
			}*/
		}
		std::sort(tp+1,tp+qq+1,cmp);
		tp[0]=(n_t){0,0,0};
		for(int i=0;i<=qq;i++) dp[i]=0x3f3f3f3f3f3f3f3f;
		LL ans=0x3f3f3f3f3f3f3f3f;
		dp[0]=0;
		build(1,0,qq);kk=0;
		up(1,0,qq,0,0,0);
		for(int i=1;i<=qq;i++) {
			int l=0,r=i-1,mm;
			while(l<r) {
				mm=(l+r+1)/2;
				if(tp[mm].r-tp[mm].l+1<=tp[i].va) l=mm;else r=mm-1;
			}
			dp[i]=qr(1,0,qq,0,l,tp[i].l-1);
//			for(int j=0;j<=l;j++)  {
//				if(tp[j].r<tp[i].l) {
//					dp[i]=std::min(dp[i],dp[j]);
//				}
//			}
			dp[i]+=C+1ll*tp[i].va*(s[tp[i].r]-s[tp[i].l-1]);
			ans=std::min(ans,dp[i]);
			up(1,0,qq,i,tp[i].r,dp[i]);
		}
		printf("%lld\n",ans);
	}
}

这程序好像有点Bug,我给组数据试试?

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 11ms
memory: 11928kb

input:

1000
10 -759451753
8 10 5 7 5 10 6 10 2 1
5 6 5 4 9 8 5 4 1 3
10 -953033990
4 10 10 8 8 2 1 7 4 1
0 ...

output:

-2278355214
-2859101918
-21349075
-1203387136
-1290642033
-851981042
-648655823
-1699966984
-1493814...

result:

ok 1000 numbers

Test #2:

score: 10
Accepted
time: 825ms
memory: 116324kb

input:

5
94253 6998801
88374 94432 21568 79110 3746 94223 86214 12083 54743 1622 16110 18595 18994 3665 547...

output:

6999125
764376403
718240695
1381649585
885156744

result:

ok 5 number(s): "6999125 764376403 718240695 1381649585 885156744"

Test #3:

score: 10
Accepted
time: 9ms
memory: 11900kb

input:

5
120 -848255662
53358 95098 92852 36846 80052 83050 44071 22632 50005 52339 7339 38495 66618 6337 5...

output:

-11025209476
-3953017107
-5975377615
-2466586019
-5823678268

result:

ok 5 number(s): "-11025209476 -3953017107 -5975377615 -2466586019 -5823678268"

Test #4:

score: 10
Accepted
time: 5ms
memory: 11836kb

input:

5
100 -964452256
94167 36596 60051 82090 55625 56197 94589 29844 14929 68497 81714 90264 66283 96767...

output:

-7714370390
-19569375783
-554464800
-1059581333
-6827008162

result:

ok 5 number(s): "-7714370390 -19569375783 -554464800 -1059581333 -6827008162"

Test #5:

score: 10
Accepted
time: 16ms
memory: 10320kb

input:

5
984 -715414516
65632 15191 55392 53178 88695 20308 44882 16528 12986 81169 76880 2239 81264 13866 ...

output:

-72952473687
-64021627506
-700730085
-994178012
-1624804963

result:

ok 5 number(s): "-72952473687 -64021627506 -700730085 -994178012 -1624804963"

Test #6:

score: 10
Accepted
time: 916ms
memory: 111868kb

input:

5
83593 -175988991
46605 26358 5204 30137 41225 11710 75224 14130 56433 62524 29873 59483 22318 2619...

output:

-1656361289384
-4920133073293
-1028097824
-2809116480
-22668175189

result:

ok 5 number(s): "-1656361289384 -4920133073293 ...097824 -2809116480 -22668175189"

Test #7:

score: 10
Accepted
time: 1033ms
memory: 127120kb

input:

5
96302 -451340376
2672 47391 49737 61648 38099 10679 28316 91168 97182 40556 20560 89630 45931 9187...

output:

-4877361987440
-9327808595024
-261498007
97842263
-37427524204

result:

ok 5 number(s): "-4877361987440 -9327808595024 -261498007 97842263 -37427524204"

Test #8:

score: 10
Accepted
time: 987ms
memory: 114188kb

input:

5
82075 -915749780
41503 94470 21657 60519 10090 65640 80911 29436 63096 68715 79609 87499 26240 606...

output:

-8500967499879
-2162204721519
-803472133
-6968294203
-60694304098

result:

ok 5 number(s): "-8500967499879 -2162204721519 ...472133 -6968294203 -60694304098"

Test #9:

score: 10
Accepted
time: 902ms
memory: 110008kb

input:

5
86206 -158351237
12910 53574 97622 75378 61116 40492 87106 60678 72614 2146 43673 15147 23123 7469...

output:

-1508037508585
-4714642449519
-3202809425
-6778545312
-12974512083

result:

ok 5 number(s): "-1508037508585 -4714642449519 ...809425 -6778545312 -12974512083"

Test #10:

score: 10
Accepted
time: 907ms
memory: 114332kb

input:

5
87683 -236086252
88768 76878 54808 98282 70961 83599 48353 34381 40413 95288 24241 74500 52966 994...

output:

-2319782085288
-10990638538852
-1651187059
-12428157994
-22719490815

result:

ok 5 number(s): "-2319782085288 -10990638538852...87059 -12428157994 -22719490815"

Extra Test:

score: 0
Extra Test Passed