给出了序列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(猫树)的更多相关文章

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

    问题描述 LG-SP1043 题解 GSS 系列第一题. \(q\) 个询问,求 \([x,y]\) 的最大字段和. 线段树,维护 \([x,y]\) 的 \(lmax,rmax,sum,val\) ...

  2. 线段树 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).查询定义 ...

  3. 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]| ≤ ...

  4. [SP1043] GSS1 - Can you answer these queries I

    传送门:>Here< 题意:求区间最大子段和 $N \leq 50000$ 包括多组询问(不需要支持修改) 解题思路 线段树的一道好题 我们可以考虑,如果一组数据全部都是正数,那么问题等同 ...

  5. 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} ...

  6. 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 ...

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

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

  8. [题解] SPOJ GSS1 - Can you answer these queries I

    [题解] SPOJ GSS1 - Can you answer these queries I · 题目大意 要求维护一段长度为 \(n\) 的静态序列的区间最大子段和. 有 \(m\) 次询问,每次 ...

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

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

随机推荐

  1. 动态规划之最长公共子序列(LCS)

             在字符串S中按照其先后顺序依次取出若干个字符,并讲它们排列成一个新的字符串,这个字符串就被称为原字符串的子串          有两个字符串S1和S2,求一个最长公共子串,即求字符串 ...

  2. CSUOJ 1542 线段树解决括号反向问题

    题目大意: 根据初始给定的合法的小括号排序,每次进行一个操作,将第a位的括号反向,找到一个尽可能靠前的括号反向后是整个括号排列合法 数据量十分大,不断进行查询,要用线段树进行logn的复杂度的查询 首 ...

  3. [luoguP3390]【模板】矩阵快速幂

    传送门 模板不解释. ——代码 #include <cstdio> #include <cstring> #define LL long long int n; LL k; ; ...

  4. bzoj 1962 硬币游戏 (猜数问题)

    [bzoj1962]模型王子 2015年3月26日1,6460 Description Input 输入数据共一行,两个整数N,K,用一个空格隔开,具体意义如题目中所述. Output 输出数据共一行 ...

  5. 【springmvc】传值的几种方式&&postman接口测试

    最近在用postman测试postman接口,对于springmvc传值这一块,测试了几种常用方式,总结一下.对于postman这个工具的使用也增加了了解.postman测试很棒,有了工具,测试接口, ...

  6. 解方程(codevs 3732)

    题目描述 已知多项式方程: a0+a1x+a2x^2+..+anx^n=0 求这个方程在[1, m ] 内的整数解(n 和m 均为正整数) 输入输出格式 输入格式: 输入文件名为equation .i ...

  7. PHP $_SERVER的使用

    常常会用到php的$_SERVER变量,可是好多常用的参数又不熟每次都去查手册.为了记住一些常用的,写个日志吧.前导:网站根目录:/www/domain.com/访问Url:http://www.do ...

  8. codevs3285 转圈游戏

    题目描述 Description n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 ...

  9. Linux下汇编语言学习笔记55 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  10. POJ2632 Crashing Robots 解题报告

    Description In a modernized warehouse, robots are used to fetch the goods. Careful planning is neede ...