uvalive3938 “Ray, Pass me the dishes!”

3938 “Ray, Pass me the dishes!”

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


题目链接:https://icpcarchive.ecs.baylor.edu/external/39/3938.pdf

题目大意:给出一个长度为n的整数序列D,你的任务是对m个询问做出回答.对于询问(a,b),有a <= x <= y <= b, 使得Dx + Dx+1 + ... + Dy尽量大.如果有多组,x,y都尽量小.

题目思路:基本用线段树来实现,构造最大连续和maxm,左区间最大连续和maxl,右区间最大连续和maxr,和相对应的区间段lr(左区间的最右点), rl(右区间的最左点),(sl, sr)对应区间的最大连续区间段.在用到pushup()向上更新时要满足连续和的特点:区间需要连续,所以这里更新不再是左区间长度==m-l+1才更新左区间所以用以下相应的代码:

pushup(int l, int r, int rt){
    int m = (l+r) >> 1;
    if(maxl[rt << 1 | 1] + c[m] - c[l-1] <= maxl[rt << 1]){
        maxl[rt] = maxl[rt << 1];
        lr[rt] = lr[rt << 1];
    }
    else{
        maxl[rt] = maxl[rt << 1 | 1] + c[m] - c[l-1];
        lr[rt] = lr[rt << 1 | 1];
    }
    if(maxr[rt << 1] + c[r] - c[m] < maxr[rt << 1 | 1]){
        maxr[rt] = maxr[rt << 1 | 1];
        rl[rt] = rl[rt << 1 | 1];
    }
    else{
        maxr[rt] = maxr[rt << 1] + c[r] - c[m];
        rl[rt] = rl[rt << 1];
    }
    if(maxm[rt << 1] >= maxm[rt << 1 | 1]){
        maxm[rt] = maxm[rt << 1], sl[rt] = sl[rt << 1], sr[rt] = sr[rt << 1];
    }
    else{
        maxm[rt] = maxm[rt << 1 | 1], sl[rt] = sl[rt << 1 | 1], sr[rt] = sr[rt << 1 | 1];
    }
    if(maxm[rt] < maxr[rt << 1] + maxl[rt << 1 | 1] || (maxm[rt] == maxr[rt << 1] + maxl[rt << 1 | 1] && (rl[rt << 1] < sl[rt] ||(rl[rt << 1] == sl[rt] && lr[rt << 1 | 1] <= sr[rt])))){
        maxm[rt] = maxr[rt << 1] + maxl[rt << 1 | 1];
        sl[rt] = rl[rt << 1];
        sr[rt] = lr[rt << 1 | 1];
    }
}


相应代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;

#define lson l, m, rt << 1
#define rson m+1, r, rt << 1 | 1

const int maxn = 500000;
int lr[maxn << 2], rl[maxn << 2], sr[maxn << 2], sl[maxn << 2];
long long maxl[maxn << 2], maxm[maxn << 2], maxr[maxn << 2];
long long c[maxn];

struct node{
    int x, y, lr, rl;
    long long sum, maxl, maxr;
};

long long Max(long long a, long long b, long long c){
    a = max(a, b);
    return max(a, c);
}

void pushup(int l, int r, int rt){
    int m = (l+r) >> 1;
    if(maxl[rt << 1 | 1] + c[m] - c[l-1] <= maxl[rt << 1]){
        maxl[rt] = maxl[rt << 1];
        lr[rt] = lr[rt << 1];
    }
    else{
        maxl[rt] = maxl[rt << 1 | 1] + c[m] - c[l-1];
        lr[rt] = lr[rt << 1 | 1];
    }
    if(maxr[rt << 1] + c[r] - c[m] < maxr[rt << 1 | 1]){
        maxr[rt] = maxr[rt << 1 | 1];
        rl[rt] = rl[rt << 1 | 1];
    }
    else{
        maxr[rt] = maxr[rt << 1] + c[r] - c[m];
        rl[rt] = rl[rt << 1];
    }
    if(maxm[rt << 1] >= maxm[rt << 1 | 1]){
        maxm[rt] = maxm[rt << 1], sl[rt] = sl[rt << 1], sr[rt] = sr[rt << 1];
    }
    else{
        maxm[rt] = maxm[rt << 1 | 1], sl[rt] = sl[rt << 1 | 1], sr[rt] = sr[rt << 1 | 1];
    }
    if(maxm[rt] < maxr[rt << 1] + maxl[rt << 1 | 1] || (maxm[rt] == maxr[rt << 1] + maxl[rt << 1 | 1] && (rl[rt << 1] < sl[rt] ||(rl[rt << 1] == sl[rt] && lr[rt << 1 | 1] <= sr[rt])))){
        maxm[rt] = maxr[rt << 1] + maxl[rt << 1 | 1];
        sl[rt] = rl[rt << 1];
        sr[rt] = lr[rt << 1 | 1];
    }
}

node query(int L, int R, int l, int r, int rt){
    node res;
    if(L <= l && R >= r){
        res.x = sl[rt];
        res.y = sr[rt];
        res.lr = lr[rt];
        res.rl = rl[rt];
        res.maxl = maxl[rt];
        res.maxr = maxr[rt];
        res.sum = maxm[rt];
        return res;
    }
    int m = (l+r) >> 1;
    if(R <= m) return query(L, R, lson);
    else if(L > m) return query(L, R, rson);
    else{
        node a, b;
        a = query(L, R, lson);
        b = query(L, R, rson);
        if(b.maxl + c[m] - c[l-1] <= a.maxl){
            res.maxl = a.maxl;
            res.lr = a.lr;
        }
        else{
            res.maxl = b.maxl + c[m] - c[l-1];
            res.lr = b.lr;
        }
        if(a.maxr + c[r] - c[m] < b.maxr){
            res.maxr = b.maxr;
            res.rl = b.rl;
        }
        else{
            res.maxr = a.maxr + c[r] - c[m];
            res.rl = a.rl;
        }
        if(a.sum >= b.sum){
            res.sum = a.sum, res.x = a.x, res.y = a.y;
        }
        else{
            res.sum = b.sum, res.x = b.x, res.y = b.y;
        }
        if(res.sum < a.maxr + b.maxl || (res.sum == a.maxr + b.maxl && (res.x > a.rl ||(res.x == a.rl && res.y > b.lr)))){
            res.sum = a.maxr + b.maxl;
            res.x = a.rl;
            res.y = b.lr;
        }
        return res;
    }
}

void build(int l, int r, int rt){
    if(l == r){
        sl[rt] = lr[rt] = rl[rt] = sr[rt] = l;
        maxl[rt] =  maxr[rt] = maxm[rt] = c[l]-c[l-1];
        return;
    }
    int m = (l+r) >> 1;
    build(lson);
    build(rson);
    pushup(l, r, rt);
}

int main(){
    int cas = 1, n, m;
    while(~scanf("%d%d", &n, &m)){
        c[0] = 0;
        for(int i = 1; i <= n; i++){
            scanf("%lld", &c[i]);
            c[i] += c[i-1];
        }
        build(1, n, 1);
        printf("Case %d:\n", cas++);
        while(m--){
            int a, b;
            scanf("%d%d", &a, &b);
            node ans = query(a, b, 1, n, 1);
            printf("%d %d\n", ans.x, ans.y);
        }
    }
    return 0;
}



/**
3 1
1 2 3
1 3
3 1
1 -2 3
1 2
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值