UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#196665#2834. 石子游戏wosile100136ms3164kbC++1.1kb2023-10-29 11:49:112023-10-29 12:03:23

answer

#include<bits/stdc++.h>
using namespace std;
int n;
int a[100005];
typedef long long ll;
ll pre[100005];
int st[100005],top=0;
int l[100005],r[100005];
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)pre[i]=pre[i-1]+a[i];
	for(int i=1;i<=n;i++){
		while(top>0 && a[i]>a[st[top]]){
			r[st[top]]=i-1;
			top--;
		}
		st[++top]=i;
//		printf("add %d\n",i);
	}
	while(top){
		r[st[top]]=n;
//		printf("%d\n",st[top]);
		top--;
	}
	for(int i=n;i>=1;i--){
		while(top>0 && a[i]>=a[st[top]]){
			l[st[top]]=i+1;
			top--;
		}
		st[++top]=i;
	}
	while(top){
		l[st[top]]=1;
		top--;
	}
	ll ans=0;
	for(int i=1;i<=n;i++){
//		printf("%d %d\n",l[i],r[i]);
		if(i-l[i]<r[i]-i){
			for(int j=l[i];j<=i;j++){
				int pos=lower_bound(pre,pre+n+1,a[i]+a[i]+pre[j-1])-pre;
				ans+=max(0,r[i]-max(pos,i)+1);
			}
		}
		else{
			for(int j=i;j<=r[i];j++){
				int pos=upper_bound(pre,pre+n+1,pre[j]-a[i]-a[i])-pre;
				ans+=max(0,min(pos,i)-l[i]+1);
			}
		}
	}
	printf("%lld",ans);
	return 0;
	//quod erat demonstrandum
}

Details

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

Test #1:

score: 10
Accepted
time: 0ms
memory: 1212kb

input:

40
16534 56599 82848 69398 128 322 987 72580 7303 87294 8729 71398 3883 52419 69949 8856 15302 19683...

output:

706

result:

ok single line: '706'

Test #2:

score: 10
Accepted
time: 0ms
memory: 1212kb

input:

100
9636470 11961537 42426935 70510054 13475835 962720513 17269836 21264966 428685834 227916218 7264...

output:

4729

result:

ok single line: '4729'

Test #3:

score: 10
Accepted
time: 0ms
memory: 1240kb

input:

2000
163106 170 32791 666981 1063 1630 1661 502304 907902 123255 1770 2166 2257 2668 2757 2832 42579...

output:

1994754

result:

ok single line: '1994754'

Test #4:

score: 10
Accepted
time: 2ms
memory: 1300kb

input:

5000
89953 480577359 387680543 941415882 104402 155924 996824972 281971 476407 930588652 389767630 4...

output:

12484365

result:

ok single line: '12484365'

Test #5:

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

input:

50000
7 2 10 6 2 10 8 1 5 4 10 1 10 10 10 8 10 10 10 5 10 8 10 10 10 10 3 10 10 10 2 5 2 10 10 9 10 ...

output:

1249933514

result:

ok single line: '1249933514'

Test #6:

score: 10
Accepted
time: 23ms
memory: 3164kb

input:

100000
1 1 9 3 1 1 1 7 1 2 1 1 1 2 8 9 1 1 6 5 1 8 1 4 1 1 5 1 1 8 3 7 7 1 9 7 1 2 1 1 1 2 1 1 5 1 9...

output:

4999819234

result:

ok single line: '4999819234'

Test #7:

score: 10
Accepted
time: 7ms
memory: 1596kb

input:

20000
5950266 6148366 1086 1091 3149443 1157 1167 1227 1483 2372 9142417 7994692 2895 3005 2676230 2...

output:

199946808

result:

ok single line: '199946808'

Test #8:

score: 10
Accepted
time: 18ms
memory: 2180kb

input:

50000
949 587 952 607 985 1791 110 1540 1 1 1 1 1 1 1 896 553 254 1014 1527 1 1 1 1 1 2032 1 1 167 1...

output:

1249878255

result:

ok single line: '1249878255'

Test #9:

score: 10
Accepted
time: 35ms
memory: 3144kb

input:

100000
96566884 999988003 60756383 999976003 999958564 368629136 328137719 999955547 999951864 30014...

output:

4999819418

result:

ok single line: '4999819418'

Test #10:

score: 10
Accepted
time: 40ms
memory: 3140kb

input:

98765
23333317 11799045 23332926 1964802 20290386 19096936 23332865 23332492 3102961 11794159 233324...

output:

4877090544

result:

ok single line: '4877090544'

Extra Test:

score: 0
Extra Test Passed