SPOJ 2916 Can you answer these queries V(线段树-分类讨论)
题目链接:http://www.spoj.com/problems/GSS5/
题意:给出一个数列。每次查询最大子段和Sum[i,j],其中i和j满足x1<=i<=y1,x2<=j<=y2,x1<=x2,y1<=y2。
思路:线段树的节点[L,R]保存LMax,RMax,Max,sum,表示左起最大值、右起最大值、区间最大值、区间数字和。更新比较简单。下面说查询。另外设置三个函数,可以查询任意区间[L,R]的最大值,以L开始向右最多到R的最大值、以R开始向左最多到L的最大值,设其分别为Q(L,R),QL(L,R),QR(L,R)。查询分两种情况讨论(设输入数组为d,S[i]表示d的前i项和):
(1)x2>y1: ans=QR(x1,y1)+S[x2-1]-S[y1]+QL(x2,y2);
(2)x2<=y1,那么有x1<=x2<=y1<=y2,则答案为下面三种情况的最大值:
a、x1<=i<=x2<=j<=y2,ans=QR(x1,x2)+QL(x2,y2)-d[x2];
b、x2<=i<=j<=y1,ans=Q(x2,y1);
c、x1<=i<=y1<=j<=y2,ans=QR(x1,y1)+QL(y1,y2)-d[y1]。
struct node
{
int LMax,RMax,Max,sum;
int L,R;
};
node a[N<<2];
int d[N],S[N];
int n;
void pushUp(int t)
{
if(a[t].L==a[t].R) return;
a[t].LMax=max(a[t*2].LMax,a[t*2].sum+a[t*2+1].LMax);
a[t].RMax=max(a[t*2+1].RMax,a[t*2+1].sum+a[t*2].RMax);
a[t].sum=a[t*2].sum+a[t*2+1].sum;
a[t].Max=max3(a[t*2].Max,a[t*2+1].Max,a[t*2].RMax+a[t*2+1].LMax);
}
void build(int t,int L,int R)
{
a[t].L=L;
a[t].R=R;
if(L==R)
{
a[t].sum=d[L];
a[t].LMax=a[t].RMax=a[t].Max=d[L];
return;
}
int mid=(L+R)>>1;
build(t*2,L,mid);
build(t*2+1,mid+1,R);
pushUp(t);
}
int queryL(int t,int L,int R)
{
if(a[t].L==L&&a[t].R==R) return a[t].LMax;
int mid=(a[t].L+a[t].R)>>1;
int ans;
if(R<=mid) ans=queryL(t*2,L,R);
else if(L>mid) ans=queryL(t*2+1,L,R);
else
{
ans=max(queryL(t*2,L,mid),S[mid]-S[L-1]+queryL(t*2+1,mid+1,R));
}
return ans;
}
int queryR(int t,int L,int R)
{
if(a[t].L==L&&a[t].R==R) return a[t].RMax;
int mid=(a[t].L+a[t].R)>>1;
int ans;
if(R<=mid) ans=queryR(t*2,L,R);
else if(L>mid) ans=queryR(t*2+1,L,R);
else
{
ans=max(queryR(t*2,L,mid)+S[R]-S[mid],queryR(t*2+1,mid+1,R));
}
return ans;
}
int query(int t,int L,int R)
{
if(a[t].L==L&&a[t].R==R) return a[t].Max;
int mid=(a[t].L+a[t].R)>>1;
int ans;
if(R<=mid) ans=query(t*2,L,R);
else if(L>mid) ans=query(t*2+1,L,R);
else
{
int x=query(t*2,L,mid);
int y=query(t*2+1,mid+1,R);
ans=max(x,y);
ans=max(ans,queryR(t*2,L,mid)+queryL(t*2+1,mid+1,R));
}
return ans;
}
int cal(int x1,int y1,int x2,int y2)
{
int a,b,c;
if(x2>y1)
{
a=queryR(1,x1,y1);
b=queryL(1,x2,y2);
return a+b+S[x2-1]-S[y1];
}
int ans;
a=queryR(1,x1,x2);
b=queryL(1,x2,y2);
ans=a+b-d[x2];
a=query(1,x2,y1);
ans=max(ans,a);
a=queryR(1,x1,y1);
b=queryL(1,y1,y2);
ans=max(ans,a+b-d[y1]);
return ans;
}
int main()
{
rush()
{
RD(n);
int i;
FOR1(i,n) RD(d[i]),S[i]=S[i-1]+d[i];
build(1,1,n);
int x1,y1,x2,y2;
int m;
RD(m);
while(m--)
{
RD(x1,y1); RD(x2,y2);
PR(cal(x1,y1,x2,y2));
}
}
}
SPOJ 2916 Can you answer these queries V(线段树-分类讨论)的更多相关文章
- 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 GSS5 Can you answer these queries V ——线段树
[题目分析] GSS1上增加区间左右端点的限制. 直接分类讨论就好了. [代码] #include <cstdio> #include <cstring> #include & ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【SP2916】Can you answer these queries V - 线段树
题面 You are given a sequence \(a_1,a_2,...,a_n\). (\(|A[i]| \leq 10000 , 1 \leq N \leq 10000\)). A qu ...
- SPOJ GSS3 Can you answer these queries III ——线段树
[题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #inc ...
随机推荐
- bzoj 1028 暴力枚举判断
昨天梦到这道题了,所以一定要A掉(其实梦到了3道,有两道记不清了) 暴力枚举等的是哪张牌,将是哪张牌,然后贪心的判断就行了. 对于一个状态判断是否为胡牌,1-n扫一遍,然后对于每个牌,先mod 3, ...
- 【BZOJ】【3007】拯救小云公主
思路题 我的naive的做法是二分答案+判定是否有路径可走……但是没有正确理解[走的方向任意]这句话…… 其实就是说想咋走咋走= =360°无死角乱走…… 所以其实是个平面上的问题…… 我们可以换个方 ...
- Matlab动态数组实现
clear all; clc; a = []; %不是null,也不能什么都不是 for i=1:10 a = [a i]; end
- IAP Store Kit Guide(中文)
IAP Store Kit Guide(中文) 一.In App Purchase概览 Store Kit代表App和App Store之间进行通信.程序将从App Store接收那些你想要提供的产品 ...
- LINGO使用教程(一)
LINGO是用来求解线性和非线性优化问题的简易工具.LINGO内置了一种建立最优化模型的语言,可以简便地表达大规模问题,利用LINGO高效的求解器可快速求解并分析结果. 1.LINGO快速入门 当你在 ...
- 安装mysql之后,存入中文出现乱码 02
现在出现这个问题,将编码字符串改成utf8 之后 数据表 还是不能存储中文. 看如下两张图,应该会有启发: 这下应该明白了吧.
- CString,string,char*之间的转换(转)
这三种类型各有各的优点,比如CString比较灵活,是基于MFC常用的类型,安全性也最高,但可移植性最差.string是使用STL时必不可少的类型,所以是做工程时必须熟练掌握的:char*是从学习C语 ...
- facebook design question 总结
http://blog.csdn.net/sigh1988/article/details/9790337 这里原帖地址: http://www.mitbbs.com/article_t/JobHun ...
- python--httplib模块使用
httplib是一个相对底层的http请求模块,其上有专门的包装模块,如urllib内建模块,goto等第三方模块,但是封装的越高就越不灵 活,比如urllib模块里请求错误时就不会返回结果页的内容, ...
- JavaScript中函数的形参和实参的实现原理剖析
我们都知道JS里面参数的传递是可以不一样的,比如我们有一个函数: <script type="text/javascript"> function one(a,b,c) ...