UVA1400 "Ray, Pass me the dishes!"
思路
线段树维护最大子段和,只不过这题还要维护左右端点
还是维护pre,suf,sum,ans,只不过每个再多出一个维护端点的变量即可
注意多解讨论的大于号和大于等于号
代码
#include <cstdio>
#include <algorithm>
#include <cstring>
#define int long long
using namespace std;
const int MAXN = 500100;
struct Node{
int premax,sufmax,preid,sufid,maxid,maxx,sum,maxxidl,maxxidr;
}Seg[500100<<2];
int a[500100],n,m;
void pushup(Node &o,Node lson,Node rson){
o.sum=lson.sum+rson.sum;
if(lson.premax>=(lson.sum+rson.premax)){
o.premax=lson.premax;
o.preid=lson.preid;
}
else{
o.premax=lson.sum+rson.premax;
o.preid=rson.preid;
}
if(rson.sufmax>(rson.sum+lson.sufmax)){
o.sufmax=rson.sufmax;
o.sufid=rson.sufid;
}
else{
o.sufmax=rson.sum+lson.sufmax;
o.sufid=lson.sufid;
}
if(lson.maxx>=max(rson.maxx,lson.sufmax+rson.premax)){
o.maxx=lson.maxx;
o.maxxidl=lson.maxxidl;
o.maxxidr=lson.maxxidr;
}
else{
if(lson.sufmax+rson.premax>=rson.maxx){
o.maxx=lson.sufmax+rson.premax;
o.maxxidl=lson.sufid;
o.maxxidr=rson.preid;
}
else{
o.maxx=rson.maxx;
o.maxxidl=rson.maxxidl;
o.maxxidr=rson.maxxidr;
}
}
}
void build(int l,int r,int o){
if(l==r){
Seg[o].maxid=Seg[o].maxxidl=Seg[o].maxxidr=Seg[o].preid=Seg[o].sufid=l;
Seg[o].maxx=Seg[o].premax=Seg[o].sufmax=Seg[o].sum=a[l];
return;
}
int mid=(l+r)>>1;
build(l,mid,o<<1);
build(mid+1,r,o<<1|1);
pushup(Seg[o],Seg[o<<1],Seg[o<<1|1]);
}
Node query(int L,int R,int l,int r,int o){
if(L<=l&&r<=R){
return Seg[o];
}
int mid=(l+r)>>1;
if(R<=mid)
return query(L,R,l,mid,o<<1);
else{
if(L>mid)
return query(L,R,mid+1,r,o<<1|1);
else{
Node ans;
pushup(ans,query(L,R,l,mid,o<<1),query(L,R,mid+1,r,o<<1|1));
return ans;
}
}
}
signed main(){
int cnt=0;
while(scanf("%lld %lld",&n,&m)==2){
cnt++;
memset(Seg,0,sizeof(Seg));
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
build(1,n,1);
printf("Case %lld:\n",cnt);
for(int i=1;i<=m;i++){
int l,r;
scanf("%lld %lld",&l,&r);
Node tmp=query(l,r,1,n,1);
printf("%lld %lld\n",tmp.maxxidl,tmp.maxxidr);
}
}
return 0;
}
UVA1400 "Ray, Pass me the dishes!"的更多相关文章
- UvaLA 3938 "Ray, Pass me the dishes!"
"Ray, Pass me the dishes!" Time Limit: 3000MS Memory Limit: Unkn ...
- UVA 1400."Ray, Pass me the dishes!" -分治+线段树区间合并(常规操作+维护端点)并输出最优的区间的左右端点-(洛谷 小白逛公园 升级版)
"Ray, Pass me the dishes!" UVA - 1400 题意就是线段树区间子段最大和,线段树区间合并,但是这道题还要求输出最大和的子段的左右端点.要求字典序最小 ...
- UVA 1400 1400 - "Ray, Pass me the dishes!"(线段树)
UVA 1400 - "Ray, Pass me the dishes!" option=com_onlinejudge&Itemid=8&page=show_pr ...
- 【LA3938】"Ray, Pass me the dishes!"
原题链接 Description After doing Ray a great favor to collect sticks for Ray, Poor Neal becomes very hun ...
- uva 1400 "Ray, Pass me the dishes!" (区间合并 最大子段和+输出左右边界)
题目链接:https://vjudge.net/problem/UVA-1400 题意:给一串序列,求最大子段,如果有多个,输出字典序最小的那个的左右端点 思路: 之前写过类似的,这个麻烦点需要输出左 ...
- 线段树(区间合并) LA 3989 "Ray, Pass me the dishes!"
题目传送门 题意:动态最大连续子序列和,静态的题目 分析:nlogn的归并思想.线段树维护结点的三个信息,最大前缀和,最大后缀和,该区间的最大和的两个端点,然后答案是三个的better.书上用pair ...
- UVa 1400 (线段树) "Ray, Pass me the dishes!"
求一个区间的最大连续子序列,基本想法就是分治,这段子序列可能在区间的左半边,也可能在区间的右半边,也有可能是横跨区间中点,这样就是左子区间的最大后缀加上右子区间的最大前缀之和. 线段树维护三个信息:区 ...
- 1400 - "Ray, Pass me the dishes!"
哈哈,原来题意看错了,但有多个解的时候,输出起点靠前的,如果起点一样,则输出终点靠前的,修改后AC的代码如下: #include <cstdio> #include <iostrea ...
- uvalive 3938 "Ray, Pass me the dishes!" 线段树 区间合并
题意:求q次询问的静态区间连续最大和起始位置和终止位置 输出字典序最小的解. 思路:刘汝佳白书 每个节点维护三个值 pre, sub, suf 最大的前缀和, 连续和, 后缀和 然后这个题还要记录解的 ...
随机推荐
- CXF整合Sping与Web容器
1.创建HelloWorld 接口类 package com.googlecode.garbagecan.cxfstudy.helloworld; import javax.jws.WebMethod ...
- 【C++ mid-term exerises】
1. 用掷骰子方式,模拟班级每个学号被随机抽点的概率. (12分) 具体要求如下: (1)设计并实现一个骰子类Dice. ① 数据成员sides表示骰子面数.构造时,指定骰子是6面,8面,还是其它数值 ...
- CString数组和CStringArray
CStringArray是编译器定义的类型!可以进行一些(如:访问.增.删.改)等操作. 集中单个字符串的操作使用Cstring,集中一批字符串的管理使用CstringArray. 一个是动态,CSt ...
- MongoDB数据库去重
查询: db.patents_texts.aggregate([ { $group:{_id:{Patent_num:'$Patent_num',Patent_name:'$Patent_name'} ...
- mysql InnoDB锁等待的查看及分析
说明:前面已经了解了InnoDB关于在出现锁等待的时候,会根据参数innodb_lock_wait_timeout的配置,判断是否需要进行timeout的操作,本文档介绍在出现锁等待时候的查看及分析处 ...
- myEclipse导入现成项目出现错误 【申明来源于网络】
myEclipse导入现成项目出现错误 [申明来源于网络] 原地址:http://blog.sina.com.cn/s/blog_6d7703400100znh6.html file–>impo ...
- phpredis Redis阵列 Redis Arrays
官方URL:https://github.com/phpredis/phpredis/blob/master/arrays.markdown#readme 2017年10月29日20:44:01 Re ...
- asp.net mvc ef 性能监控调试工具 MiniProfiler
MiniProfiler是一款针对.NET, Ruby, Go and Node.js的性能分析的轻量级程序.可以对一个页面本身,及该页面通过直接引用.Ajax.Iframe形式访问的其它页面进行监控 ...
- Luogu 1086 - 花生采摘 - [简单模拟]
题目链接:https://www.luogu.org/problemnew/show/P1086 题目描述鲁宾逊先生有一只宠物猴,名叫多多.这天,他们两个正沿着乡间小路散步,突然发现路边的告示牌上贴着 ...
- SQL Server 索引自动组织维护
公司的一个产品中的数据库,几个热点表因为主键和索引设计不合理,造成索引碎片过大,影响性能. 我尝试新建了一个索引碎片整理的定时任务,用于维护索引锁片和统计信息. 具体的过程如下: 本文原创,转发请表明 ...