SPOJ 1043 Can you answer these queries I 求任意区间最大连续子段和 线段树

题目链接:点击打开链接

维护区间左起连续的最大和,右起连续的和。。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <vector>
#include <map>
using namespace std;
#define N 50050
#define Lson(x) tree[x].l
#define Rson(x) tree[x].r
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define Sum(x) tree[x].sum
#define Max(x) tree[x].max
#define Lmax(x) tree[x].lmax
#define Rmax(x) tree[x].rmax
struct node{
	int l, r;
	int mid(){return (l+r)>>1;}
	int lmax, rmax, max, sum;
}tree[N<<4];
int n, a[N], Q;
void push_down(int id){}
void push_up(int id){
	Lmax(id) = max(Lmax(L(id)), Sum(L(id)) + Lmax(R(id)));
	Rmax(id) = max(Rmax(R(id)), Sum(R(id)) + Rmax(L(id)));
	Sum(id) = Sum(L(id)) + Sum(R(id));
	Max(id) = max(max(Max(L(id)), Max(R(id))), Rmax(L(id)) + Lmax(R(id)));
}
void updata_point(int val, int id){Lmax(id) = Rmax(id) = Max(id) = Sum(id) = val;}
void build(int l, int r, int id){
	Lson(id) = l; Rson(id) = r;
	if(l == r)
	{
		updata_point(a[l], id);
		return;
	}
	int mid = tree[id].mid();
	build(l, mid, L(id));
	build(mid+1, r, R(id));
	push_up(id);
}
void updata(int pos, int val, int id){
	push_down(id);
	if(Lson(id) == Rson(id))
	{
		updata_point(val, id);
		return ;
	}
	int mid = tree[id].mid();
	if(mid < pos)
		updata(pos, val, R(id));
	else
		updata(pos, val, L(id));
}
int query_l(int l, int r, int id){
	push_down(id);
	if(l == Lson(id) && Rson(id) == r) return Lmax(id);
	int mid = tree[id].mid();
	if(mid < l)
		return query_l(l, r, R(id));
	else if(r <= mid)
		return query_l(l, r, L(id));
	int lans = query_l(l, mid, L(id)), rans = query_l(mid+1, r, R(id));
	return max(lans, Sum(L(id)) + rans);
}
int query_r(int l, int r, int id){
	push_down(id);
	if(l == Lson(id) && Rson(id) == r) return Rmax(id);
	int mid = tree[id].mid();
	if(mid < l)
		return query_r(l, r, R(id));
	else if(r <= mid)
		return query_r(l, r, L(id));
	int lans = query_r(l, mid, L(id)), rans = query_r(mid+1, r, R(id));
	return max(rans, Sum(R(id)) + lans);
}
int query(int l, int r, int id){
	push_down(id);
	if(l == Lson(id) && Rson(id) == r)return Max(id);
	int mid = tree[id].mid();
	if(mid < l)
		return query(l, r, R(id));
	else if(r<=mid)
		return query(l, r, L(id));
	int lans = query(l, mid, L(id)), rans = query(mid+1, r, R(id));
	int ans = max(lans, rans);
	return max(ans, query_r(l, mid, L(id)) + query_l(mid+1, r, R(id)));
}
int main(){
	while(~scanf("%d",&n)){
		for(int i = 1; i <= n; i++)scanf("%d",&a[i]);
		build(1, n, 1);
		scanf("%d",&Q);
		while(Q--){
			int l, r;
			scanf("%d %d",&l,&r);
			printf("%d\n", query(l, r, 1));
		}
	}
	return 0;
}
/*
3
-1 2 3
1
1 2

*/

SPOJ 1043 Can you answer these queries I 求任意区间最大连续子段和 线段树,布布扣,bubuko.com

时间: 2024-09-29 17:00:32

SPOJ 1043 Can you answer these queries I 求任意区间最大连续子段和 线段树的相关文章

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/problems/GSS2/ Description Being a completist and a simplist, kid Yang Zhe cannot solve but get Wrong Answer from most of the OI problems. And he refuse

SPOJ 4487. Can you answer these queries VI splay

题目链接:点击打开链接 题意比较明显,不赘述. 删除时可以把i-1转到根,把i+1转到根下 则i点就在 根右子树 的左子树,且只有i这一个 点 #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> using namespace std; #define N 300500 #define inf 10000000 #define L(x) tree[x].ch[

SPOJ GSS3 Can you answer these queries III (线段树)

题目大意: 求区间最大子区间的和. 思路分析: 记录左最大,右最大,区间最大. 注意Q_L  和 Q_R  就好. #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #define lson num<<1,s,mid #define rson num<<1|1,mid+1,e #define maxn 55555 using na

SPOJ GSS4 Can you answer these queries IV (线段树)

题目大意: 给出N个数 0     操作   把 l -----  r之间的数全部开平方 1     操作  输出 l -----r  之间的和 思路分析: 判断区间里的数字是否全相同.如果相同, 将cov 置为该数 查询的时候和更新的时候,如果碰到cov != -1 的  就直接返回就可以了 #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #incl

spoj gss2 : Can you answer these queries II 离线&amp;&amp;线段树

1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang Zhe cannot solve but get Wrong Answer from most of the OI problems. And he refuse to write two program of same kind at all. So he always failes in co

SPOJ GSS5 Can you answer these queries V (线段树)

原来有一两个人说我不帅的时候,我不以为意,逗我玩而已,后来几乎所有 人都说我不帅,我才真正意识到事态的严重,这社会骗子真是越来越多了... 好吧我承认,这个笑话不好笑,其实我想说的是,做人一定要坚持自己的原则, 哪怕有一天所有人都和你背道而驰,都不要放弃自己当初的梦想,如果有一天, 我们淹没在人海之中,庸碌一生,那是因为我们不够努力,不够勇敢的去面对生活. 每天积累一点点,嗯,满足简单的快乐. ---------------------------------------------------

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  Solved: 76[Submit][Status][Discuss] Description 给定n个元素的序列. 给出m个询问:求l[i]~r[i]的最大子段和(可选空子段). 这个最大子段和有点特殊:一个数字在一段中出现了两次只算一次. 比如:1,2,3,2,2,2出现了3次,但只算一次,

SPOJ GSS4 Can you answer these queries IV

Can you answer these queries IV Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Original ID: GSS464-bit integer IO format: %lld      Java class name: Main You are given a sequence A of N(N <= 100,000) positive integers.

SPOJ GSS1 Can you answer these queries I

Can you answer these queries I Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Original ID: GSS164-bit integer IO format: %lld      Java class name: Main You are given a sequence $A[1], A[2], ..., A[N] $. $( |A[i]| \leq