Can you answer these queries V 【SPOJ - GSS5】【线段树 最大子段和】

题目链接


  WA了好多发,最后的时候把两个其中两个分开的判断「y1 == y2 与 else」放到一块去之后尽然直接A了。

  有一个整数序列a[n]。有m次询问,每次询问Max { A[i]+A[i+1]+...+A[j] ; x1 <= i <= y1 , x2 <= j <= y2 , x1 <= x2 , y1 <= y2 }。

  然后,就是去考虑怎样处理这个关系,直接分类讨论吧讨论区间是分开的y1 < y2这样的类型,还是else。

给两组样例:

1
6
12 -6 -5 -8 -5 -4
1
1 2 2 3
1
6
12 -6 -5 -8 -5 -4
1
1 3 2 5
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define efs 1e-6
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define max3(a, b, c) max(a, max(b, c))
#define max4(a, b, c, d) max(max(a, b), max(c, d))
#define max5(a, b, c, d, f) max4(a, b, c, max(d, f))
#define max6(a, b, c, d, f, g) max(max3(a, b, c), max3(d, f, g))
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1e4 + 7;
int N, M;
struct node
{
    int lm, rm, mx, sum;
    node(int a=0, int b=0, int c=0, int d=0):lm(a), rm(b), mx(c), sum(d) {}
    void give(int x) { lm = rm = mx = sum = x; }
}tree[maxN<<2];
inline void pushup(node &root, node lc, node rc)
{
    root.lm = max(lc.lm, lc.sum + rc.lm);
    root.rm = max(rc.rm, rc.sum + lc.rm);
    root.sum = lc.sum + rc.sum;
    root.mx = max5(root.lm, root.rm, lc.mx, rc.mx, lc.rm + rc.lm);
}
void buildTree(int rt, int l, int r)
{
    if(l == r)
    {
        int val;
        scanf("%d", &val);
        tree[rt].give(val);
        return;
    }
    int mid = HalF;
    buildTree(Lson); buildTree(Rson);
    pushup(tree[rt], tree[lsn], tree[rsn]);
}
node query(int rt, int l, int r, int ql, int qr)
{
    if(ql <= l && qr >= r) return tree[rt];
    int mid = HalF;
    if(qr <= mid) return query(QL);
    else if(ql > mid) return query(QR);
    else
    {
        node TL = query(QL), TR = query(QR);
        node ans;   pushup(ans, TL, TR);
        return ans;
    }
}
int main()
{
    int Cas; scanf("%d", &Cas);
    while(Cas--)
    {
        scanf("%d", &N);
        buildTree(1, 1, N);
        scanf("%d", &M);
        int range_left = 0, range_right = 0, range_sum = 0;
        for(int i=1, x1, y1, x2, y2; i<=M; i++)
        {
            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
            if(y1 < x2)     //x1 <= y1 < x2 <= y2
            {
                range_left = query(1, 1, N, x1, y1).rm;
                range_right = query(1, 1, N, x2, y2).lm;
                range_sum = 0;
                if(x2 - y1 > 1) range_sum = query(1, 1, N, y1 + 1, x2 - 1).sum;
                printf("%d\n", range_left + range_right + range_sum);
            }
            else
            {
                int ans;
                range_left = query(1, 1, N, x1, x2).rm;
                range_right = 0;
                if(x2 < y2) range_right = query(1, 1, N, x2 + 1, y2).lm;
                range_right = max(0, range_right);
                ans = range_left + range_right;
                range_left = query(1, 1, N, x1, y1).rm;
                range_right = 0;
                if(y1 < y2) range_right = query(1, 1, N, y1 + 1, y2).lm;
                ans = max(ans, range_left + range_right);
                ans = max(ans, query(1, 1, N, x2, y1).mx);
                printf("%d\n", ans);
            }
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值