LA3938:"Ray, Pass me the dishes!"(线段树)

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 [ab] (inclusive) to represent all the dishes aa + 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 (nm <
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的整数序列,然后m次询问,对于每次询问,要求找到区间内的两个下标x,y使得区间和尽量大,如果有多解,x尽量小,还有多解,那么y也尽量小

思路:
线段树,建树过程中要标记
max_all:最大连续和
max_prefix:最大前缀和
max_suffix:最大后缀和
pre_r:最大前缀的结束位置
suf_l:最大后缀的开始位置
sum:区间总和

对于更新,由于要x尽量小与y尽量小,所以在更新的时候我们要确定好更新的顺序,也就是l先尽量小,然后才是y尽量小
更新有5种状况,那么按顺序就是

1.左边的sum+右区间的max_prefix;
2.左区间max_all
3.左边的max_suffix+右边的max_prefix
4.左边的max_suffix+右边的sum
5.右边的max_all


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define lson 2*i
#define rson 2*i+1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 500005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)

LL s[N];
int n,m;
struct node
{
    int l,r,pre_r,suf_l;
    LL max_all,max_prefix,max_suffix,sum;
} a[N<<2];

void pushup(int l,int r,int i)
{
    int mid = (l+r)/2;
    a[i].sum = a[lson].sum+a[rson].sum;

    a[i].max_prefix = a[lson].max_prefix;
    a[i].pre_r = a[lson].pre_r;
    a[i].max_suffix = a[rson].max_suffix;
    a[i].suf_l = a[rson].suf_l;
//1.左边的sum+右区间的max_prefix;
    a[i].max_all = a[lson].sum+a[rson].max_prefix;
    a[i].l = l;
    a[i].r = a[rson].pre_r;
//2.左区间max_all
    if(a[i].max_all<a[lson].max_all || (a[i].max_all==a[lson].max_all&&a[i].l==a[lson].l))
    {
        a[i].max_all = a[lson].max_all;
        a[i].l = a[lson].l;
        a[i].r = a[lson].r;
    }
//3.左边的max_suffix+右边的max_prefix
    if(a[i].max_all<a[lson].max_suffix+a[rson].max_prefix || (a[i].max_all==a[lson].max_suffix+a[rson].max_prefix &&a[i].l>a[lson].suf_l))
    {
        a[i].max_all=a[lson].max_suffix+a[rson].max_prefix;
        a[i].l = a[lson].suf_l;
        a[i].r = a[rson].pre_r;
    }
//4.左边的max_suffix+右边的sum
    if(a[i].max_all<a[lson].max_suffix+a[rson].sum || (a[i].max_all==a[lson].max_suffix+a[rson].sum &&a[i].l>a[lson].suf_l))
    {
        a[i].max_all=a[lson].max_suffix+a[rson].sum;
        a[i].l = a[lson].suf_l;
        a[i].r = r;
    }
//5.右边的max_all
    if(a[i].max_all<a[rson].max_all || (a[i].max_all==a[rson].max_all&&a[i].l>a[rson].suf_l))
    {
        a[i].max_all = a[rson].max_all;
        a[i].l = a[rson].l;
        a[i].r = a[rson].r;
    }
//更新前缀与后缀的位置
    if(a[lson].sum+a[rson].max_prefix>a[i].max_prefix)
    {
        a[i].max_prefix=a[lson].sum+a[rson].max_prefix;
        a[i].pre_r = a[rson].pre_r;
    }
    if(a[rson].sum+a[lson].max_suffix>=a[i].max_suffix)
    {
        a[i].max_suffix=a[rson].sum+a[lson].max_suffix;
        a[i].suf_l = a[lson].suf_l;
    }
}

void init(int l,int r,int i)
{
    if(l == r)
    {
        a[i].max_all = a[i].max_prefix = a[i].max_suffix = a[i].sum = s[l];
        a[i].l = a[i].r = a[i].pre_r = a[i].suf_l = l;
        return ;
    }
    int mid = (l+r)/2;
    init(LS);
    init(RS);
    pushup(l,r,i);
}

node query(int i,int l,int r,int L,int R)
{
    if(l==L&&r==R)
    {
        return a[i];
    }
    int mid=(L+R)>>1;
    if(r<=mid)     return  query(lson,l,r,L,mid);
    else if(l>mid) return  query(rson,l,r,mid+1,R);
    else
    {
        node t1=query(lson,l,mid,L,mid);
        node t2=query(rson,mid+1,r,mid+1,R);
        node t;
        t.sum = t1.sum+t2.sum;

        t.max_prefix = t1.max_prefix;
        t.pre_r = t1.pre_r;
        t.max_suffix = t2.max_suffix;
        t.suf_l = t2.suf_l;

        t.max_all = t1.sum+t2.max_prefix;
        t.l = l;
        t.r = t2.pre_r;

        if(t.max_all<t1.max_all || (t.max_all==t1.max_all&&t.l==t1.l))
        {
            t.max_all = t1.max_all;
            t.l = t1.l;
            t.r = t1.r;
        }
        if(t.max_all<t1.max_suffix+t2.max_prefix || (t.max_all==t1.max_suffix+t2.max_prefix &&t.l>t1.suf_l))
        {
            t.max_all=t1.max_suffix+t2.max_prefix;
            t.l = t1.suf_l;
            t.r = t2.pre_r;
        }
        if(t.max_all<t1.max_suffix+t2.sum || (t.max_all==t1.max_suffix+t2.sum &&t.l>t1.suf_l))
        {
            t.max_all=t1.max_suffix+t2.sum;
            t.l = t1.suf_l;
            t.r = t2.r;
        }
        if(t.max_all<t2.max_all || (t.max_all==t2.max_all&&t.l>t2.l))
        {
            t.max_all = t2.max_all;
            t.l = t2.l;
            t.r = t2.r;
        }

        if(t1.sum+t2.max_prefix>t.max_prefix)
        {
            t.max_prefix=t1.sum+t2.max_prefix;
            t.pre_r = t2.pre_r;
        }
        if(t2.sum+t1.max_suffix>=t.max_suffix)
        {
            t.max_suffix=t2.sum+t1.max_suffix;
            t.suf_l = t1.suf_l;
        }
        return t;
    }
}

int main()
{
    int i,j,l,r,cas = 1;
    while(~scanf("%d%d",&n,&m))
    {
        for(i = 1; i<=n; i++)
            scanf("%lld",&s[i]);
        init(1,n,1);
        printf("Case %d:\n",cas++);
        while(m--)
        {
            scanf("%d%d",&l,&r);
            node t=query(1,l,r,1,n);
            printf("%d %d\n",t.l,t.r);
        }
    }

    return 0;
}

时间: 2024-10-17 19:22:46

LA3938:"Ray, Pass me the dishes!"(线段树)的相关文章

LA3938 &quot;Ray, Pass me the dishes!&quot; (线段树区间合并)

题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22105 题意:给定整数n和m,给出一个n个元素的序列,查询m次给定区间[L,R]的最大连续和的位置[x,y],有多个区间输出x最小的,还有多个的话输出y最小的. 分析:每个节点存8个信息,最大连续和.最大后缀和.最大前缀和.区间和.前缀末位置.后缀首位置.最大连续和的首位置和末位置. 最大连续和=max(lson最大连续和,rson最大连续和,lson最大后缀+rso

UVALive3938 &quot;Ray, Pass me the dishes!&quot; 线段树动态区间最大和

AC得相当辛苦的一道题,似乎不难,但是需要想仔细, 开始的时候的错误思路----是受之前做过的区间最长连续子串影响http://blog.csdn.net/u011026968/article/details/38357157 区间合并的时候,我直接按照---如果(左子树的最大前缀和长度==左子树的长度 && 右子树的前缀和>0),就合并左前缀,这想法有两个错误:1.右子树的前缀和==0的时候是不是要合并区间呢?题目要求x尽量小,如果x相同y尽量小(x是ans的区间左端点,y是Ans

UVA 1400 1400 - &quot;Ray, Pass me the dishes!&quot;(线段树)

UVA 1400 - "Ray, Pass me the dishes!" 题目链接 题意:给定一个序列,每次询问一个[L,R]区间,求出这个区间的最大连续子序列和 思路:线段树,每个节点维护3个值,最大连续子序列,最大连续前缀序列,最大连续后缀序列,那么每次pushup的时候,根据这3个序列去拼凑得到新的一个结点即可 代码: #include <cstdio> #include <cstring> #include <algorithm> usin

uva 1400 - &quot;Ray, Pass me the dishes!&quot;(线段树)

题目链接:uva 1400 - "Ray, Pass me the dishes!" 题目大意:给定一个长度为n个整数序列,对m次询问作出回答,对于每次询问(a,b),找到两个下标x,y使得x到y的连续和为区间a,b中最大的连续和,如果存在多解优先x小,然后y小. 解题思路:线段树,对于每个节点维护三个线段值: max_sub:区间连续最大和 max_prefix:区间连续前缀最大和 max_suffix:区间连续后缀最大和 建树的过程维护三个值,查询时只需考虑左右子节点的max_su

Uva 1400 &quot;Ray, Pass me the dishes!&quot; ( 线段树 + 区间查询 )

Uva  1400 "Ray, Pass me the dishes!" (线段树 + 区间查询) 题意: 给顶一个长度为n的整数序列D,我们的任务是对m的询问做出回答对于询问(a,b),需要找到两个下标x和y,是的 a <= x <= y <=b并且Dx+...........Dy 尽量大. x,y尽量小 分析: 这题是做线段树比较好的一题,大白书上介绍的是维护了三个域,maxsub,maxpre,maxsuf这里也可以只维护两个域,再最后再考虑跨区间的问题这里没有

UVA - 1400&quot;Ray, Pass me the dishes!&quot;(线段树)

题目链接 题目大意:给你N个数字,要求你动态的给出L到R之间,X>= L && Y<=R,使得X,Y这段的连续和是LR之间的最大连续和,如果有多解,输出X小的,接着是Y小的. 解题思路:结点保存三个附加线段,max_sub, max_suffix, max_prefix.对于每次查询最大的连续和要不出现在左子树的max_sub, 要不就是右子树的max_sub, 要不就是左子树的max_suffix + 右子树的max_prefix.然后每次查询的时候都返回一个新的节点,是新的

uvalive 3938 &quot;Ray, Pass me the dishes!&quot; 线段树 区间合并

题意:求q次询问的静态区间连续最大和起始位置和终止位置 输出字典序最小的解. 思路:刘汝佳白书 每个节点维护三个值 pre, sub, suf 最大的前缀和, 连续和, 后缀和 然后这个题还要记录解的位置所以还要区间总和sum 1 #include<iostream> 2 #include<string> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<cstdio> 6 #include

UVAlive - 3938 —— &quot;Ray, Pass me the dishes!&quot; 【线段树】

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22105 #include <iostream> #include <cstdio> #include <cstring> #include <string> #define INF 0x3f3f3f3f #define lson rt<<1, l, m #define rson rt<<1|1, m+1, r u

UVA 1400 1400 - &amp;quot;Ray, Pass me the dishes!&amp;quot;(线段树)

UVA 1400 - "Ray, Pass me the dishes!" 题目链接 题意:给定一个序列,每次询问一个[L,R]区间.求出这个区间的最大连续子序列和 思路:线段树,每一个节点维护3个值.最大连续子序列.最大连续前缀序列,最大连续后缀序列,那么每次pushup的时候,依据这3个序列去拼凑得到新的一个结点就可以 代码: #include <cstdio> #include <cstring> #include <algorithm> us