[POJ2750] 最大连续和 - 线段树

来源:互联网 发布:国家电网照片采集软件 编辑:程序博客网 时间:2024/04/30 07:24

题目描述

给出一个含有N个结点的环,编号分别为1..N,环上的点带有权值(可正可负),现要动态的修改某个点的权值,求每次修改后环上的最大连续和,但不能是整个序列的和。


输入格式

第一行为一个整数N(4<=N<=100000);
第二行为N个用空格分开的整数;
第三行为一个整数M(4<=M<=100000),表示修改的次数(绝对值小于等于1000);
接下来M行,每行两个整数A和B(-1000<=B<=1000),表示将序列中的第A个数的值,修改为B。


输出格式

对于每个修改,输出修改后环上的最大连续和。


样例数据

样例输入

5
3 -2 1 2 -5
4
2 -2
5 -5
2 -4
5 -1

样例输出

4
4
3
5


题目分析

此题有点难
预备知识:环上的最大连续和可以转为max{链上的最大和,所有数减去链上的最小和}
为什么呢?
这里写图片描述
因为如果最大跨越了链,那么最小一定是在链上的,并且补集就是最大,否则最大可以吞掉一部分变得更大。
那么我们就可以用线段树维护线段的和,线段的最大和,线段的最小和。
可是我们会发现,仅有这3个属性是无法完成标记上传的。
因为父亲的最大和最小和不仅仅可能是来自儿子的最大最小和,还可能各有一部分
这里写图片描述
因此我们还需要维护线段从左端点向右的最大最小和,从右端点向左的最大最小和。
那么我们就可以标记上传了。
father.sum=lc.sum+rc.sum
father.leftmax=max(lc.leftmax,lc.sum+rc.leftmax)
father.rightmax=max(rc.rightmax,rc.sum+lc.rightmax)
father.summax=max(lc.summax,rc.summax,lc.rightmax+rc.leftmax
min的属性同理

维护的属性较多,注意别写错了


源代码

#include<algorithm>#include<iostream>#include<iomanip>#include<cstring>#include<cstdlib>#include<vector>#include<cstdio>#include<cmath>#include<queue>using namespace std;inline const int Get_Int() {    int num=0,bj=1;    char x=getchar();    while(x<'0'||x>'9') {        if(x=='-')bj=-1;        x=getchar();    }    while(x>='0'&&x<='9') {        num=num*10+x-'0';        x=getchar();    }    return num*bj;}const int maxn=100000;struct Tree { //点修改 区间查询    int left,right,sum; //sum代表当前段的总和    int leftmax,rightmax,summax; //leftmax代表从左走最大的总和,rightmax代表从右走最大的总和,summax代表最大总和    int leftmin,rightmin,summin; //同理};struct Segment_Tree {    Tree tree[maxn*4];    void build(int index,int Left,int Right,int* a) {        tree[index].left=Left;        tree[index].right=Right;        if(Left==Right) { //叶结点            tree[index].sum=a[Left];            tree[index].leftmax=tree[index].rightmax=tree[index].summax=a[Left];            tree[index].leftmin=tree[index].rightmin=tree[index].summin=a[Left];            return;        }        int mid=(Left+Right)/2;        build(2*index,Left,mid,a);        build(2*index+1,mid+1,Right,a);        push_up(index);    }    void push_up(int index) { //标记上传,合并子树信息        tree[index].sum=tree[index*2].sum+tree[index*2+1].sum;        tree[index].leftmax=max(tree[index*2].leftmax,tree[index*2].sum+tree[index*2+1].leftmax); //leftmax可以来自左儿子leftmax或左->右儿子leftmax        tree[index].rightmax=max(tree[index*2+1].rightmax,tree[index*2+1].sum+tree[index*2].rightmax); //rightmax同理        tree[index].summax=max(max(tree[index*2].summax,tree[index*2+1].summax),tree[index*2].rightmax+tree[index*2+1].leftmax); //summax可以来自各个儿子单独的summax或左右儿子中间部分        tree[index].leftmin=min(tree[index*2].leftmin,tree[index*2].sum+tree[index*2+1].leftmin);        tree[index].rightmin=min(tree[index*2+1].rightmin,tree[index*2+1].sum+tree[index*2].rightmin);        tree[index].summin=min(min(tree[index*2].summin,tree[index*2+1].summin),tree[index*2].rightmin+tree[index*2+1].leftmin);    }    void modify(int index,int target,int data) {        if(target<tree[index].left||target>tree[index].right)return; //不相交        if(target==tree[index].left&&target==tree[index].right) {            tree[index].sum=data;            tree[index].leftmax=tree[index].rightmax=tree[index].summax=data;            tree[index].leftmin=tree[index].rightmin=tree[index].summin=data;            return;        }        modify(index*2,target,data);        modify(index*2+1,target,data);        push_up(index); //标记上传    }};Segment_Tree st;int n,m,a[100005];int main() {    n=Get_Int();    for(int i=1; i<=n; i++)a[i]=Get_Int();    st.build(1,1,n,a);    m=Get_Int();    for(int i=1; i<=m; i++) {        int pos=Get_Int(),v=Get_Int();        st.modify(1,pos,v);        if(st.tree[1].summax==st.tree[1].sum)printf("%d\n",st.tree[1].sum-st.tree[1].summin); //不能选完         else printf("%d\n",max(st.tree[1].summax,st.tree[1].sum-st.tree[1].summin));    }    return 0;}

0 0