uva1400 区间最大连续和 线段树

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 [a, b] (inclusive) to represent all the dishes a, a + 1,..., b, where a 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 (n, m < 500000). n is 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

  给出长度为N的整数序列,对于询问(a,b),找到两个下标x,y,a<=x<=y<=b,使x到y区间连续和最大。

  把区间[a,b]分成两半,设mid=(a+b)/2,答案区间可能在[a,mid],[mid+1,b],也可能开始在前一半,结束在后一半。设max_prefix[x]为点x包含区间的最大前缀的右边坐标,max_suffix[x]为点x包含区间的最大后缀的左边坐标,pair类型的max_sub[x]为点x包含区间的最大区间左右坐标。建树的时候算出每个节点的这三个值。询问的时候重要的是处理开始在前一半,结束在后一半的情况,同时要查找到[L,mid]的后缀和[mid+1,R]的前缀。因此还需要两个询问函数询问前缀和后缀,这两个询问比较特殊,不用同时给出ql和qr,如果是前缀,询问区间就是[L,qr],如果是后缀,询问区间是[ql,R],查询函数也有点不一样。

  代码很长,但思路比较清楚,sum函数用到了多态性。

#include<iostream>
#include<cstdio>
#include<cstring>
#define INF 0x3f3f3f3f
#define MAXN 500010
#define MAXNODE 1000010
using namespace std;

typedef pair<int,int> Interval;

int N,Q;
long long prefix_sum[MAXN];

long long sum(int L,int R){
    return prefix_sum[R]-prefix_sum[L-1];
}

long long sum(Interval a){
    return sum(a.first,a.second);
}

Interval better(Interval a,Interval b){
    if(sum(a)==sum(b)) return a<b?a:b;
    return sum(a)>sum(b)?a:b;
}

struct IntervalTree{
    int max_prefix[MAXNODE],max_suffix[MAXNODE];
    Interval max_sub[MAXNODE];

    void build(int o,int L,int R){
        if(L==R){
            max_prefix[o]=max_suffix[o]=L;
            max_sub[o]=make_pair(L,L);
            return;
        }

        //创建子树
        int mid=(L+R)/2,l=o<<1,r=(o<<1)+1;
        build(l,L,mid);
        build(r,mid+1,R);

        long long s1,s2;
        s1=sum(L,max_prefix[l]);
        s2=sum(L,max_prefix[r]);
        if(s1==s2) max_prefix[o]=max_prefix[l];
        else max_prefix[o]=s1>s2?max_prefix[l]:max_prefix[r];

        s1=sum(max_suffix[l],R);
        s2=sum(max_suffix[r],R);
        if(s1==s2) max_suffix[o]=max_suffix[l];
        else max_suffix[o]=s1>s2?max_suffix[l]:max_suffix[r];

        max_sub[o]=better(max_sub[l],max_sub[r]);
        max_sub[o]=better(max_sub[o],make_pair(max_suffix[l],max_prefix[r]));
    }

    //[L,qr]的最大前缀区间
    Interval query_prefix(int o,int L,int R,int qr){
        if(max_prefix[o]<=qr) return make_pair(L,max_prefix[o]);
        int mid=(L+R)/2;
        if(qr<=mid) return query_prefix(o<<1,L,mid,qr);
        Interval i=query_prefix((o<<1)+1,mid+1,R,qr);
        i.first=L;
        return better(i,make_pair(L,max_prefix[o<<1]));
    }

    //[ql,R]的最大前缀区间
    Interval query_suffix(int o,int L,int R,int ql){
        if(max_suffix[o]>=ql) return make_pair(max_suffix[o],R);
        int mid=(L+R)/2;
        if(ql>mid) return query_suffix((o<<1)+1,mid+1,R,ql);
        Interval i=query_suffix(o<<1,L,mid,ql);
        i.second=R;
        return better(i,make_pair(max_suffix[(o<<1)+1],R));
    }

    //[ql,qr]的最大区间
    Interval query_sub(int o,int L,int R,int ql,int qr){
        if(ql<=L&&qr>=R) return max_sub[o];
        int mid=(L+R)/2;
        if(qr<=mid) return query_sub(o<<1,L,mid,ql,qr);
        if(ql>mid) return query_sub((o<<1)+1,mid+1,R,ql,qr);
        Interval i1=query_prefix((o<<1)+1,mid+1,R,qr);
        Interval i2=query_suffix(o<<1,L,mid,ql);
        Interval i3=better(query_sub(o<<1,L,mid,ql,qr),query_sub((o<<1)+1,mid+1,R,ql,qr));
        return better(i3,make_pair(i2.first,i1.second));
    }
}tree;

int main(){
    freopen("in.txt","r",stdin);
    int cas=0;
    while(scanf("%d%d",&N,&Q)!=EOF){
        prefix_sum[0]=0;
        for(int i=1;i<=N;i++){
            int t;
            scanf("%d",&t);
            prefix_sum[i]=prefix_sum[i-1]+t;
        }
        tree.build(1,1,N);
        printf("Case %d:\n",++cas);
        while(Q--){
            int ql,qr;
            scanf("%d%d",&ql,&qr);
            Interval ans=tree.query_sub(1,1,N,ql,qr);
            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、付费专栏及课程。

余额充值