GSS1 - Can you answer these queries I(线段树)
前言
线段树菜鸡报告,stO ZCDHJ Orz,GSS基本上都切完了。
Solution
考虑一下用线段树维护一段区间左边连续的Max,右边的连续Max,中间的连续Max还有总和,发现这些东西可以相互合并,然后直接写就好了。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi(){
int f=1,sum=0;char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
return f*sum;
}
const int N=50010;
int a[N];
struct node{
int sum,ls,rs,ts;
}t[N<<4];
void pushup(int o){
t[o].sum=t[o<<1].sum+t[o<<1|1].sum;
t[o].ts=max(t[o<<1].ts,max(t[o<<1|1].ts,t[o<<1].rs+t[o<<1|1].ls));
t[o].ls=max(t[o<<1].ls,t[o<<1].sum+t[o<<1|1].ls);
t[o].rs=max(t[o<<1|1].rs,t[o<<1].rs+t[o<<1|1].sum);
}
void build(int o,int l,int r){
if(l==r){t[o].ls=t[o].rs=t[o].ts=t[o].sum=a[l];return;}
int mid=(l+r)>>1;
build(o<<1,l,mid);build(o<<1|1,mid+1,r);
pushup(o);
}
node query(int o,int l,int r,int posl,int posr){
if(posl<=l && r<=posr)return t[o];
int mid=(l+r)>>1;
if(posl>mid)return query(o<<1|1,mid+1,r,posl,posr);
if(posr<=mid)return query(o<<1,l,mid,posl,posr);
else{
node ans,a,b;
a=query(o<<1,l,mid,posl,mid);b=query(o<<1|1,mid+1,r,mid+1,posr);
ans.sum=a.sum+b.sum;
ans.ts=max(a.ts,max(b.ts,a.rs+b.ls));
ans.ls=max(a.ls,a.sum+b.ls);
ans.rs=max(b.rs,a.rs+b.sum);
return ans;
}
}
int main(){
int n=gi();
for(int i=1;i<=n;i++)a[i]=gi();
build(1,1,n);int m=gi();
while(m--){
int l=gi(),r=gi();
printf("%d\n",query(1,1,n,l,r).ts);
}
return 0;
}
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 ——线段树
[题目分析] 线段树裸题. 注意update的操作,写结构体里好方便. 嗯,没了. [代码] #include <cstdio> #include <cstring> #inc ...
- SP1043 GSS1 - Can you answer these queries I 线段树
问题描述 LG-SP1043 题解 GSS 系列第一题. \(q\) 个询问,求 \([x,y]\) 的最大字段和. 线段树,维护 \([x,y]\) 的 \(lmax,rmax,sum,val\) ...
- 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(线段树区间合并)
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 ...
- GSS4 2713. Can you answer these queries IV 线段树
GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...
- 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[ ...
- 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 ...
- bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树
2482: [Spoj1557] Can you answer these queries II Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 145 ...
随机推荐
- 43-python-自己的词典
可以用python实现一个自己的词典, 就是在网上下一个英汉词典,作为自己的词库,然后整理出一个json文件,存起来,查词时,直接读取查询: 处理时可以用正则表达式处理: https://www.cn ...
- 17-js 提交表单以及判空
js
- 解决mysql无法远程登陆问题
解决这个问题的思路: 一.先确定能过3306端口 二.再检查授权Host是否存在 (新授权记得flush privileges;) 一 步骤 1.首先打开mysql的配置文件,找到这句话,注释掉. ...
- Pull to RefreshListView 添加HeaderView
使用listView.addHeaderView(view) 可以在 listView 上方添加一个view视图 ,使listView和这个view连接在一起 效果上看上去是一个整体 一般用于上拉刷新 ...
- 在iframe框架中全屏不好使的原因
遇到的问题:我是在iframe框架中添加了一个插件在360和火狐中不好使,将allowfullscreen="true" 属性配置好就没问题了: 可能出现的原因:将allowful ...
- Alluxio/Tachyon如何发挥lineage的作用?
在Spark的RDD中引入过lineage这一概念.指的是RDD之间的依赖.而Alluxio则使用lineage来表示文件之间的依赖.在代码层面,指的是fileID之间的依赖. 代码中的注释指出: * ...
- POJ 1122.FDNY to the Rescue! Dijkstra
FDNY to the Rescue! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2808 Accepted: 86 ...
- Python鸭子类型思想
动态语言中经常提到鸭子类型,所谓鸭子类型就是:如果走起路来像鸭子,叫起来也像鸭子,那么它就是鸭子(If it walks like a duck and quacks like a duck, it ...
- const与预处理宏#define的区别
在c语言程序设计时,预处理器可以不受限制地建立宏并用它来替代值.因为预处理器只做一些文本替换,宏没有类型检测概念,也没有类型检测功能.所以预处理器的值替换会出现一些小的问题,出现的这些问题,在c++中 ...
- BZOJ 1009 [HNOI2008]GT考试 (KMP + 矩阵快速幂)
1009: [HNOI2008]GT考试 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 4266 Solved: 2616[Submit][Statu ...