GSS3 C

//在gss1的基础上加了修改操作,一样的做法,加一个modify函数就可以了 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

const int N=5e4+5;

int n,m,type,l,r;
int L,R,M,S;
struct Tree
{
    int l,r,mid;
    int sum,lmaxn,rmaxn,maxn;
}tree[N<<2];

int read()
{
    char c=getchar();int num=0,f=1;
    for(;!isdigit(c);c=getchar())
        if(c=='-')
            f=-1;
    for(;isdigit(c);c=getchar())
        num=num*10+c-'0';
    return num*f;
}

void pushup(int root)
{
    tree[root].maxn=max(tree[root<<1].maxn,max(tree[root<<1|1].maxn,tree[root<<1].rmaxn+tree[root<<1|1].lmaxn));
    tree[root].lmaxn=max(tree[root<<1].lmaxn,tree[root<<1].sum+tree[root<<1|1].lmaxn);
    tree[root].rmaxn=max(tree[root<<1|1].rmaxn,tree[root<<1|1].sum+tree[root<<1].rmaxn);
    tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum;
}

void build(int root,int l,int r)
{
    tree[root].l=l,tree[root].r=r,tree[root].mid=l+r>>1;
    if(l==r)
    {
        tree[root].sum=tree[root].maxn=tree[root].rmaxn=tree[root].lmaxn=read();
        return;
    }
    build(root<<1,l,tree[root].mid);
    build(root<<1|1,tree[root].mid+1,r);
    pushup(root);
}

void Modify(int root,int pos,int x)
{
    if(tree[root].l==tree[root].r&&tree[root].l==pos)
    {
        tree[root].lmaxn=tree[root].rmaxn=tree[root].maxn=tree[root].sum=x;
        return;
    }
    if(pos<=tree[root].mid)
        Modify(root<<1,pos,x);
    else
        Modify(root<<1|1,pos,x);
    pushup(root);
}

void Qeuery(int root,int l,int r,int &L,int &R,int &M,int &S)
{
    if(l<=tree[root].l&&tree[root].r<=r)
    {
        L=tree[root].lmaxn,
        R=tree[root].rmaxn,
        M=tree[root].maxn,
        S=tree[root].sum;
        return;
    }
    if(tree[root].mid>=r)
    {
        Qeuery(root<<1,l,r,L,R,M,S);
    }
    else if(tree[root].mid<l)
    {
        Qeuery(root<<1|1,l,r,L,R,M,S);
    }
    else
    {
        int lL=0,lR=0,lM=0,lS=0,rL=0,rR=0,rM=0,rS=0;
        Qeuery(root<<1,l,tree[root].mid,lL,lR,lM,lS);
        Qeuery(root<<1|1,tree[root].mid+1,r,rL,rR,rM,rS);
        L=max(lL,lS+rL);
        R=max(rR,rS+lR);
        M=max(lR+rL,max(lM,rM));
        S=lS+rS;
    }
}

int main()
{
    n=read();
    build(1,1,n);
    m=read();
    for(int i=1;i<=m;++i)
    {
        type=read();
        l=read(),r=read();
        if(!type)
            Modify(1,l,r);
        else
        {
            Qeuery(1,l,r,L,R,M,S);
            printf("%d
",M);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/lovewhy/p/8463574.html