BZOJ 5089: 最大连续子段和

来源:互联网 发布:荒野求生 知乎 编辑:程序博客网 时间:2024/05/20 13:36

似乎还没有人写题解,所以访问量是不是应该多点?

首先,这题暴力是能过的(滑稽
然后其实看数据范围得算法可知,用分块来搞就好了
维护的是块内最大和,左起最大和,右起最大和,这个东西用凸包维护就好了
而且斜率还是单调的(加的数>0
接着你发现块内重构 是要 size2 的,所以让 size=n1/3 这样复杂度就是 O(n5/3)

本来想看看能不能再快一点的。。结果越改越慢
如果有人知道怎么样更快 请告诉我一声
可能我用结构体有点多?(代码还是挺能看的吧。。

#include<bits/stdc++.h>using namespace std;typedef long long LL;const int N=5e4+3,bk=40;const LL inf=1e15;char B[1<<14],*S=B,*T=B;#define gc (S==T&&(T=(S=B)+fread(B,1,1<<14,stdin),S==T)?-1:*S++)inline int read(){    int x=0,f=1; char ch=gc;    while(ch<'0' || ch>'9'){if(ch=='-')f=-1; ch=gc;}    while(ch>='0' && ch<='9'){x=(x<<1)+(x<<3)+ch-'0'; ch=gc;}    return x*f;}int bl[N],a[N];struct P{double x,y;};double xl(P x,P y){    return (x.y-y.y)/(x.x-y.x);}struct tb{    int tp,o; P s[bk+2];    void ins(P u){        while(tp>1 && xl(u,s[tp-1])>=xl(s[tp],s[tp-1]) )--tp;        s[++tp]=u;    }    void move(int u){        while(o<tp && xl(s[o],s[o+1])>=u)++o;    }};struct block{    int l,r,k; LL num;    tb s[3];    void bulid(){        num=0;        for(int i=l;i<=r;++i) a[i]+=k,num+=a[i];        int n=r-l+1; k=0;        s[0].tp=s[1].tp=s[2].tp=0;        s[0].o =s[1].o =s[2].o=1;        for(int i=1;i<=n;++i){            P u=(P){i,-inf}; LL o=0;            for(int j=1;j<i;++j) o+=a[l+j-1];            for(int j=i;j<=n;++j){                o+=a[l+j-1];                if(j!=i)o-=a[l+j-1-i];                u.y=max(u.y,(double)o);            }            s[0].ins(u);        }        P u=(P){0,0};        for(int i=1;i<=n;++i){            u.y+=a[l+i-1],u.x=i;            s[1].ins(u);        }        u=(P){0,0};        for(int i=n;i;--i){            u.y+=a[l+i-1],u.x=n-i+1;            s[2].ins(u);        }        for(int i=0;i<3;++i) s[i].move(0);    }    void add(int u){        num+=(LL)u*(r-l+1),k+=u;        for(int i=0;i<3;++i) s[i].move(-k);    }    LL get(int u){        P o=s[u].s[ s[u].o ];        return o.y+(LL)o.x*k;    }}b[1302];int main(){    int n=read(),q=read(),i;    for(i=1;i<=n;++i)        a[i]=read(),bl[i]=(i-1)/bk+1,b[bl[i]].r=i;    for(i=1;i<=bl[n];++i)        b[i].k=0,b[i].l=(i-1)*bk+1,b[i].bulid();    while(q--){        char ch=gc; while(ch!='A' && ch!='Q')ch=gc;        if(ch=='A'){            int l=read(),r=read(),x=read();            for(i=l;bl[i]==bl[l] && i<=r;++i) a[i]+=x;            b[bl[l]].bulid();            for(i=bl[l]+1;i<bl[r];++i) b[i].add(x);            if(bl[l]<bl[r]){                for(i=r;bl[i]==bl[r];--i) a[i]+=x;                b[bl[r]].bulid();            }        }        else{            int l=read(),r=read();            LL ans=0,s=0;            for(i=l;bl[i]==bl[l] && i<=r;++i){                s+=a[i]+b[bl[i]].k;                s=s<0?0:s; ans=s>ans?s:ans;            }            for(i=bl[l]+1;i<bl[r];++i){                ans=max(ans,b[i].get(0));                ans=max(ans,s+b[i].get(1));                s=max(s+b[i].num,b[i].get(2));                s=s<0?0:s;            }            if(bl[l]<bl[r])                for(i=b[bl[r]].l;i<=r;++i){                    s+=a[i]+b[bl[r]].k;                    s=s<0?0:s; ans=s>ans?s:ans;                }            printf("%lld\n",ans);        }    }    return 0;}
原创粉丝点击