SPOJ - GSS1-Can you answer these queries I 线段树维护区间连续和最大值
SPOJ - GSS1:https://vjudge.net/problem/SPOJ-GSS1
参考:http://www.cnblogs.com/shanyr/p/5710152.html?utm_source=itdadao&utm_medium=referral
题意:
给定一个数列,很多次询问,问某个区间中最大的连续和是多少。
思路
线段树,每个线段树的节点要维护对应区间的最大值ans,与左端点相连的最大值lv,与右端点相连的最大值rv,还有区间全部的总和V;
这个V用在pushup中。
这个pushup的操作是:
void pushup(int rt){
p[rt].v = p[rt<<].v + p[rt<<|].v;
p[rt].lv = max(p[rt<<].lv, p[rt<<].v + p[rt<<|].lv);
p[rt].rv = max(p[rt<<|].rv, p[rt<<|].v + p[rt<<].rv);
p[rt].ans = max3(p[rt<<].ans, p[rt<<|].ans, p[rt<<].rv + p[rt<<|].lv);
}
每个区间的lv 就是 (左子区间的lv ,左子区间v + 右子区间的lv) 中的较大者。rv同理。
每个区间的ans就是,(左子区间的ans,右子区间的ans, 左子区间和右子区间中间连接的那段)中的较大者。
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue
#define max3(a,b,c) max(max(a,b),c) typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0); template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/
const int maxn = ;
int a[maxn];
struct node
{
int lv,rv,v;
int ans;
node(){
lv = rv = v = ans = -inf;
}
}p[maxn<<];
void pushup(int rt){
p[rt].v = p[rt<<].v + p[rt<<|].v;
p[rt].lv = max(p[rt<<].lv, p[rt<<].v + p[rt<<|].lv);
p[rt].rv = max(p[rt<<|].rv, p[rt<<|].v + p[rt<<].rv);
p[rt].ans = max3(p[rt<<].ans, p[rt<<|].ans, p[rt<<].rv + p[rt<<|].lv);
}
void build(int l,int r,int rt){
if(l==r){
p[rt].v = p[rt].lv = p[rt].rv = p[rt].ans = a[l];
return;
}
int mid = (l + r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
pushup(rt);
}
node query(int l,int r,int rt,int L,int R){
if(l>=L&&r<=R){
return p[rt];
}
int mid = (l + r)>>;
node t1,t2,res;
t1.ans = -inf,t2.ans = -inf;
if(mid >= L)t1 = query(l,mid,rt<<,L,R);
if(mid < R) t2 = query(mid+,r,rt<<|,L,R);
if(t1.ans!=-inf && t2.ans!=-inf){
res.lv = max(t1.lv, t1.v + t2.lv);
res.rv = max(t2.rv, t2.v + t1.rv);
res.v = t1.v + t2.v;
res.ans = max3(t1.ans, t2.ans, t1.rv + t2.lv);
}
else if(t1.ans!=-inf){
res = t1;
}
else if(t2.ans!=-inf){
res = t2;
}
return res;
}
int main(){
int n,m;
scanf("%d", &n);
for(int i=; i<=n; i++){
scanf("%d", &a[i]);
}
build(,n,);
scanf("%d", &m);
while(m--){
int l,r;
scanf("%d%d", &l, &r);
printf("%d\n", query(,n,,l,r).ans);
}
return ;
}
SPOJ - GSS1
SPOJ - 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 ...
- Spoj 1557 Can you answer these queries II 线段树 随意区间最大子段和 不反复数字
题目链接:点击打开链接 每一个点都是最大值,把一整个序列和都压缩在一个点里. 1.普通的区间求和就是维护2个值,区间和Sum和延迟标志Lazy 2.Old 是该区间里出现过最大的Sum, Oldlaz ...
- 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 ...
- 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 ...
- Can you answer these queries I SPOJ - GSS1 (线段树维护区间连续最大值/最大连续子段和)
You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ). A query is defi ...
- 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 ...
随机推荐
- Ubuntu 系统如何用pycharm开发python—OpenCV
- 什么?小程序实时语音识别你还在痛苦的对接科大讯飞?百度Ai识别?
前言 微信小程序,说不上大火,但是需求还是不少的.各大企业都想插一足 于是前端同学就有事情做了. 需求 我需要录音 我边说话边识别,我要同声传译,我要文字转语音,还要萝莉音 我:??? 正文 一开始, ...
- 10.Go-goroutine,waitgroup,互斥锁和channel
10.1.goroutine goroutine的使用 //Learn_Go/main.go package main import ( "fmt" "time" ...
- linux 配置ssh无密码登录不起作用的解决方案
1.安装ssh 直接 sudo apt-get install openssh-server 2.查看ssh运行状态 ps -e | grep ssh 如果发现 sshd 和 ssh-agent 即表 ...
- java学习-NIO(一)简介
I/O简介 在 Java 编程中,直到最近一直使用 流 的方式完成 I/O.所有 I/O 都被视为单个的字节的移动,通过一个称为 Stream 的对象一次移动一个字节.流 I/O 用于与外部世界接触. ...
- 冬天苹果笔记macbookpro消除静电的方法
冬天mac除静电的方法 1.mac虽然声称不需要关机,但和apple技术人员沟通后,还是需要1周进行一次关机操作 2.知乎上的回答:https://www.zhihu.com/question/195 ...
- Web开发中的相对路径和绝对路径
在学习HTML的时候一定会遇到引入文件和链接跳转页面,比如:JS文件.CSS文件.Image图片.我们就会考虑是相对路径和绝对路径的问题.下面PHP程序员雷雪松就详细讲解下Web开发中的相对路径和绝对 ...
- pikachu
题目链接:http://127.0.0.1/pikachu-master/vul/sqli/sqli_id.php 题目来源:pikachu-->SQL-inject-->数字型注入(po ...
- ASP.NET Core on K8S深入学习(3-2)DaemonSet与Job
本篇已加入<.NET Core on K8S学习实践系列文章索引>,可以点击查看更多容器化技术相关系列文章. 上一篇<3-1 Deployment>中介绍了Deployment ...
- javaScript基础-0 javascript概述
一.简介 javaScript一种面向web的编程语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早 ...