codeforces Destroying Array 线段树维护区间

本文介绍了一种使用线段树数据结构解决给定序列每次破坏后的最大值问题的方法。通过维护每个节点的总和、最大值、最大前缀和最大后缀,实现了高效查询和更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
    题目描述:给出n个数的序列a[1] , a[2] , ... ,a[n] ,每次将其中的一个数破坏掉,每一次破坏掉以后问连续数
             的最大值是多少
                
    方法:利用线段树维护,线段树的每一个节点(l , r , x)包含4个信息:(l , r)中的总和,(l , r)中的最大值,(l , r)中最大前缀,
         (l , r)中最大后缀,每次修改后输出t[1].summax(1节点的最大值)即可,每次更新的pushup具体操作见代码
*/
#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#include<string>
#include<sstream>
#define LL long long
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define lson l,m,x<<1
#define rson m+1,r,x<<1|1
using namespace std;
const LL INF = 1e15 + 5 ;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps=1e-6;
const int maxn = 1e5 + 5 ;
struct node
{
    LL sum , summax , lmax , rmax ;
}t[4 * maxn];
LL a[maxn] ;
int op[maxn] ;
LL Max(const LL a , const LL b)
{
    return a > b ? a : b ;
}
void pushup(int l , int r , int x)
{
    t[x].sum = t[x<<1].sum + t[x << 1 | 1].sum ;                    //总和等于左右两节点总和
    t[x].summax = Max(t[x<<1].summax , Max(t[x<<1|1].summax , t[x<<1].rmax + t[x <<1 | 1].lmax) ) ;     //最大值的三种可能:左子节点最大值,右子节点最大值,左子节点最大后缀+右子节点最大前缀
    t[x].lmax = Max(t[x<<1].sum + t[x<<1|1].lmax , t[x<<1].lmax ) ;  //最大前缀二种可能:左子节点最大前缀 , 左子节点总和+右子节点最大前缀
    t[x].rmax = Max( t[x<<1].rmax + t[x<<1|1].sum , t[x<<1|1].rmax ) ; //最大后缀二种可能:右子节点最大后缀 , 右子节点总和+左子节点最大后缀
    if(t[x].sum < 0)        t[x].sum = -INF;        //<0就置于-INF是因为害怕-INF加多了会爆LL型
    if(t[x].summax < 0)        t[x].summax = -INF;
    if(t[x].lmax < 0)        t[x].lmax = -INF;
    if(t[x].rmax < 0)        t[x].rmax = -INF;
}
void build(int l , int r , int x)
{
    if(l == r){
        t[x].sum = t[x].lmax = t[x].summax = t[x].rmax = a[l] ;
        return ;
    }
    int m = l + (r - l ) / 2 ;
    build(lson);
    build(rson);
    pushup(l , r , x);
}
void modify(int pos , LL val , int l , int r , int x)
{
    if(pos==l && l == r){
        t[x].sum = t[x].lmax = t[x].summax = t[x].rmax = val ;
        return ;
    }
    int m = l + (r - l) / 2;
    if(pos <= m)
        modify(pos , val , lson);
    else
        modify(pos , val , rson);
    pushup(l , r , x) ;
}
int main()
{
    int n ;
    scanf("%d",&n);
    for(int i = 1 ; i<= n ; i++)
        scanf("%lld",&a[i]);
    for(int i = 1 ; i<= n ; i++)
        scanf("%d",&op[i]);
    build(1 , n , 1 );
    for(int i = 1 ; i <= n ; i++){
        modify( op[i] , -INF , 1 , n , 1 ) ;
        LL ans = t[1].summax ;
        if(ans > 0)
            printf("%lld\n",ans);
        else
            printf("0\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值