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 ...
随机推荐
- Angular JS 中 ng-controller 值复制和引用复制
我们知道在使用ng-app或者ng-controller指令的时候,都会创建一个新的作用域($rootScope或者是$scope),并且在使用ng-controller指令创建的作用域会继承父级作用 ...
- Shiro权限框架与SpringMVC集成
1.Shiro整合SpringMVC 我们学习Shiro框架肯定是要应用到Web项目上的,所以我们需要整合Shiro和SpringMVC 整合步骤: 第一步:SpringMVC框架的配置 spring ...
- 详解 git 忽略文件 删除远端仓库的文件
要解决的问题 忽略指定类型的文件 或 某个指定文件(夹) 将已经push到github的文件删除, 但本地的文件不删除 (写忽略规则之前就把这个文件夹push了 T_T ) 将想要忽略掉的文件的相关记 ...
- 优雅的对象转换解决方案-MapStruct及其入门(一)
第一次看到 MapStruct 的时候, 我个人非常的开心. 因为其跟我内心里面的想法不谋而合. 1 MapStruct 是什么? 1.1 JavaBean 的困扰 对于代码中 JavaBean之间的 ...
- Go中的结构体
前面我们或多或少的都使用了结构体这种数据结构,本身结构体也有很多特性,我们一一来看. 结构体的作用是将一个或者多个任一类型的变量组合在一起的数据类型,类似于我们在Java中class的作用.在结构体重 ...
- HTML/CSS:图片居中(水平居中和垂直居中)
css图片居中(水平居中和垂直居中) css图片居中分css图片水平居中和垂直居中两种情况,有时候还需要图片同时水平垂直居中, 下面分几种居中情况分别介绍: css图片水平居中 1.利用margin: ...
- Java——字符串
1.不可变的String String对象是不可变的.String类中的每一个看起来会修改String值的方法,实际上都是创建了一个全新的String对象,以包含修改修改后的字符串内容. public ...
- JSP前端数据本地排序
在前端中我们经常需要数据的排序,首先写引入我写好的js $(function($) { $('#sclazzId').val($('#voId').val()); document.getElemen ...
- .netcore持续集成测试篇之Xunit结合netcore内存服务器发送post请求
系列目录 Web项目中,很多与用户数据交互的请求都是Post请求,想必大家都用过HttpClient构造过post请求,这里并不对HttpClient做详细介绍,只介绍一些常用的功能.并结合AutoF ...
- abap简单实现form递归
需求:根据物料号查询下层物料清单 DATA LV_MATNR LIKE ZMARA_TEST-MATNR VALUE '000000000000000001'. DATA: LT_MAT LIKE T ...