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

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这里也可以只维护两个域,再最后再考虑跨区间的问题这里没有update操作,但是需要注意的是PushUp和Query.

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
#define MID                 ( l + r ) >> 1
#define CLR( a, b )         memset( a, b, sizeof(a) )
#define REP( i, a, b )      for( int i = (a); i < (b); ++i )
#define FOR( i, a, b )      for( int i = (a); i <= (b); ++i )
#define FOD( i, a, b )      for( int i = (a); i >= (b); --i )
#define lson                l, m, o << 1
#define rson                m + 1, r, o << 1 | 1
#define ls                  o << 1
#define rs                  o << 1 | 1
#define root                1, n, 1
#define MAXN                500050

struct SegTree
{
    int l, r;
    LL sum;
    LL sub, pre, suf;
    int subs, sube, pres, pree, sufs, sufe;

}tree[ MAXN << 2 ];

void PushUp( int o )
{
    tree[o].sum = tree[ls].sum + tree[rs].sum;

    //int l = tree[ls].l, r = tree[rs].r;
    LL val = tree[ls].pre;
    int st = tree[ls].pres, ed = tree[ls].pree;
    if( val < tree[ls].sum + tree[rs].pre )
        val = tree[ls].sum + tree[rs].pre, ed = tree[rs].pree;
    tree[o].pre = val, tree[o].pres = st, tree[o].pree = ed;

    val = tree[rs].suf;
    st = tree[rs].sufs, ed = tree[rs].sufe;
    if( val <= tree[ls].suf + tree[rs].sum )
        val = tree[ls].suf + tree[rs].sum, st = tree[ls].sufs;
    tree[o].suf = val, tree[o].sufs = st, tree[o].sufe = ed;

    val = tree[ls].sub;
    st = tree[ls].subs, ed = tree[ls].sube;
    if( val < tree[ls].suf + tree[rs].pre )
        val = tree[ls].suf + tree[rs].pre, st = tree[ls].sufs , ed = tree[rs].pree;
    if( val < tree[rs].sub )
        val = tree[rs].sub, st = tree[rs].subs, ed = tree[rs].sube;
    tree[o].sub = val, tree[o].subs = st, tree[o].sube = ed;
}

void Build( int l, int r, int o )
{
    tree[o]. l = l, tree[o].r = r;
    if( l == r )
    {
        scanf( "%lld", &tree[o].sum );
        tree[o].sub = tree[o].pre = tree[o].suf = tree[o].sum;
        tree[o].subs = tree[o].sube = tree[o].pres = tree[o].pree = tree[o].sufs = tree[o].sufe = l;
        return;
    }
    int m = MID;
    Build( lson );
    Build( rson );
    PushUp( o );
}

SegTree Query( int L, int R, int l, int r, int o )
{
    if( L <= tree[o].l && tree[o].r  <= R )
 //   if( L == tree[o].l && tree[o].r  == R )
    {
        return tree[o];
    }

    int m = MID;
    int ll = 0, rr = 0;
    SegTree res1, res2, res;
    if( L <= m )
    {
        res1 = Query( L, R, lson );
        ll = 1;
    }
    if( R > m )
    {
        res2 = Query( L, R, rson );
        rr = 1;
    }
    if( ll && !rr ) return res1;
    if( !ll && rr ) return res2;
    else
    {
        res.pre = res1.pre, res.pres = res1.pres, res.pree = res1.pree;
        if( res.pre < res1.sum + res2.pre )
            res.pre = res1.sum + res2.pre, res.pree = res2.pree;

        res.suf = res2.suf, res.sufs = res2.sufs, res.sufe = res2.sufe;
        if( res.suf <= res2.sum + res1.suf )
            res.suf = res2.sum + res1.suf, res.sufs = res1.sufs;

        res.sub = res1.sub, res.subs = res1.subs, res.sube = res1.sube;
        if( res.sub < res1.suf + res2.pre )
            res.sub = res1.suf + res2.pre, res.subs = res1.sufs, res.sube = res2.pree;
        if( res.sub < res2.sub )
            res.sub = res2.sub, res.subs = res2.subs, res.sube = res2.sube;

        res.sum = res1.sum + res2.sum;
        return res;
    }
}

void Orz()
{
    int m, n, cas = 1;
    while( ~scanf("%d %d",&n,&m) )
    {
        Build( root );
        printf( "Case %d:\n", cas++ );
        FOR( i, 1, m )
        {
            int a, b;
            scanf( "%d %d", &a, &b );
            SegTree orz = Query( a, b, root );
            printf( "%d %d\n", orz.subs, orz.sube );
        }
    }
    return;
}

int main()
{
    Orz();
    return 0;
}

代码一


