UVALive - 3938 "Ray, Pass me the dishes!" (线段树)

原题链接

Problem Description

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.
, … , aj .

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的数列D,对于每个询问(a,b),找到两个下标x,y在a,b之间,且 Dx + Dx+1 + Dx+2 +···+ Dy 尽量大。

解题思路

构造线段树,维护最大区间和,最大前缀和,最大后缀和,通过分治的思想进行区间合并。同时维护每个结点的前缀和终点prer,后缀和起点sufl,作为答案输出,详细步骤见代码注释。

AC代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<queue>
#include<list>
#include<stack>
#include<set>
#include<map>
#define ll long long
#define ull unsigned long long
#define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++)
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define pb push_back
#define PI 3.1415927
#define inf 0x3f3f3f3f
#define fi first
#define se second
using namespace std;
#define root 1,n,1
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef pair<int,int> Interval;
const int N=5e5+10;
int n,q,temp,l,r;
struct node
{
    int l,r,prer,sufl;//结点的左端,右端,前缀和终点,后缀和起点
    Interval sub;//答案要的区间
}dat[N<<2];
ll a[N];
ll sum(Interval x) {return a[x.se]-a[x.fi-1];}//区间中数的和
Interval getmax(Interval x,Interval y)//比较两个区间和的大小
{
    ll a=sum(x);
    ll b=sum(y);
    if(a!=b) return a>b?x:y;
    return x<y?x:y;
}
node com(node x,node y)//合并两个结点
{
    node res;
    res.l=x.l;
    res.r=y.r;
    res.prer=getmax(Interval(x.l,x.prer),Interval(x.l,y.prer)).se;
    res.sufl=getmax(Interval(y.sufl,y.r),Interval(x.sufl,y.r)).fi;
    res.sub=getmax(Interval(x.sufl,y.prer),getmax(x.sub,y.sub));
    return res;
}
void build(int l,int r,int rt)//建树
{
    if(l==r)
    {
        dat[rt].l=dat[rt].r=dat[rt].prer=dat[rt].sufl=l;
        dat[rt].sub=Interval(l,l);
        return ;
    }
    int m=l+r>>1;
    build(lson);
    build(rson);
    dat[rt]=com(dat[rt<<1],dat[rt<<1|1]);
}
node query(int l,int r,int rt)
{
    if(l<=dat[rt].l&&r>=dat[rt].r) return dat[rt];
    int m=dat[rt].l+dat[rt].r>>1;
    node res;
    if(l<=m&&r>m) res=com(query(l,r,rt<<1),query(l,r,rt<<1|1));
    else if(l<=m) res=query(l,r,rt<<1);
    else res=query(l,r,rt<<1|1);
    return res;
}
int main(void)
{
    int kase=0;
    while(cin>>n>>q)
    {
        a[0]=0;
        for(int i=1;i<=n;++i)
        {
            scanf("%d",&temp);
            a[i]=a[i-1]+temp;
        }
        build(root);
        printf("Case %d:\n",++kase);
        for(int i=1;i<=q;++i)
        {
            scanf("%d%d",&l,&r);
            Interval ans=query(l,r,1).sub;
            printf("%d %d\n",ans.fi,ans.se);
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值