SPOJ 1043 1043. Can you answer these queries I
思路:用TREE记录节点的最大连续和,LEF记录左边开始的最大连续和,RIG记右边开始的最大连续和
然后处理的时候就是比较左边最大,右边最大 中间区间的问题
其中这个query 只能膜拜了。。。
大大缩减了时间。放弃治疗的我只会用三个函数
传参就要传出翔。
flag == -1表示要左边最大
flag == 1 表示要右边最大
然后ans在找到的区间的同时进行比较。
我已可入灵魂
#include <iostream>
#include <cstdio>
#include <algorithm>
#define MAXN 50005
#define lson num<<1,s,mid
#define rson num<<1|1,mid+1,e using namespace std; int tree[MAXN<<2];
int lef[MAXN<<2];
int rig[MAXN<<2];
int X[MAXN]={0};
int n;
int cnt; inline void pushup(int num,int s,int e)
{
int mid=(s+e)>>1; lef[num]=max(lef[num<<1],X[mid]-X[s-1]+lef[num<<1|1]);
rig[num]=max(rig[num<<1|1],X[e]-X[mid]+rig[num<<1]);
tree[num]=max(lef[num<<1|1]+rig[num<<1],max(tree[num<<1],tree[num<<1|1]));
} void build(int num,int s,int e)
{
if(s==e)
{
scanf("%d",&tree[num]);
lef[num]=rig[num]=tree[num];
X[cnt]=X[cnt-1]+tree[num];
cnt++;
return;
}
int mid=(s+e)>>1; build(num<<1,s,mid);
build(num<<1|1,mid+1,e);
pushup(num,s,e);
} int query(int num, int s, int e, int l, int r, int flag, int &ans)
{
int mid = (s+e)>>1;
if(s >= l && e <= r)
{
ans = max(ans, tree[num]);
return flag == -1 ? lef[num] : rig[num];
}
if(mid >= r)
return query(num<<1, s, mid, l, r, -1, ans);
else if(mid < l)
return query(num<<1|1, mid + 1, e, l, r, 1, ans);
else
{
int ln, rn;
ln = query(num<<1, s, mid, l, r, 1, ans);
rn = query(num<<1|1, mid + 1, e, l, r, -1, ans);
ans = max(ans, ln + rn);
if(flag == -1)
return max(lef[num<<1], X[mid] - X[s - 1] + rn);
else
return max(rig[num<<1|1], X[e] - X[mid] + ln);
}
} int main()
{
while(scanf("%d",&n)!=EOF)
{
cnt=1;
X[0]=0;
build(1,1,n); int m;
scanf("%d",&m);
while(m--)
{
int aa,bb;
scanf("%d%d",&aa,&bb);
int ans=X[aa]-X[aa-1];
query(1,1,n,aa,bb,-1,ans);
printf("%d\n",ans);
}
}
return 0;
} /*
8
-1 5 -6 7 -4 8 -2 7
999
*/
SPOJ 1043 1043. Can you answer these queries I的更多相关文章
- 「 SPOJ GSS3 」 Can you answer these queries III
# 题目大意 GSS3 - Can you answer these queries III 需要你维护一种数据结构,支持两种操作: 单点修改 求一个区间的最大子段和 # 解题思路 一个区间的最大子段 ...
- SPOJ 1043 GSS1 - Can you answer these queries I
题目描述 给出了序列A[1],A[2],-,A[N]. (a[i]≤15007,1≤N≤50000).查询定义如下: 查询(x,y)=max{a[i]+a[i+1]+-+a[j]:x≤i≤j≤y}. ...
- spoj 1557 GSS3 - Can you answer these queries III 线段树
题目链接 给出n个数, 2种操作, 一种是将第x个数改为y, 第二种是询问区间[x,y]内的最大连续子区间. 开4个数组, 一个是区间和, 一个是区间最大值, 一个是后缀的最大值, 一个是前缀的最大值 ...
- 【SPOJ - GSS2】Can you answer these queries II(线段树)
区间连续不重复子段最大值,要维护历史的最大值和当前的最大值,打两个lazy,离线 #include<cstdio> #include<cstring> #include< ...
- SPOJ 2916 GSS5 - Can you answer these queries V
传送门 解题思路 和GSS1相似,但需要巨恶心的分类讨论,对于x1<=y1< x2< =y2 这种情况 , 最大值应该取[x1,y1]的右端最大+[y1+1,x2-1]的和+[x2, ...
- SPOJ 1557 GSS2 - Can you answer these queries II (线段树+维护历史最值)
都说这题是 GSS 系列中最难的,今天做了一下,名副其实 首先你可以想到各种各样的在线乱搞想法,线段树,主席树,平衡树,等等,但发现都不太可行. 注意到题目也没有说强制在线,因此可以想到离线地去解决这 ...
- GSS1 spoj 1043 Can you answer these queries I 最大子段和
今天下午不知道要做什么,那就把gss系列的线段树刷一下吧. Can you answer these queries I 题目:给出一个数列,询问区间[l,r]的最大子段和 分析: 线段树简单区间操作 ...
- GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树
GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...
- 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 ...
随机推荐
- Using .NET 4's Lazy<T> 实现单实例
Using .NET 4's Lazy<T> type Explanation of the following code: If you're using .NET 4 (or high ...
- django HTTP请求(Request)和回应(Response)对象
Django使用request和response对象在系统间传递状态.—(阿伦)当一个页面被请示时,Django创建一个包含请求元数据的 HttpRequest 对象. 然后Django调入合适的视图 ...
- 在Eclipse中安装ADT
启动 Eclipse,然后选择 Help > Software Updates….在出现的对话框中,单击 Available Software 选项卡. 单击 Add Site 在 Add Si ...
- iOS各种系统通知Name
当了一次搬运工,原地址:http://blog.csdn.net/db905517804/article/details/50569949 用法: - (void) viewWillAppear:(B ...
- ASP.NET之HttpModule拦截404异常
Httpmodule代码: public class Error404Module : IHttpModule { public void Init(HttpApplication context) ...
- Android sqlite 数据库在java代码中的增删改查
private void queryPerson(PersonSQLiteOpenHelper personSQLiteOpenHelper) { SQLiteDatabase sqLiteDatab ...
- python3使用requests爬取新浪热门微博
微博登录的实现代码来源:https://gist.github.com/mrluanma/3621775 相关环境 使用的python3.4,发现配置好环境后可以直接使用pip easy_instal ...
- 《鸟哥的Linux私房菜》读书笔记三
1.在Linux系统中,每个设备都被当成一个文件来对待,每个设备都会有设备文件名.比如 IDE接口的硬盘文件名为/dev/hd[a-d] 括号内的字母为a-d当中任意一个,即/dev/hda,/dev ...
- Chapter 17 Replication
Chapter 17 Replication Table of Contents 17.1 Replication Configuration 17.2 Replication Implementat ...
- POJ_1182_食物链_[NOI]_(并查集)
描述 http://poj.org/problem?id=1182 共A,B,C三种动物,A吃B,B吃C,C吃A.给出询问 q : t , x , y , 表示: x 与 y 是同类 ( t==1 ...