/* ***********************************************
Problem       :
File Name     :
Author        :
Created Time  :
************************************************ */
//#define BUG
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#include <list>
#include <queue>
#include <ctime>
#include <iostream>
#include <cmath>
#include <set>
#include <string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define INF                 0x7fffffff
#define MAX                 0x3f3f3f3f

#define CLR( a, b )         memset( a, b, sizeof(a) )
#define REP( i, a, b )      for( int i = (a); i < (b); ++i )
#define FOR( i, a, b )      for( int i = (a); i <=(b); ++i )
#define FOD( i, a, b )      for( int i = (a); i >=(b); --i )
#define MID                 ( l + r ) >> 1
#define LSON                l, m, rt << 1
#define RSON                m + 1, r, rt << 1 | 1

#define MAXN                500050

LL Sum[MAXN << 2];
LL Sub[MAXN << 2], sb[MAXN << 2], eb[MAXN << 2];
LL Prefix[MAXN << 2], sp[MAXN << 2], ep[MAXN << 2];
LL Suffix[MAXN << 2], ss[MAXN << 2], es[MAXN << 2];
int m, n, cas = 1;

void PushUp(int rt)
{
    Sum[rt] = Sum[rt << 1] + Sum[rt << 1 | 1];

    int l = (rt << 1), r = (rt << 1 | 1);
    LL  Maxval = Prefix[l];

    //?ŠÀíprefixÇé¿ö
    int st = sp[l], ed = ep[l];
    if(Prefix[l] < Sum[l] + Prefix[r])
        Maxval = Sum[l] + Prefix[r], st = sp[l], ed = ep[r];
    Prefix[rt] = Maxval, sp[rt] = st, ep[rt] = ed;

    //?ŠÀísuffixÇé¿ö
    Maxval = Suffix[r], st = ss[r], ed = es[r];
    if(Suffix[r] <= Sum[r] + Suffix[l])
        Maxval = Sum[r] + Suffix[l], st = ss[l], ed = es[r];
    Suffix[rt] = Maxval, ss[rt] = st, es[rt] = ed;

    //?ŠÀíSubÇé¿ö
    Maxval = Sub[l], st = sb[l], ed = eb[l];
    if(Maxval < Suffix[l] + Prefix[r])
        Maxval = Suffix[l] + Prefix[r], st = ss[l], ed = ep[r];
    if(Maxval < Sub[r])
        Maxval = Sub[r], st = sb[r], ed = eb[r];
    Sub[rt] = Maxval, sb[rt] = st, eb[rt] = ed;
}

void Build(int l, int r, int rt)
{
    if(l == r)
    {
        scanf("%lld",&Sum[rt]);
        Sub[rt] = Prefix[rt] = Suffix[rt] = Sum[rt];
        sb[rt] = eb[rt] = sp[rt] = ep[rt] = ss[rt] = es[rt] = l;
        return;
    }
    int m = MID;
    Build(LSON);
    Build(RSON);
    PushUp(rt);
}
struct Orz
{
    LL sum;
    LL sub,pre,suf;
    int sb, eb, sp, ep, ss, es;
};

Orz Query(int L,int R,int l,int r,int rt)
{
    Orz ret;
    if(L <= l && r <= R)
    {
        Orz tt;
        tt.pre = Prefix[rt], tt.sub = Sub[rt], tt.suf = Suffix[rt];
        tt.sb = sb[rt], tt.eb = eb[rt];
        tt.sp = sp[rt], tt.ep = ep[rt];
        tt.ss = ss[rt], tt.es = es[rt];
        tt.sum = Sum[rt];
        return tt;
    }

    int m = MID;
    Orz t1, t2;
    int ll = 0, rr = 0;
    if(L <= m)
    {
        t1 = Query(L,R,LSON);
        ll = 1;
    }
    if(R > m)
    {
        t2 = Query(L,R,RSON);
        rr = 1;
    }
    if(ll && !rr)   ret = t1;
    if(!ll && rr)   ret = t2;
    if(ll && rr)
    {
        //?ŠÀíǰ׺prefix
        ret.pre = t1.pre, ret.sp = t1.sp, ret.ep = t1.ep;
        if(ret.pre < t1.sum + t2.pre)
            ret.pre = t1.sum + t2.pre, ret.ep = t2.ep;

        //?ŠÀíºó׺suffix
        ret.suf = t2.suf, ret.ss= t2.ss, ret.es = t2.es;
        if(ret.suf <= t2.sum + t1.suf)  //ÒòΪÓÐx,yŸ¡Á¿Ð¡
            ret.suf = t2.sum + t1.suf, ret.ss = t1.ss;

        //?ŠÀí×Ó?®Sub
        ret.sub = t1.sub, ret.sb = t1.sb, ret.eb = t1.eb;
        if(ret.sub < t1.suf + t2.pre)
            ret.sub = t1.suf + t2.pre, ret.sb = t1.ss, ret.eb = t2.ep;
        if(ret.sub < t2.sub)
            ret.sub = t2.sub, ret.sb = t2.sb, ret.eb = t2.eb;

        ret.sum = t1.sum + t2.sum;
    }
    return ret;

}

