zoukankan      html  css  js  c++  java
  • GSS1 A

    //题意:询问一段区间的最大子序列的值。 
    
    //做法:维护四个值:包含当前区间左端点的最大子区间LM,包含当前区间右端点的最大子区间RM、当前区间的最大子区间M, 当前区间的区间和S 
    //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;
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    
    const int N=5e4+5;
    
    int n,m,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 query(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)
        {
            query(root<<1,l,r,L,R,M,S);
        }
        else if(tree[root].mid<l)
        {
            query(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;
            query(root<<1,l,tree[root].mid,lL,lR,lM,lS);
            query(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)
        {
            l=read(),r=read();
            query(1,l,r,L,R,M,S);
            printf("%d
    ",M);
        }
        return 0;
    }
  • 相关阅读:
    LeetCode_326. Power of Three
    LeetCode_303. Range Sum Query
    LeetCode_299. Bulls and Cows
    LeetCode_292. Nim Game
    LeetCode_290. Word Pattern
    LeetCode_283. Move Zeroes
    LeetCode_278. First Bad Version
    LeetCode_268. Missing Number
    LeetCode_263. Ugly Number
    LeetCode_258. Add Digits
  • 原文地址:https://www.cnblogs.com/lovewhy/p/8463559.html
Copyright © 2011-2022 走看看