问题描述

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\n",query(1,1,n).val);
}
return 0;
}

SP1043 GSS1 - Can you answer these queries I 线段树的更多相关文章

  1. SPOJ GSS1 - Can you answer these queries I(线段树维护GSS)

    Can you answer these queries I SPOJ - GSS1 You are given a sequence A[1], A[2], -, A[N] . ( |A[i]| ≤ ...

  2. SPOJ GSS1 Can you answer these queries I[线段树]

    Description You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ). A q ...

  3. SPOJ GSS1 Can you answer these queries I ——线段树

    [题目分析] 线段树裸题. 注意update的操作,写结构体里好方便. 嗯,没了. [代码] #include <cstdio> #include <cstring> #inc ...

  4. 线段树 SP1043 GSS1 - Can you answer these queries I

    SP1043 GSS1 - Can you answer these queries I 题目描述 给出了序列A[1],A[2],-,A[N]. (a[i]≤15007,1≤N≤50000).查询定义 ...

  5. SPOJ GSS1_Can you answer these queries I(线段树区间合并)

    SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...

  6. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  7. GSS5 spoj 2916. Can you answer these queries V 线段树

    gss5 Can you answer these queries V 给出数列a1...an,询问时给出: Query(x1,y1,x2,y2) = Max { A[i]+A[i+1]+...+A[ ...

  8. GSS4 2713. Can you answer these queries IV 线段树

    GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...

  9. SPOJ 1557. Can you answer these queries II 线段树

    Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...

随机推荐

  1. 初识NLP 自然语言处理

    接下来的一段时间,要深入研究下自然语言处理这一个学科,以期能够带来工作上的提升. 学习如何实用python实现各种有关自然语言处理有关的事物,并了解一些有关自然语言处理的当下和新进的研究主题. NLP ...

  2. Add a Preview to a List View将预览添加到列表视图

    In this lesson, you will learn how to show a preview section in a List View's grid. For this purpose ...

  3. 为什么学习Python?

    互联网高速发展的今天,很荣幸能够接触计算机这个专业,成为一名程序猿小白,本科期间学习了C.C++.Java.Pthon.C#,每门语言都了解那么一点,学的很杂,刚好遇到大学毕业的这个暑期,在继续读研之 ...

  4. jqgrid addRowData报错

    今天再写项目的时候, 有一个手动添加行的功能,使用的是jqgrid的addRowData方法添加数据.但是在我们切换标签页的时候,再次添加行,调用这个方法的时候,报错了.错误信息如下 然后经过自己的反 ...

  5. iviewer插件

    jQuery-iviewer插件的使用 iviewer是一个具有缩放和图像旋转功能的图像查看小部件. 在jQuery官网下载后,有很多文件. 直接把文件夹解压拖到项目里. 然后再html中引入主要的文 ...

  6. jQuery随笔记录

            DOM遍历 parent()方法返回所选元素的直接父元素.(parent() 只能遍历单个级别的 DOM树) parents()方法获取所选元素的所有祖先 children()所选元素 ...

  7. JavaScript设计模式基础(一)

    模式的起源 模式 起源于建筑学.20世纪70年代,哈佛大学建筑学博士Christopher Alexander和他的团队花大约20年,来研究为解决同一个问题而设计出的不同建筑结构,从中发现那些高质量设 ...

  8. Oracle 11g与12c的审计详解

    最近遇到一些脚本诱发的审计相关BUG,感觉有必要重新梳理一下11g与12c的审计模式,于是根据官网修正了一下以前的一篇笔记这里发出来. 一.审计功能的开启: SQL> show paramete ...

  9. 数据库迁移导致Unknown character set: 'GBK' 应用异常

    https://blog.csdn.net/u013415591/article/details/82692242https://blog.csdn.net/gx_1_11_real/article/ ...

  10. Security+学习笔记

    第二章 风险分析 风险管理 评估:确定并评估系统中存在的风险 分析:分析风险对系统产生的潜在影响 响应:规划如何响应风险的策略 缓解: 缓解风险对未来安全造成的不良影响 风险分析流程 资产确定 漏洞确 ...