bzoj5380 Function 单调栈维护凸壳+二分

37 篇文章 0 订阅
10 篇文章 0 订阅

Description


这里写图片描述

Solution


改了一天搞出来了(哭泣脸
翘掉了数据结构讲课,自我感觉良好

一个结论就是,最优答案一定是一条先向左上↖若干步再向上到顶↑的一条路径
考虑枚举从哪一列开始向上到顶,设为i,那么此时对于起点(x,y)答案就是 a[i](xy)+a[i]isum[i]+sum[y] a [ i ] ⋅ ( x − y ) + a [ i ] ⋅ i − s u m [ i ] + s u m [ y ]
我们把y-x看作自变量,将a[i]视为系数,后面的为常数,那么这就变成了许多条直线。我们需要在y-x这个点上找到最小值。如果不太能理解可以把这个看成是分段函数
考虑维护这些直线的凸壳,我们根据斜率和相邻直线交点的单调性做单调栈,然后在凸壳上二分得到一条单调栈内的直线与前后直线的交点组成的区间恰好包含y-x
据某位数据结构大佬说据某位数据结构大佬说可以用李超树

码力不够

Code


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)

typedef long long LL;
const int N=1000005;

struct Q {int x,y,id;} q[N];

int stack[N],top;
LL ans[N],a[N],b[N],sum[N];
double pos[N];

int read() {
    int x=0,v=1; char ch=getchar();
    for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
    for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
    return x*v;
}

double get(int i,int j) {
    return (double) (b[j]-b[i])/(a[i]-a[j]);
}

bool cmp(Q a,Q b) {
    return a.y<b.y;
}

int main(void) {
    int n=read();
    rep(i,1,n) {
        a[i]=read(); sum[i]=sum[i-1]+a[i];
        b[i]=a[i]*i-sum[i];
    }
    int T=read();
    rep(i,1,T) q[i]=(Q) {read(),read(),i};
    std:: sort(q+1,q+T+1,cmp);
    int now=0;
    rep(i,1,T) {
        int d=q[i].x-q[i].y;
        while (now+1<=q[i].y) { now++;
            while (top&&a[stack[top]]>=a[now]) top--;
            while (top>=2&&get(now,stack[top])>get(stack[top],stack[top-1])) top--;
            stack[++top]=now;
            if (top>1) pos[n-top]=get(now,stack[top-1]);
        }
        int tmp=std:: lower_bound(pos+n-top,pos+n-1,d)-pos;
        tmp=n-tmp;
        ans[q[i].id]=d*a[stack[tmp]]+b[stack[tmp]]+sum[now];
    }
    rep(i,1,T) printf("%lld\n", ans[i]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值