2019南昌ICPC网络赛 I Max answer【单调栈+ST表】

本文介绍了一种使用ST表解决区间最值问题的方法,通过预处理建立ST表,实现快速查询区间最大值和最小值,适用于单调栈无法处理的情况,特别是数据包含负数时。文章提供了一个具体示例,详细解释了算法流程,包括如何利用ST表进行区间查询,以及如何结合单调栈确定区间边界。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Alice has a magic array. She suggests that the value of a interval is equal to the sum of the values in the interval, multiplied by the smallest value in the interval.

Now she is planning to find the max value of the intervals in her array. Can you help her?

Input

First line contains an integer n(1 \le n \le 5 \times 10 ^5n(1≤n≤5×105).

Second line contains nn integers represent the array a (-10^5 \le a_i \le 10^5)a(−105≤ai​≤105).

Output

One line contains an integer represent the answer of the array.

样例输入复制

5
1 2 3 4 5

样例输出复制

36

看到这道题的时候我是乐坏了的,这不是单调栈吗

交了几发WA之后是自闭的,然后发现这题的数据可以是负数

然后就不会处理了、、

补题,需要用到ST表来查找区间内的最值,??

刚学的ST表,涨姿势了

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e17;
const int maxn = 500000 + 10;
int a[maxn];
int st[maxn];
int posl[maxn],posr[maxn];
ll sum[maxn];
ll mx[maxn][32],mi[maxn][32];
ll ans;
int n;
void ST_prework(){
  for(int i = 0; i <= n; i++)  
    mx[i][0] = mi[i][0] = sum[i];
  int  t = log(n) / log(2) + 1;
  for(int j = 1; j < t; j++)
    for(int i = 0; i <= n - (1 << j) + 1; i++){
      mx[i][j] = max(mx[i][j - 1],mx[i + (1 << (j-1))][j - 1]);
      mi[i][j] = min(mi[i][j - 1],mi[i + (1 << (j-1))][j - 1]);
    }
}
ll ST_querymax(int l,int r){
  int k = log(r - l + 1)/log(2);
  return max(mx[l][k],mx[r - (1 << k) + 1][k]);
}
ll ST_querymin(int l,int r){
  int k = log(r - l + 1)/log(2);
  return min(mi[l][k],mi[r - (1 << k) + 1][k]);
}
int main()
{
    scanf("%d",&n);
    for (int i = 1; i <= n; i++){
        scanf("%d",&a[i]);
        sum[i] = sum[i - 1] + a[i];
    }
    ST_prework();

    int top = 0;
    for(int i = 1; i <= n; i++){
      if(top == 0 || a[i] > a[st[top - 1]]){
        st[top++] = i;
        posl[i] = i;
      }
      else{
         while (top >= 1 && a[i] <= a[st[top - 1]]){
           --top;
         }
         posl[i] = posl[st[top]];
         st[top++] = i;
      }
    }
    memset(st,0,sizeof st);
    top = 0;
    for(int i = n; i >= 1; i--){
      if(top == 0 || a[i] > a[st[top - 1]]){
        st[top++] = i;
        posr[i] = i;
      }
      else{
         while (top >= 1 && a[i] <= a[st[top - 1]]){
           --top;
         }
         posr[i] = posr[st[top]];
         st[top++] = i;
      }
    }
    ans = -inf;
    for(int i = 1; i <= n; i++){
      if(a[i] > 0){
        ll l = ST_querymin(posl[i] - 1,i -1);
        ll r = ST_querymax(i,posr[i]);
        ll tmp = (r - l) * a[i];
        ans = max(ans,tmp);
      }
      else {
        ll l = ST_querymax(posl[i] - 1,i - 1);
        ll r = ST_querymin(i,posr[i]);
        ll tmp = (r - l) * a[i];
        ans = max(ans,tmp);
      }
    }
    printf("%lld\n", ans);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值