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 rst 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 le contains multiple test cases. For each test case, there are two integers n and m in the rst 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 le.

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的整数序列D,对m个询问做出回答,对询问(a,b)找到(x,y)使得a<=x<=y<=b且Dx+Dx+1+……+Dy最大。如有多组答案取字典序最小的一组。

算法采用线段树,对于每个线段维护三个值,max_sub,max_prefix,max_suffix.因为对于一个区间(a,b).可能完全被包含在一条线段中,又或是两条线段的前半部分和后半部分拼在一起得到的。

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

using namespace std;
const int maxn=500000+10;
const int maxnode=1000000+10;
typedef long long LL;
typedef pair<int,int>Interval;
LL prefix_sum[maxn];
LL sum(int L,int R)
{
    return prefix_sum[R]-prefix_sum[L-1];
}
LL sum(Interval p)
{
    return sum(p.first,p.second);
}
Interval better(Interval a,Interval b)
{
    if(sum(a)!=sum(b)){
        return sum(a)>sum(b)?a:b;
    }
    return a<b?a:b;
}
int qL,qR;
struct IntervalTree{
    Interval max_sub[maxnode];
    int max_prefix[maxnode];
    int max_suffix[maxnode];
    void build(int o,int L,int R)
    {
        if(L==R){
            max_prefix[o]=L;
            max_suffix[o]=R;
            max_sub[o]=make_pair(L,L);
        }else{
            int lc=o*2,rc=o*2+1;
            int M=(L+R)/2;
            build(lc,L,M);
            build(rc,M+1,R);
            LL v1=sum(L,max_prefix[lc]);//LL 防止溢出
            LL v2=sum(L,max_prefix[rc]);
            if(v1==v2)max_prefix[o]=min(max_prefix[lc],max_prefix[rc]);
            else max_prefix[o]=v1>v2?max_prefix[lc]:max_prefix[rc];

            v1=sum(max_suffix[lc],R);
            v2=sum(max_suffix[rc],R);
            if(v1==v2)max_suffix[o]=min(max_suffix[lc],max_suffix[rc]);
            else max_suffix[o]=v1>v2?max_suffix[lc]:max_suffix[rc];

            max_sub[o]=better(max_sub[lc],max_sub[rc]);
            max_sub[o]=better(max_sub[o],make_pair(max_suffix[lc],max_prefix[rc]));
        }
    }
    Interval query_prefix(int o,int L,int R)
    {
        if(max_prefix[o]<=qR)return make_pair(L,max_prefix[o]);
        int M=(L+R)/2;
        int lc=o*2,rc=o*2+1;
        if(qR<=M)return query_prefix(lc,L,M);
        Interval i=query_prefix(rc,M+1,R);
        i.first=L;
        return better(i,make_pair(L,max_prefix[lc]));
    }
    Interval query_suffix(int o,int L,int R)
    {
        if(max_suffix[o]>=qL)return make_pair(max_suffix[o],R);
        int M=(L+R)/2;
        int lc=o*2,rc=o*2+1;
        if(qL>M)return query_suffix(rc,M+1,R);
        Interval i=query_suffix(lc,L,M);
        i.second=R;
        return better(i,make_pair(max_suffix[rc],R));
    }
    Interval query(int o,int L,int R)
    {
        if(qL<=L&&qR>=R){
            return max_sub[o];
        }else{
            int lc=o*2,rc=o*2+1;
            int M=(L+R)/2;
            if(qR<=M)return query(lc,L,M);
            if(qL>M)return query(rc,M+1,R);
            Interval i1=query_suffix(lc,L,M);
            Interval i2=query_prefix(rc,M+1,R);
            Interval i3=better(query(lc,L,M),query(rc,M+1,R));
            return better(make_pair(i1.first,i2.second),i3);
        }
    }
};
IntervalTree tree;

int main()
{
    int kase=0,n,a,Q;
    while(scanf("%d%d",&n,&Q)==2){
        prefix_sum[0]=0;
        for(int i=0;i<n;i++){
            scanf("%d",&a);
            prefix_sum[i+1]=prefix_sum[i]+a;
        }
        tree.build(1,1,n);
        printf("Case %d:\n",++kase);
        for(int i=0;i<Q;i++){
            int L,R;
            scanf("%d%d",&L,&R);
            qL=L;qR=R;
            Interval ans=tree.query(1,1,n);
            printf("%d %d\n",ans.first,ans.second);
        }
    }
    return 0;
}
/*8 5
1 2 3 5 6 1 2 -1 8
Case 1:
1 6

Process returned -1073741571 (0xC00000FD)   execution time : 31.778 s
Press any key to continue.*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值