UVALive 3938 一道被我WA了的线段树

题目点着
题意是一段区间,q次询问一段区间最大连续字段和
看了眼白书,每段最大连续子段和为
左子树的最大子段,右子树的最大子段,或横跨左右的最大子段
这三个里面最大的
每个节点维护3个值,最大前缀子段(L开头),最大后缀子段(R结尾),最大子段

WAWAWAWAWAWAWA。。。。。。
代码能力捉急,先存着,等到海枯石烂那天在回头看

#include<cstdio>//cww=2016.3.6
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef pair<int,int> pa;
const int N=1000100;
int QL,QR;
LL s[N];
LL sum(int L,int R){return s[R]-s[L-1];}
LL sum(pa x){return sum(x.first,x.second);}
pa better(pa a,pa b){
    if (sum(a)!=sum(b))return sum(a)>sum(b)?a:b;
    return a<b?a:b;
}

struct tree{
    int pre[N],suf[N];pa sub[N];

    void build(int t,int L,int R){
        if (L==R){
            pre[t]=suf[t]=L;
            sub[t]=make_pair(L,L);
            return ;
        }
        int lc=t<<1,rc=lc+1,mid=L+(R-L)/2;
        build(lc,L,mid);                     //子树 
        build(rc,mid+1,R);
        LL x=sum(L,pre[lc]);//pre
        LLy=sum(L,pre[rc]);
        if (x==y)pre[t]=min(pre[lc],pre[rc]);
        else pre[t]=x>y?pre[lc]:pre[rc];
        x=sum(suf[lc],R);//suf
        y=sum(suf[rc],R);
        if (x==y)suf[t]=min(suf[lc],suf[rc]);
        else suf[t]=x>y?suf[lc]:suf[rc];
        sub[t]=better(sub[lc],sub[rc]);      //sub
        sub[t]=better(sub[t],make_pair(suf[lc],pre[rc]));
    }

    pa querypre(int t,int L,int R){
        if (pre[t]<=QR)return make_pair(L,pre[t]);
        int mid=L+(R-L)/2,lc=t<<1,rc=lc+1;
        if (QR<=mid)return querypre(lc,L,mid);
        pa x=querypre(rc,mid+1,R);
        x.first=L;
        return better(x,make_pair(L,pre[lc]));
    }

    pa querysuf(int t,int L,int R){
        if (QL<=suf[t])return make_pair(suf[t],R);
        int mid=L+(R-L)/2,lc=t<<1,rc=lc+1;
        if (mid<QL)return querysuf(rc,mid+1,R);
        pa x=querysuf(lc,L,mid);
        x.second=R;
        return better(x,make_pair(suf[rc],R));
    }

    pa query(int t,int L,int R){
        if (QL<=L&&R<=QR)return sub[t];
        int mid=L+(R-L)/2,lc=t<<1,rc=lc+1;
        if (QL<=mid)return query(lc,L,mid);
        if (mid<QR)return query(rc,mid+1,R);
        pa x=querypre(rc,mid+1,R);
        pa y=querysuf(lc,L,mid);
        pa z=better(query(lc,L,mid),query(rc,mid+1,R));
        return better(z,make_pair(y.first,x.second));
    }
}T;
int main(){
    //freopen("fuck.in","r",stdin);
    int cas=0,n,x,q;
    while (scanf("%d%d",&n,&q)==2){
        s[0]=0;
        for (int i=1;i<=n;i++){
            scanf("%d",&x);
            s[i]=s[i-1]+x;
        }
        T.build(1,1,n);
        printf("Case %d:\n", ++cas);
        while (q--){
            int L,R;scanf("%d%d",&L,&R);
            QL=L;QR=R;
            pa ans=T.query(1,1,n);
            printf("%d %d\n",ans.first,ans.second);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值