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个查询,程序必须输出这些查询的结果。
这就是一个最大子段和,用线段树就能直接搞掉
然后这里学习了一下一个叫做猫树的神奇东西->这里
能做到预处理之后查询$O(1)$
//minamoto
#include<iostream>
#include<cstdio>
using namespace std;
#define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[<<],*p1=buf,*p2=buf;
int read(){
#define num ch-'0'
char ch;bool flag=;int res;
while(!isdigit(ch=getc()))
(ch=='-')&&(flag=true);
for(res=num;isdigit(ch=getc());res=res*+num);
(flag)&&(res=-res);
#undef num
return res;
}
char sr[<<],z[];int C=-,Z;
inline void Ot(){fwrite(sr,,C+,stdout),C=-;}
void print(int x){
if(C><<)Ot();if(x<)sr[++C]=,x=-x;
while(z[++Z]=x%+,x/=);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
const int N=5e+;
int n,m,a[N],len,log[N],pos[N],p[][N],s[][N];
void build(int pp,int l,int r,int d){
if(l==r) return (void)(pos[l]=pp);
int mid=(l+r)>>,prep,sm;
p[d][mid]=s[d][mid]=sm=prep=a[mid];
if(sm<) sm=;
for(int i=mid-;i>=l;--i){
prep+=a[i],sm+=a[i];
s[d][i]=max(s[d][i+],prep),
p[d][i]=max(p[d][i+],sm);
if(sm<) sm=;
}
p[d][mid+]=s[d][mid+]=sm=prep=a[mid+];
if(sm<) sm=;
for(int i=mid+;i<=r;++i){
prep+=a[i],sm+=a[i];
s[d][i]=max(s[d][i-],prep),
p[d][i]=max(p[d][i-],sm);
if(sm<) sm=;
}
build(pp<<,l,mid,d+);
build(pp<<|,mid+,r,d+);
}
int query(int l,int r){
if(l==r) return a[l];
int d=log[pos[l]]-log[pos[l]^pos[r]];
return max(max(p[d][l],p[d][r]),s[d][l]+s[d][r]);
}
int main(){
// freopen("testdata.in","r",stdin);
n=read();for(int i=;i<=n;++i) a[i]=read();
len=;while(len<n) len<<=;
for(int i=,l=len<<;i<=l;++i) log[i]=log[i>>]+;
build(,,len,);
m=read();
while(m--){
int l=read(),r=read();
print(query(l,r));
}
return Ot(),;
}
SP1043 GSS1 - Can you answer these queries I(猫树)的更多相关文章
- 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
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(线段树维护GSS)
Can you answer these queries I SPOJ - GSS1 You are given a sequence A[1], A[2], -, A[N] . ( |A[i]| ≤ ...
- [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} ...
- 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 ...
- [题解] SPOJ GSS1 - Can you answer these queries I
[题解] SPOJ GSS1 - Can you answer these queries I · 题目大意 要求维护一段长度为 \(n\) 的静态序列的区间最大子段和. 有 \(m\) 次询问,每次 ...
- SPOJ GSS1_Can you answer these queries I(线段树区间合并)
SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...
随机推荐
- 整体二分初识--POJ2104:K-th Number
n<=100000个数有m<=5000个询问,每次问区间第k大. 方法一:主席树!…… 方法二:整体二分. 整体二分一次性计算半个值域对一个区间的询问的贡献,然后根据“这半边的贡献在某个询 ...
- spring history &design Philosophy
Spring简介 Spring是一个开源框架,它由Rod Johnson创建.它是为了解决企业应用开发的复杂性而创建的.Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情.然而, ...
- 打开Eclipse出现“An internal error has occurred. java.lang.NullPointerException
今天打开eclipse出现了这个问题:下面是老外给出的办法,粘给大家: Instead of deleting the whole .metadata folder, just delete the ...
- Ubuntu16.04安装deb文件时提示:此软件来自第三方且可能包含非自由组件
解决方式: 1.在Ubuntu软件中心安装GDebi. 2.安装好之后,选择这个要安装的deb文件右键,打开方式选择GDebi,然后输入管理员密码等待安装,期间如果不行需要重试几次. 3.另外的方法, ...
- Eclipse配色方案插件 真漂亮!
原文:https://my.oschina.net/jean/blog/208263 最近发现了一个Eclipse配色方案插件,这回给Eclipse配色太方便了. 插件主页:http://eclips ...
- dubbo的jmeter压测时jar包的热加载/动态加载
在做dubbo的jmeter压测时,需要把jar包放入jmeter的lib/ext目录下,但是jmeter启动的时候会自动加载这个目录lib目录及lib/ext目录,这样启动后放入这些目录下的jar包 ...
- symfony could not load type 'datetime'
当用curd生成控制器后,当修改的时候,会有这个提示,解决方法 在orm中通过事务的方式填充时间,然后把生成的form中的文件的时间段去掉 $builder ->add('title') -&g ...
- Elasticsearch学习系列之多文档操作mget
测试数据 GET /library/books/1 { "_index": "library", "_type": "books& ...
- [Sqlite3] Sqlite Introduction
Check whether you have sqlite3 installed: sqlite3 -version To create a new db: sqlite3 <filename. ...
- HDU 4950 Monster(公式)
HDU 4950 Monster 题目链接 题意:给定怪兽血量h,你攻击力a.怪物回血力b,你攻击k次要歇息一次,问是否能杀死怪兽 思路:签到题,注意最后一下假设打死了怪,那么怪就不会回血了 思路: ...