codeforce/722/C 线段树合并

本文提供了一道来自Codeforces平台的题目722/C的解题思路与代码实现,该题要求在序列中每次删除一个数后,计算序列的最大片段和。文章采用线段树合并的方法解决此问题,详细介绍了线段树的构建、更新和查询过程。

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

题目:http://codeforces.com/problemset/problem/722/C

题意:每次在序列中删去一个数,求每次删去后序列中最大片段和。

思路:线段树合并

#include <bits/stdc++.h>
#define LL long long
#define lson rt<<1
#define rson rt<<1|1
using namespace std;
const int maxn = 1e5+5;

struct Tree{
    int mark;
    LL ls, rs, ms;
}T[maxn<<2];
int n, x; LL a[maxn];
void pushup(int rt){
    T[rt].ls = T[lson].ls;
    if(!T[lson].mark) T[rt].ls += T[rson].ls;

    T[rt].rs = T[rson].rs;
    if(!T[rson].mark) T[rt].rs += T[lson].rs;

    T[rt].ms = max(max(T[lson].ms, T[rson].ms), T[lson].rs + T[rson].ls);
    if(T[lson].mark || T[rson].mark) T[rt].mark = 1;
}
void build(int rt, int l, int r){
    T[rt].mark = 0;
    if(l == r) {
        T[rt].ls = T[rt].rs = T[rt].ms = a[l];
        return ;
    }
    int mid = l+r >> 1;
    build(lson, l, mid); build(rson, mid+1, r);
    pushup(rt);
}
void update(int rt, int l, int r, int x){
    if(l == r){
        T[rt].ls = T[rt].rs = T[rt].ms = 0;
        T[rt].mark = 1;
        return ;
    }
    int mid = l+r >> 1;
    if(x <= mid) update(lson, l, mid, x);
    else update(rson, mid+1, r, x);
    pushup(rt);
}
int main()
{
    cin >> n;
    for(int i=1; i<=n; i++) cin >> a[i];
    build(1, 1, n);
    for(int i=1; i<=n; i++){
        cin >> x;
        update(1, 1, n, x);
        cout << T[1].ms << endl;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值