245. 你能回答这些问题吗 线段树 求某区间最大连续子段和

题目

在这里插入图片描述

题解思路

最大连续字段和 (tmax) 可以由某个节点的tmax推到,也可能是两端区间合并而成 这样我们就需要 lmax 和 rmax 求出左边节点的最大后缀和 和右边节点的最大前缀和。
而推出lmax 又需要分两种情况 ,lmax 可能由 左边节点的 lmax直接得到 也可能由左节点的sum加上右节点的lmax得到。rmax则同理。
这样的话我们要在线段树里放 l r sum lmax rmax tmax
在这里插入图片描述
在建树和修改值的时候要使用上面的转移方程更新对应的值。

但是查询仅仅只需求max 的 tmax吗?
这是错误的想法。因为给定的区间不一定是完全的属于某个部分的,他们直接可能存在部分的合并,而这部分的合并是线段树之前没有操作到的。
在这里插入图片描述
所以我们需要进行之前的取可能的两段的tmax操作
我们先找到通过递归两边到底有那些节点 ,取出 , 再让这两个节点进行 之前的更新操作

void pushup (node &u , node&l , node&r )
{
    u.sum = l.sum + r.sum ;
    u.lma = max(l.lma , l.sum + r.lma );
    u.rma = max(r.rma , r.sum + l.rma );
    u.tma = max( max( l.tma , r.tma) , l.rma + r.lma );
}
node sea( int i ,int t1 , int t2 )
{
    if ( tee[i].l >= t1 && tee[i].r <= t2 )
        return tee[i];
    // pd(i);
    int mid = tee[i].l + tee[i].r >> 1 ;
    if ( t2 <= mid )
        return sea(i*2 , t1 ,t2 );
    else if ( t1 > mid )
        return sea(i*2+1  , t1 , t2 );
    else
    {
        node le = sea(i*2 , t1 , t2 );
        node ri = sea(i*2+1 , t1 , t2 );
        node res ;
        pushup(res,le,ri);
        return res ;
    }
}

在这里插入图片描述

参考文章

AC代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
using namespace std;

const  int  INF =  0x3f3f3f3f;

struct node
{
    long long sum , lma , rma ,tma ;
    int l,r;
};
const int N = 1001000 ;
long long a[N];
node tee[2*N];

int n , m ;


void pushup (node &u , node&l , node&r )
{
    u.sum = l.sum + r.sum ;
    u.lma = max(l.lma , l.sum + r.lma );
    u.rma = max(r.rma , r.sum + l.rma );
    u.tma = max( max( l.tma , r.tma) , l.rma + r.lma );
}



void  in(int i , int t1 , int t2)
{
    tee[i].l = t1 , tee[i].r = t2;
    if ( t1 == t2 )
    {
        tee[i].sum = a[t1] ;
        tee[i].lma = a[t1] ;
        tee[i].rma = a[t1] ;
        tee[i].tma = a[t1] ;
        return ;
    }
    int j = (t1+t2)/2;
    in(i*2,t1,j);
    in(i*2+1,j+1,t2);
    tee[i].sum = tee[i*2].sum+tee[i*2+1].sum;
    tee[i].lma = max( tee[i*2].lma , tee[i*2].sum + tee[i*2 + 1 ].lma );
    tee[i].rma = max( tee[i*2+1].rma , tee[i*2+1].sum + tee[i*2].rma ) ;
    tee[i].tma =  max( max( tee[i*2].rma + tee[i*2+1].lma , tee[i*2].tma ) , tee[i*2+1].tma ) ;
}


node sea( int i ,int t1 , int t2 )
{
    if ( tee[i].l >= t1 && tee[i].r <= t2 )
        return tee[i];
    // pd(i);
    int mid = tee[i].l + tee[i].r >> 1 ;
    if ( t2 <= mid )
        return sea(i*2 , t1 ,t2 );
    else if ( t1 > mid )
        return sea(i*2+1  , t1 , t2 );
    else
    {
        node le = sea(i*2 , t1 , t2 );
        node ri = sea(i*2+1 , t1 , t2 );
        node res ;
        pushup(res,le,ri);
        return res ;
    }
}
void _add( int i , int t1 , int t2 , int vv )
{
    if ( tee[i].l >= t1 && tee[i].r <= t2 )
    {
         tee[i].sum = vv ;
         tee[i].tma = vv ;
         tee[i].lma = vv ;
         tee[i].rma = vv ;
         return;
    }
    if ( tee[i*2].r >= t1)
        _add(i*2,t1,t2,vv);
    if (tee[i*2+1].l <= t2)
        _add(i*2+1,t1,t2,vv);
    tee[i].sum = tee[i*2].sum + tee[i*2+1].sum;
    tee[i].lma = max( tee[i*2].lma , tee[i*2].sum + tee[i*2 + 1 ].lma );
    tee[i].rma = max( tee[i*2+1].rma , tee[i*2+1].sum + tee[i*2].rma ) ;
    tee[i].tma =  max( max( tee[i*2].rma + tee[i*2+1].lma , tee[i*2].tma ) , tee[i*2+1].tma ) ;

}

int main ()
{
    ios::sync_with_stdio(false);
    cin >> n >> m ;
    for (int i = 1 ; i <= n ; i++ )
        cin >> a[i] ;
    in(1 , 1 , n );
    while ( m-- )
    {
        int t1 , t2 , t3 ;
        cin >> t1 >> t2 >> t3 ;
        if ( t1 == 1 )
        {
            if ( t2 > t3 )
                swap(t2 , t3 );
            cout<<sea(1,t2,t3).tma<<"\n";
        }else
        {
            _add(1 , t2 , t2 , t3);
        }
    }
    return 0 ;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值