int main()
{
    #ifdef BUG
        freopen("in.txt","r",stdin);
    #endif
    while(~scanf("%d %d",&n,&m))
    {
        //FOR(i,1,n)  scanf("%lld",&num[i]);
        Build(1,n,1);
        printf("Case %d:\n",cas++);
        FOR(i,1,m)
        {
            int a, b;
            scanf("%d %d",&a,&b);
            Orz orz = Query(a,b,1,n,1);
            printf("%d %d\n",orz.sb,orz.eb);
        }
    }
    return 0;
}

代码二

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define lc rt<<1
#define rc rt<<1|1

const int maxn = 500000 + 5;
typedef long long LL;
LL num[maxn], max_prefix[maxn<<2], max_suffix[maxn<<2];

struct node{
    int l, r;
    node(int ll=0, int rr=0):l(ll),r(rr){}
} max_sub[maxn<<2];

LL prefix_sum[maxn];
int  ql, qr;

LL sum(int l, int r){
    return prefix_sum[r] - prefix_sum[l-1];
}
LL sum(node a){
    return sum(a.l, a.r);
}
node better(node a, node b){
    if(sum(a) != sum(b)) return sum(a) > sum(b) ? a:b;
    return (a.l < b.l||(a.l==b.l&&a.r<b.r))? a : b;
}

void build(int rt,int l,int r){
    if(l == r){
        max_prefix[rt] = max_suffix[rt] = l;
        max_sub[rt] = node(l, l);
        return ;
    }
    int m = (l+r) >> 1;
    build(lc, l, m);
    build(rc, m+1, r);
    LL v1 = sum(l, max_prefix[lc]);
    LL v2 = sum(l, max_prefix[rc]);
    if(v1 == v2) max_prefix[rt] = min(max_prefix[lc], max_prefix[rc]);
    else max_prefix[rt] = 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[rt] = min(max_suffix[lc], max_suffix[rc]);
    else max_suffix[rt] = v1 > v2 ? max_suffix[lc] : max_suffix[rc];
    max_sub[rt] = better(max_sub[lc], max_sub[rc]);
    max_sub[rt] = better(max_sub[rt], node(max_suffix[lc], max_prefix[rc]));
}

int query_prefix(int rt, int l, int r){
    if(qr >= max_prefix[rt]) return max_prefix[rt];
    int m = (l+r)>>1;
    //l<=qr<=m
    if(qr <= m) return query_prefix(lc, l, m);
    //m+1<=qr<=r
    int rr = query_prefix(rc, m+1, r);
    node ret = better(node(l,rr), node(l, max_prefix[lc]));
    return ret.r;
}

int query_suffix(int rt, int l, int r){
    if(ql <= max_suffix[rt]) return max_suffix[rt];
    int m = (l+r)>>1;
    //m+1<=ql<=r
    if(ql > m) return query_suffix(rc, m+1, r);
    //l<=ql<=m
    int ll = query_suffix(lc, l, m);
    node ret = better(node(ll, r), node(max_suffix[rc], r));
    return ret.l;
}

node query(int rt, int l, int r){
    if(ql <= l && r <= qr) return max_sub[rt];
    int m = (l+r)>>1;

    if(qr <= m) return query(lc, l, m);

    if(ql >  m) return query(rc, m+1, r);

    //ql <= m <= qr
    int ll = query_suffix(lc, l, m);  //l_max_suffix
    int rr = query_prefix(rc, m+1, r); //r_max_prefix
    node mid = node(ll, rr);
    node sub = better( query(lc, l, m), query(rc, m+1, r));
    return better( mid, sub);
}
int main()
{
    int n, m, i, cas = 1, l, r;
    while(~scanf("%d%d", &n, &m)){
        printf("Case %d:\n", cas++);
        prefix_sum[0] = 0;
        for(i=1; i<=n; ++i) {
                scanf("%lld", &num[i]);
                prefix_sum[i] = prefix_sum[i-1] + num[i];
        }
        node v = node(1,3);
        build(1, 1, n);
        while(m--){
            scanf("%d%d", &l, &r);
            ql = l; qr = r;
            node ans = query(1, 1, n);
            printf("%d %d\n", ans.l, ans.r);
        }
    }
    return 0;
}

别人的代码

				
时间: 2024-10-05 09:44:10

Uva 1400 "Ray, Pass me the dishes!" ( 线段树 + 区间查询 )的相关文章

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;(线段树)

题目链接 题目大意:给你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.然后每次查询的时候都返回一个新的节点,是新的

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

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

LA3938:&quot;Ray, Pass me the dishes!&quot;(线段树)

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 (actua

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

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

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

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