SP1043 GSS1
简单说就是带修的查询区间最大子段和,用线段树维护即可
对于每个区间,我们肯定要记录它的最大子段和\(v\),但是怎么维护呢?
我们可以记录下从区间左端点开始的最大子段和\(v1\),从右端点开始的最大子段和\(v2\)以及区间和\(sum\)
那么\(t[p].sum=t[lc].sum+t[rc].sum\)
\(t[p].v1=max(t[lc].v1,t[lc].sum+t[rc].v1)\)
\(t[p].v2=max(t[rc].v 2,t[rc].sum+t[lc].v2)\)
\(t[p].v=max(t[lc].v2+t[rc].v1,t[lc].v,t[rc].v)\)
正确性应该很显然不需要再给出证明了吧
下放代码
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cctype>
#define ll long long
#define gc getchar
#define maxn 50005
using namespace std;
inline ll read(){
ll a=0;int f=0;char p=gc();
while(!isdigit(p)){f|=p=='-';p=gc();}
while(isdigit(p)){a=(a<<3)+(a<<1)+(p^48);p=gc();}
return f?-a:a;
}int n,m;
struct ahaha{
int v,v1,v2,sum;
}t[maxn<<2];
#define lc p<<1
#define rc p<<1|1
inline void pushup(int p){
t[p].sum=t[lc].sum+t[rc].sum;
t[p].v1=max(t[lc].v1,t[lc].sum+t[rc].v1);
t[p].v2=max(t[rc].v2,t[rc].sum+t[lc].v2);
t[p].v=max(t[lc].v2+t[rc].v1,max(t[lc].v,t[rc].v));
}
void build(int p,int l,int r){
if(l==r){t[p].v=t[p].v1=t[p].v2=t[p].sum=read();return;}
int m=l+r>>1;
build(lc,l,m);build(rc,m+1,r);
pushup(p);
}
ahaha query(int p,int l,int r,int L,int R){
if(l>R||r<L)return {-214748,-214748,-214748,-214748};
if(L<=l&&r<=R)return t[p];
int m=l+r>>1;
ahaha l1=query(lc,l,m,L,R),r1=query(rc,m+1,r,L,R),i;
i.sum=l1.sum+r1.sum;
i.v1=max(l1.v1,l1.sum+r1.v1);
i.v2=max(r1.v2,r1.sum+l1.v2);
i.v=max(l1.v2+r1.v1,max(l1.v,r1.v));
return i;
}
inline void solve(){
int x=read(),y=read();
printf("%d\n",query(1,1,n,x,y).v);
}
int main(){
n=read();
build(1,1,n);
m=read();
while(m--)
solve();
return 0;
}
SP1043 GSS1的更多相关文章
- 线段树 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).查询定义 ...
- 解题报告:SP1043 GSS1
题目链接:SP1043 GSS1 - Can you answer these queries I 对,\(GSS\)毒瘤数据结构题,就是我在这篇文章中提到的紫题. 相对其他\(GSS\)题简单些,但 ...
- [SP1043] GSS1 - Can you answer these queries I
传送门:>Here< 题意:求区间最大子段和 $N \leq 50000$ 包括多组询问(不需要支持修改) 解题思路 线段树的一道好题 我们可以考虑,如果一组数据全部都是正数,那么问题等同 ...
- SP1043 GSS1 - Can you answer these queries I(线段树,区间最大子段和(静态))
题目描述 给出了序列A[1],A[2],…,A[N]. (a[i]≤15007,1≤N≤50000).查询定义如下: 查询(x,y)=max{a[i]+a[i+1]+...+a[j]:x≤i≤j≤y} ...
- SP1043 GSS1 - Can you answer these queries I(猫树)
给出了序列A[1],A[2],…,A[N]. (a[i]≤15007,1≤N≤50000).查询定义如下: 查询(x,y)=max{a[i]+a[i+1]+...+a[j]:x≤i≤j≤y}. 给定M ...
- SP1043 GSS1 - Can you answer these queries I 线段树
问题描述 LG-SP1043 题解 GSS 系列第一题. \(q\) 个询问,求 \([x,y]\) 的最大字段和. 线段树,维护 \([x,y]\) 的 \(lmax,rmax,sum,val\) ...
- 线段树【SP1043】GSS1 - Can you answer these queries I
Description 给出了序列\(A_1,A_2,-,A_n\). \(a_i \leq 15007,1 \leq n \leq 50000\).查询定义如下: 查询\((x,y)=max{a_i ...
- 题解【SP1043】 GSS1 - Can you answer these queries I
题目描述 You are given a sequence \(A_1, A_2, ..., A_n(|A_i|≤15007,1≤N≤50000)\). A query is defined as f ...
- 「SP1043」GSS1 - Can you answer these queries I
传送门 Luogu 解题思路 这题就是 GSS3 的一个退化版,不带修改操作的区间最大子段和,没什么好讲的. 细节注意事项 咕咕咕 参考代码 #include <algorithm> #i ...
随机推荐
- Theano3.6-练习之消噪自动编码器
来自:http://deeplearning.net/tutorial/dA.html#daa Denoising Autoencoders (dA) note:该部分假设读者已经看过(Theano3 ...
- 基于bootstrap的multiple-select下拉控件使用
multiple-select是一款优秀的下拉菜单控件,能够支持单选和多选. 详细参考文档: JS组件系列——两种bootstrap multiselect组件大比拼 multiple-select ...
- mysql自定义函数并在存储过程中调用,生成一千万条数据
mysql 自定义函数,生成 n 个字符长度的随机字符串 -- sql function delimiter $$ create function rand_str(n int) returns VA ...
- Newtonsoft.Json.Linq对象读取DataSet数据
Newtonsoft.Json.Linq对象读取DataSet数据: private void button4_Click(object sender, EventArgs e) { ...
- 64位RHEL5系统上运行yum出现"This system is not registered with RHN”的解决方法
在红帽EL5上运行yum,提示“This system is not registered with RHN”,意思是没有在官网上注册,不能下载RH的软件包,替代方案是采用centos源. 1.卸载r ...
- 20155238 2016-2017-2 《JAVA程序设计》第九周学习总结
教材学习内容总结 第十六章 JDBC SQL的解决方案是JDBC,在Java中,JDBC API主要用来存取数据库. *JDBC API是一个Java API,可以访问任何类型表列数据,特别是存储在关 ...
- Ueditor使用笔记
富文本编辑器在javaweb项目中还是比较常见的,如:ckeditor.kindeditor.ueditor等.今天主要叙述的对象为ueditor,它属于百度的.闲话不多说,下面开始介 ...
- Android开发——进程间通信之Bundle和文件
0. 前言 不论是Android还是其他操作系统,都会有自己的IPC机制,所谓IPC(Inter-Process Communication)即进程间通信.首先线程和进程是很不同的概念,线程是CPU ...
- POJ3278&&1426&&3126&&3087&&3414
接上次的,标签是BFS专题 其实无论是Deepth还是Breadth都是Search 3278 又是撸过的题目 1426 找一个十进制数(我刚开始看样例以为是二进制数),使得它每一位上都是1或0,且是 ...
- MFC CHotKeyCtrl控件
知识点: CHotKeyCtrl控件 获取热键数据 注册热键 响应热键事件 一.CHotKeyCtrl控件 void SetHotKey( WORD wVirtualKeyCode, WORD wMo ...