I Max Answer
考虑每一个点作为区间最小值扩展。
单调队列找到该区间左右端点。
线段树或rmq询问左右半区间的前缀和最大最小值
线段树
#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N =5e5+10;
int l[N],r[N];
ll ans[N],nu[N];
struct node {
int l,r;
ll mx,mn;
}tree[N<<2];
void push_up(int cur){
tree[cur].mx = max(tree[cur<<1].mx,tree[cur<<1|1].mx);
tree[cur].mn = min(tree[cur<<1].mn,tree[cur<<1|1].mn);
}
void build(int l,int r,int cur){
tree[cur].l=l;
tree[cur].r=r;
if(l==r){
tree[cur].mx = tree[cur].mn = ans[l];
return ;
}
int mid = (l+r)>>1;
build(l,mid,cur<<1);
build(mid+1,r,cur<<1|1);
push_up(cur);
}
ll querymax(int l,int r,int cur){
if(l<=tree[cur].l&&r>=tree[cur].r)return tree[cur].mx;
ll res = -1e18;
if(l<=tree[cur<<1].r) res = max(res,querymax(l,r,cur<<1));
if(r>=tree[cur<<1|1].l) res = max(res,querymax(l,r,cur<<1|1));
return res;
}
ll querymin(int l,int r,int cur){
if(l<=tree[cur].l&&r>=tree[cur].r)return tree[cur].mn;
ll res = 1e18;
if(l<=tree[cur<<1].r) res = min(res,querymin(l,r,cur<<1));
if(r>=tree[cur<<1|1].l) res = min(res,querymin(l,r,cur<<1|1));
return res;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lld",&nu[i]);
ans[i]=ans[i-1]+nu[i];
}
stack<int>s;
for(int i=1;i<=n;i++){
while(!s.empty()&&nu[s.top()]>=nu[i])s.pop();
if(s.empty())l[i]=0;
else l[i]=s.top();
s.push(i);
}
while(!s.empty())s.pop();
for(int i=n;i>=1;i--){
while(!s.empty()&&nu[s.top()]>=nu[i])s.pop();
if(s.empty())r[i]=n;
else r[i] = s.top()-1;
s.push(i);
}
build(0,n,1);
ll cnt;
ll mx = -1e18;
for(int i=1;i<=n;i++){
cnt = nu[i];
if(nu[i]>0) cnt = cnt*(querymax(i,r[i],1)-querymin(l[i],i-1,1));
else cnt = cnt*(querymin(i,r[i],1)-querymax(l[i],i-1,1));
mx = max(mx,cnt);
}
printf("%lld\n",mx);
return 0;
}
rmq
#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N =5e5+10;
int l[N],r[N];
ll ans[N],nu[N];
ll mx[N][20],mn[N][20];
void getbestarry(int n){
int tot = (int)(log2((double)n));
for(int i=0;i<=n;i++)
mx[i][0] = mn[i][0] = ans[i];
for(int j=1;j<=tot;j++)
for(int i=0;i+(1<<j)-1<=n;i++){
mx[i][j] = max(mx[i][j-1],mx[i+(1<<(j-1))][j-1]);
mn[i][j] = min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
}
}
ll query(int l,int r,bool getmax){
int k = log2(r+1-l);
if(getmax)
return max(mx[l][k],mx[r-(1<<k)+1][k]);
else
return min(mn[l][k],mn[r-(1<<k)+1][k]);
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lld",&nu[i]);
ans[i]=ans[i-1]+nu[i];
}
stack<int>s;
for(int i=1;i<=n;i++){
while(!s.empty()&&nu[s.top()]>=nu[i])s.pop();
if(s.empty())l[i]=0;
else l[i]=s.top();
s.push(i);
}
while(!s.empty())s.pop();
for(int i=n;i>=1;i--){
while(!s.empty()&&nu[s.top()]>=nu[i])s.pop();
if(s.empty())r[i]=n;
else r[i] = s.top()-1;
s.push(i);
}
//for(int i=1;i<=n;i++)printf("%lld ",ans[i]);
//puts("");
getbestarry(n);
ll cnt;
ll mx = -1e18;
for(int i=1;i<=n;i++){
//cout << l[i] << " " << r[i] << endl;
if(nu[i]>=0){
mx = max(mx,nu[i]*(ans[r[i]]-ans[l[i]]));
//cout << query(i,r[i],true) << " " << query(l[i],i,false) << endl;
}
else{
mx = max(mx,(ll)nu[i]*(query(i,r[i],false)-query(l[i],i-1,true)));
//cout << query(i,r[i],false) << " " << query(l[i],i,true) << endl;
}
}
printf("%lld\n",mx);
return 0;
}