UVALive3938 "Ray, Pass me the dishes!" 线段树

求一个区间的最大子串和的起点与中点 要求起点尽量小,当起点都一样的时候终点尽量小

很常规的一题

每个节点维护3个值

最大前缀和 最大后缀和 与 最大子串和

之前做过不少类似的题目。处理方法一样,


Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

[]   [Go Back]   [Status]  

Description

Download as PDF

After doing Ray a great favor to collect sticks for Ray, Poor Neal becomes very hungry. In return for Neal's help, Ray makes a great dinner for Neal. When it is time for dinner, Ray arranges all the dishes he makes in a single line (actually this line is very long ... , the dishes are represented by 1, 2, 3 ... ). ``You make me work hard and don't pay me! You refuse to teach me Latin Dance! Now it is time for you to serve me", Neal says to himself.

Every dish has its own value represented by an integer whose absolute value is less than 1,000,000,000. Before having dinner, Neal is wondering about the total value of the dishes he will eat. So he raises many questions about the values of dishes he would have.

For each question Neal asks, he will first write down an interval [ab] (inclusive) to represent all the dishes aa + 1,..., b , wherea and b are positive integers, and then asks Ray which sequence of consecutive dishes in the interval has the most total value. Now Ray needs your help.

Input

The input file contains multiple test cases. For each test case, there are two integers n and m in the first line (nm < 500000) . nis the number of dishes and m is the number of questions Neal asks.

Then n numbers come in the second line, which are the values of the dishes from left to right. Next m lines are the questions and each line contains two numbers a , b as described above. Proceed to the end of the input file.

Output

For each test case, output m lines. Each line contains two numbers, indicating the beginning position and end position of the sequence. If there are multiple solutions, output the one with the smallest beginning position. If there are still multiple solutions then, just output the one with the smallest end position. Please output the result as in the Sample Output.

Sample Input

3 1 
1 2 3 
1 1

Sample Output

Case 1: 
1 1

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>

using namespace std;

#define lson l,m,root<<1
#define rson m+1,r,root<<1|1
#define MAXN 500005
typedef long long LL;
int n,m;
LL sum[MAXN<<2];
LL sub[MAXN<<2],pre[MAXN<<2],suf[MAXN<<2];
int st[MAXN<<2],ed[MAXN<<2];
int fst[MAXN<<2],fed[MAXN<<2];
int sst[MAXN<<2],sed[MAXN<<2];

void push_up(int root){
    int l=root<<1;
    int r=root<<1|1;
    sum[root]=sum[l]+sum[r];
    //prefix
    LL maxs=pre[l];
    int ss=fst[l],ee=fed[l];
    if(pre[l]<sum[l]+pre[r])
        maxs=sum[l]+pre[r],ss=fst[l],ee=fed[r];
    pre[root]=maxs,fst[root]=ss,fed[root]=ee;
    //suffix
    maxs=suf[r],ss=sst[r],ee=sed[r];
    if(suf[r]<=sum[r]+suf[l])
        maxs=sum[r]+suf[l],ss=sst[l],ee=sed[r];
    suf[root]=maxs,sst[root]=ss,sed[root]=ee;
    //sub
    maxs=sub[l],ss=st[l],ee=ed[l];
    if(maxs<suf[l]+pre[r])
        maxs=suf[l]+pre[r],ss=sst[l],ee=fed[r];
    if(maxs<sub[r])
        maxs=sub[r],ss=st[r],ee=ed[r];
    sub[root]=maxs,st[root]=ss,ed[root]=ee;
}

void build(int l,int r,int root){
    if(l==r){
        scanf("%lld",&sum[root]);
        sub[root]=pre[root]=suf[root]=sum[root];
        st[root]=ed[root]=l;
        fst[root]=fed[root]=l;
        sst[root]=sed[root]=l;
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    push_up(root);
}

struct node{
    LL sum;
    LL l,m,r;
    int ls,le;
    int ms,me;
    int rs,re;
};

node query(int l,int r,int root,int L,int R){
    node tp;
    if(l>=L && r<=R){
        node tp;
        tp.l=pre[root];tp.m=sub[root];tp.r=suf[root];
        tp.ls=fst[root],tp.le=fed[root];
        tp.ms=st[root],tp.me=ed[root];
        tp.rs=sst[root],tp.re=sed[root];
        tp.sum=sum[root];
        return tp;
    }
    int m=(l+r)>>1;
    node tp1,tp2;
    int ll=0,rr=0;
    if(L<=m){
        tp1=query(lson,L,R);
        ll=1;
    }
    if(R>m){
        tp2=query(rson,L,R);
        rr=1;
    }
    if(ll && !rr) tp=tp1;
    if(!ll && rr) tp=tp2;
    if(ll && rr){
        //prefix
        tp.l=tp1.l,tp.ls=tp1.ls,tp.le=tp1.le;
        if(tp.l<tp1.sum+tp2.l)
            tp.l=tp1.sum+tp2.l,tp.ls=tp1.ls,tp.le=tp2.le;
        //suffix
        tp.r=tp2.r,tp.rs=tp2.rs,tp.re=tp2.re;
        if(tp.r<=tp2.sum+tp1.r)
            tp.r=tp2.sum+tp1.r,tp.rs=tp1.rs,tp.re=tp2.re;
        //sub
        tp.m=tp1.m,tp.ms=tp1.ms,tp.me=tp1.me;
        if(tp.m<tp1.r+tp2.l)
            tp.m=tp1.r+tp2.l,tp.ms=tp1.rs,tp.me=tp2.le;
        if(tp.m<tp2.m)
            tp.m=tp2.m,tp.ms=tp2.ms,tp.me=tp2.me;
        tp.sum=tp1.sum+tp2.sum;
    }

    return tp;
}

int main(){
    int cs=1;
    while(~scanf("%d%d",&n,&m)){
        build(1,n,1);
        printf("Case %d:\n",cs++);
        for(int i=1;i<=m;i++){
            int a,b;
            scanf("%d%d",&a,&b);
            node ans=query(1,n,1,a,b);
            printf("%d %d\n",ans.ms,ans.me);
        }
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值