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 ...
随机推荐
- vue 初始化table数据,数据闪现的问题
使用的iview,很简单的一个table,可以扩展显示,我这里则是更改了一下,显示的也是表格,内容为明细数据. 原以为很简单的可以直接调用方法,进行数据的渲染,但是没想到,数据只是一闪而过. 百思不得 ...
- 在Linux - Centos上安装Python3(上)
必看内容 在Linux上安装Python常用的2种方法 1.Python源码编译安装,有点复杂,适合老司机 2.从EPEL/IUS仓库安装,新手建议使用些方法,比较简单,目前2019-07-31提供最 ...
- 【Android Studio】E/memtrack: Couldn't load memtrack module (No such file or directory)【待解决】
Android Studio 又遇到了问题--如下: 06-21 07:27:57.855 3232-3232/? E/memtrack: Couldn't load memtrack module ...
- interceptor拦截器
fifter.servlet.interceptor fifter用来处理请求头.请求参数.编码的一些设置,然后转交给servlet,处理业务,返回 servlet现在常用的spring,servle ...
- H3C模拟器单臂路由配置实例
单臂路由:用802.1Q和子接口实现VLAN间路由 单臂路由利用trunk链路允许多个VLAN的数据帧通过而实现 网络拓扑图: RTA配置: SW1配置: PC1/2配置如图: 但是值得注意的是,在配 ...
- (数据科学学习手札66)在ubuntu服务器上部署shiny
一.简介 shiny是R中专门用于开发轻量级web应用的框架,在本地写一个shiny应用并调用非常方便,但如果你希望你的shiny应用能够以远程的方式提供给更多人来使用,就需要将写好的shiny应用部 ...
- Spark 系列(五)—— Spark 运行模式与作业提交
一.作业提交 1.1 spark-submit Spark 所有模式均使用 spark-submit 命令提交作业,其格式如下: ./bin/spark-submit \ --class <ma ...
- n的阶乘 -牛客
题目描述 输入一个整数n,输出n的阶乘(每组测试用例可能包含多组数据,请注意处理) 输入描述: 一个整数n(1<=n<=20) 输出描述: n的阶乘 解题思路 采用递归求解,也可以使用循环 ...
- 多线程 共享资源 同步锁 java
Java多线程编程:Lock synchronized是java中的一个关键字,也就是说是Java语言内置的特性.那么为什么会出现Lock呢? 如果一个代码块被synchronized修饰了,当一 ...
- CodeForces 526D Om Nom and Necklace
洛谷题目页面传送门 & CodeForces题目页面传送门 给定字符串\(a\),求它的每一个前缀,是否能被表示成\(m+1\)个字符串\(A\)和\(m\)个字符串\(B\)交错相连的形式, ...