zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    [置顶] 也论百度轻应用--一个开发者的吐槽
    HDU 1506 Largest Rectangle in a Histogram
    [置顶] 搭建apache+tomcat+memcached集群环境
    (step8.2.6)hdu 1848(Fibonacci again and again——组合博弈)
    SQL2005、2008、2000 清空删除日志
    网络获取北京时间和系统时间
    C++界面库
    Cocos2d-X游戏开发之Windows7+VS2010环境搭建(亲测)
    华为面试题——一道关于指针方面的编程题(C/C++)
    java之Math
  • 原文地址:https://www.cnblogs.com/lovewhy/p/8463574.html
Copyright © 2011-2022 走看看