zoukankan      html  css  js  c++  java
  • SP1043 GSS1

    问题描述

    LG-SP1043


    题解

    GSS 系列第一题。

    (q) 个询问,求 ([x,y]) 的最大字段和。

    线段树,维护 ([x,y])(lmax,rmax,sum,val) ,向上合并即可。

    但是注意询问过程中也需要维护这些信息。


    (mathrm{Code})

    #include<bits/stdc++.h>
    using namespace std;
    
    template <typename Tp>
    void read(Tp &x){
    	x=0;char ch=1;int fh;
    	while(ch!='-'&&(ch>'9'||ch<'0')) ch=getchar();
    	if(ch=='-') ch=getchar(),fh=-1;
    	else fh=1;
    	while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    	x*=fh;
    }
    
    const int maxn=50007;
    
    #define lfc (x<<1)
    #define rgc ((x<<1)|1)
    #define mid ((l+r)>>1)
    int n;
    int val[maxn<<2],lf[maxn<<2],rg[maxn<<2];
    int sum[maxn<<2];
    int a[maxn];
    
    void pushup(int x){
    	sum[x]=sum[lfc]+sum[rgc];
    	lf[x]=max(lf[lfc],sum[lfc]+lf[rgc]);
    	rg[x]=max(rg[rgc],sum[rgc]+rg[lfc]);
    	val[x]=max(max(val[lfc],val[rgc]),rg[lfc]+lf[rgc]);
    }
    
    void build(int x,int l,int r){
    	if(l==r){
    		sum[x]=val[x]=lf[x]=rg[x]=a[l];
    		return;
    	}
    	build(lfc,l,mid);build(rgc,mid+1,r);
    	pushup(x);
    }
    
    const int INF=0x3f3f3f3f;
    
    int L,R;
    
    struct node{
    	int val,lf,rg,sum;
    };
    
    node query(int x,int l,int r){
    	if(L<=l&&r<=R) return (node){val[x],lf[x],rg[x],sum[x]};
    	if(L>mid) return query(rgc,mid+1,r);
    	if(R<=mid) return query(lfc,l,mid);
    	node res,s1=query(lfc,l,mid),s2=query(rgc,mid+1,r);
    	res.sum=s1.sum+s2.sum;
    	res.val=max(max(s1.val,s2.val),s1.rg+s2.lf);
    	res.lf=max(s1.lf,s1.sum+s2.lf);
    	res.rg=max(s2.rg,s2.sum+s1.rg);
    	return res;
    }
    
    int main(){
    	read(n);
    	for(int i=1;i<=n;i++) read(a[i]);
    	build(1,1,n);
    	int T;read(T);
    	while(T--){
    		read(L);read(R);
    		printf("%d
    ",query(1,1,n).val);
    	}
    	return 0;
    }
    
  • 相关阅读:
    Flask目录结构
    RHSA-2019:1880-低危: curl 安全和BUG修复更新 及 RHSA-2019:1884-中危: libssh2 安全更新
    ELK+Logback进行业务日志分析查看
    Maven编译过程中出现的问题
    Zabbix监控服务器磁盘I/O
    创建readonly只读用户脚本
    Zabbix监控多个JVM进程
    redis命令
    docker配置Nginx
    docker基本命令
  • 原文地址:https://www.cnblogs.com/liubainian/p/11775114.html
Copyright © 2011-2022 走看看