codeforces 855B

题目链接:https://codeforces.com/problemset/problem/855/B

对于这种三段区间式的题目,一般是枚举中间区间
因为选出的三个数的顺序是一定的
所以枚举中间的数,然后在两边的区间取最值即可
关于 long long 的最小值...

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;

const int maxn = 200010;

int n;
ll p,q,r;
ll a[maxn],sta[maxn][22],sti[maxn][22];

ll qry(int l,int r,int type){
	int k = 0;
	while((1<<(k+1))<=(r-l+1)) ++k;
	return type==1?max(sta[l][k],sta[r-(1<<k)+1][k]):min(sti[l][k],sti[r-(1<<k)+1][k]);
}

ll read(){ ll s=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*f; }

int main(){
	n = read(),p = read(),q = read(), r = read();
	
	for(int i=1;i<=n;++i) a[i] = read(), sta[i][0] = sti[i][0] = a[i];
	
	for(int j=1;(1<<j)<=n;++j){
		for(int i=1;i<=n;++i) {
			sta[i][j] = max(sta[i][j-1], sta[i+(1<<j-1)][j-1]);
			sti[i][j] = min(sti[i][j-1], sti[i+(1<<j-1)][j-1]);
		}
	}
	
	ll ans = -3e18;
	for(int i=1;i<=n;++i){ 
		ans = max(ans,q * a[i] + p * qry(1,i,p<0?0:1) + r * qry(i,n,r<0?0:1)); 
	} 
	
	printf("%lld
",ans);

	return 0;
}
原文地址:https://www.cnblogs.com/tuchen/p/13834315.html