问题描述

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. Java生鲜电商平台-生鲜电商中商品类目、属性、品牌、单位架构设计与实战

    Java生鲜电商平台-生鲜电商中商品类目.属性.品牌.单位架构设计与实战 说明:Java生鲜电商平台-生鲜电商中商品类目.属性.品牌.单位架构设计与实战经验分享 凡是涉及到购物,必然是建立在商品的基础 ...

  2. 如何修改CAD字体颜色?试试这种方法

    CAD中编辑图纸的时候,使用的CAD制图软件来进行绘制,图纸中的CAD字体颜色都是默认的颜色,这样不方便进行查看.这个时候就需要修改CAD字体颜色了,那么如何修改CAD字体颜色呢?具体要怎么来进行操作 ...

  3. 性能调优 -- Java编程中的性能优化

    String作为我们使用最频繁的一种对象类型,其性能问题是最容易被忽略的.作为Java中重要的数据类型,是内存中占据空间比较大的一个对象.如何高效地使用字符串,可以帮助我们提升系统的整体性能. 现在, ...

  4. How to use special characters in XML?

    https://dvteclipse.com/documentation/svlinter/How_to_use_special_characters_in_XML.3F.html Because X ...

  5. CODING 2.0 企业级持续交付解决方案

    "The key to DevOps transformation is that there is no end-state-we must continuously evolve.&qu ...

  6. log file sync等待超高案例浅析

    监控工具DPA发现海外一台Oracle数据库服务器DB Commit Time指标告警,超过红色告警线(40毫秒左右,黄色告警是10毫秒,红色告警线是20毫秒),如下截图所示,生成了对应的时段的AWR ...

  7. 用iText5-1-生成PDF

    参考代码和图片出处 https://howtodoinjava.com/library/read-generate-pdf-java-itext/ pom引入jar包 <dependencies ...

  8. 构造函数new运算符进行了哪些操作

    new 运算符 1,实例化一个对象 2,将构造函数prototype对象赋值给对象__proto__属性 3,将对象作为函数this传进去,函数有return 并且是对象的话,就直接返回return的 ...

  9. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

  10. router-view 与 动态组件 区别

    提问:router-view 可以页面跳转,使用 is特性 也可以进行页面跳转,有什么区别? 参考链接 https://segmentfault.com/q/1010000010750059