The Preliminary Contest for ICPC China Nanchang National Invitational 补题

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值