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<<1].v + p[rt<<1|1].v; p[rt].lv = max(p[rt<<1].lv, p[rt<<1].v + p[rt<<1|1].lv); p[rt].rv = max(p[rt<<1|1].rv, p[rt<<1|1].v + p[rt<<1].rv); p[rt].ans = max3(p[rt<<1].ans, p[rt<<1|1].ans, p[rt<<1].rv + p[rt<<1|1].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 << " "; #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 ' ' #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; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9+7; const double esp = 1e-8; const double PI=acos(-1.0); template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } /*-----------------------showtime----------------------*/ const int maxn = 50009; int a[maxn]; struct node { int lv,rv,v; int ans; node(){ lv = rv = v = ans = -inf; } }p[maxn<<2]; void pushup(int rt){ p[rt].v = p[rt<<1].v + p[rt<<1|1].v; p[rt].lv = max(p[rt<<1].lv, p[rt<<1].v + p[rt<<1|1].lv); p[rt].rv = max(p[rt<<1|1].rv, p[rt<<1|1].v + p[rt<<1].rv); p[rt].ans = max3(p[rt<<1].ans, p[rt<<1|1].ans, p[rt<<1].rv + p[rt<<1|1].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)>>1; build(l,mid,rt<<1); build(mid+1,r,rt<<1|1); 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)>>1; node t1,t2,res; t1.ans = -inf,t2.ans = -inf; if(mid >= L)t1 = query(l,mid,rt<<1,L,R); if(mid < R) t2 = query(mid+1,r,rt<<1|1,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=1; i<=n; i++){ scanf("%d", &a[i]); } build(1,n,1); scanf("%d", &m); while(m--){ int l,r; scanf("%d%d", &l, &r); printf("%d ", query(1,n,1,l,r).ans); } return 0; }