UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#196654#2834. 石子游戏snow_trace100152ms4492kbC++111.3kb2023-10-29 10:51:242023-10-29 12:02:59

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n;
int l[300005],r[300005],a[300005],pre[300005];
signed main(){
	cin >> n;
	for(int i= 1;i<=n;i++)cin>> a[i];
	for(int i = 1;i<=n;i++)pre[i] = pre[i-1]+a[i];
	vector<int>s;
	for(int i = 1;i<=n;i++)l[i] =0 ;
	for(int i = 1;i<=n;i++)r[i] =n+1;
	for(int i = 1;i<=n;i++){
		while(!s.empty() and a[s.back()]<=a[i]){
			s.pop_back();
		}if(!s.empty())l[i] = s.back();
		s.push_back(i);
	}s.clear();
	for(int i= n;i>=1;i--){
		while(!s.empty() and a[s.back()]<a[i]){
			s.pop_back();
		}if(!s.empty())r[i] = s.back();
		s.push_back(i);
	}int ans =0 ;
	for(int i = 1;i<=n;i++){
	//	cout << i << endl;
		int len1 = i-l[i],len2 = r[i]-i;
		if(len1<len2){
			for(int j = i;j>l[i];j--){
				if(pre[r[i]-1]-pre[j-1]<2*a[i])continue;
				int ll = i,rr = r[i]-1;
				while(ll<rr){
					int mid = ll+rr>>1;
					if(pre[mid]-pre[j-1]>=2*a[i])rr = mid;
					else ll = mid+1;
				}ans+=r[i]-rr;
			//	cout << i << " " << r[i]-rr << endl;
			}
		}else{
			for(int j = i;j<r[i];j++){
				if(pre[j]-pre[l[i]]<2*a[i])continue;
				int ll = l[i]+1,rr = i;
				while(ll<rr){
					int mid = ll+rr+1>>1;
					if(pre[j]-pre[mid-1]>=2*a[i])ll = mid;
					else rr = mid-1;
				}ans+=ll-l[i];
			//	cout << " " << i << " " << ll-l[i] << endl;
			}
		}
	}cout << ans << endl;
	return 0;
}

Details

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

Test #1:

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

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

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

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

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: 12ms
memory: 2944kb

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: 25ms
memory: 4492kb

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: 5ms
memory: 1892kb

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: 17ms
memory: 2820kb

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: 47ms
memory: 4392kb

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: 43ms
memory: 4360kb

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