SP1043 GSS1 - Can you answer these queries I 线段树
问题描述
题解
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 线段树的更多相关文章
- 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]| ≤ ...
- 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 ...
- SPOJ GSS1 Can you answer these queries I ——线段树
[题目分析] 线段树裸题. 注意update的操作,写结构体里好方便. 嗯,没了. [代码] #include <cstdio> #include <cstring> #inc ...
- 线段树 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).查询定义 ...
- SPOJ GSS1_Can you answer these queries I(线段树区间合并)
SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...
- 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 ...
- 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[ ...
- GSS4 2713. Can you answer these queries IV 线段树
GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...
- 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 ...
随机推荐
- 【Java并发系列】--Java内存模型
Java内存模型 1 基本概念 程序:代码,完成某一个任务的代码序列(静态概念) 进程:程序在某些数据上的一次运行(动态) 线程:一个进程有一个或多个线程组成(占有资源的独立单元) 2 JVM与线程 ...
- 速查TARGET_OS关系/Apple xcode 宏定义
记法 : 次级等于上级 TARGET_OS_WIN32 - Generated code will run under 32-bit Windows TARGET_OS_UNIX - Generate ...
- 【Cocos谁学谁会】制作会跑动的地板
版权申明: 本文原创首发于以下网站,您可以自由转载,但必须加入完整的版权声明 博客园:https://www.cnblogs.com/MogooStudio/ csdn博客:https://blog. ...
- 有抱负的 DevOps 和 SRE 工程师必读好书清单 | 文末有福利!
原文地址:https://medium.com/faun/10-great-books-for-aspiring-devops-sre-engineers-76536c7c4909 原文作者:Ayme ...
- C++ 进阶笔记之一
优化相关 使用灵活的.动态分配的数据,不要使用固定大小多数组: 优先使用线性算法或者尽可能快的算法: push_back 散列表查询:O(1) set/map lower_bound/upper_bo ...
- [PHP] Workerman中的注册树模式
注册树模式是把对象挂到一个类的属性数组里,下次直接在这个数组里面取,保持全局唯一,一般在项目入口初始化的时候有用到.在workerman中一开始的就是个注册树模式的运用,下面是对他的模拟 <?p ...
- ORM查询(细致):
ORM查询(细致): 1.正向查找 ret1=model.Book.objects.first() print(ret1.title) print(ret1.price) print(ret1.pub ...
- mysql实践:sql优化
---恢复内容开始--- 设计表的时候 1. 不同的表涉及同一个公共意义字段不要使用不同的数据类型(可能导致索引不可用,查询结果有偏差) 2. 不要一张表放太多的数据 主表20~30个字段 ...
- java学习路线推荐,希望能帮到你
很多小白刚开始学习java时,肯定迷惘过,因为对java基本是啥也不懂的,一直想知道java的具体学习路线,我曾经也看了许许多多的java经验分享的帖子,评论,以及其他各种培训机构所谓的学习路线,特别 ...
- [专题总结]初探插头dp
彻彻底底写到自闭的一个专题. 就是大型分类讨论,压行+宏定义很有优势. 常用滚动数组+哈希表+位运算.当然还有轮廓线. Formula 1: 经过所有格子的哈密顿回路数. 每个非障碍点必须有且仅有2个 ...