思路

线段树维护最大子段和,只不过这题还要维护左右端点

还是维护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!"的更多相关文章

  1. UvaLA 3938 "Ray, Pass me the dishes!"

                            "Ray, Pass me the dishes!" Time Limit: 3000MS   Memory Limit: Unkn ...

  2. UVA 1400."Ray, Pass me the dishes!" -分治+线段树区间合并(常规操作+维护端点)并输出最优的区间的左右端点-(洛谷 小白逛公园 升级版)

    "Ray, Pass me the dishes!" UVA - 1400 题意就是线段树区间子段最大和,线段树区间合并,但是这道题还要求输出最大和的子段的左右端点.要求字典序最小 ...

  3. UVA 1400 1400 - &quot;Ray, Pass me the dishes!&quot;(线段树)

    UVA 1400 - "Ray, Pass me the dishes!" option=com_onlinejudge&Itemid=8&page=show_pr ...

  4. 【LA3938】"Ray, Pass me the dishes!"

    原题链接 Description After doing Ray a great favor to collect sticks for Ray, Poor Neal becomes very hun ...

  5. uva 1400 "Ray, Pass me the dishes!" (区间合并 最大子段和+输出左右边界)

    题目链接:https://vjudge.net/problem/UVA-1400 题意:给一串序列,求最大子段,如果有多个,输出字典序最小的那个的左右端点 思路: 之前写过类似的,这个麻烦点需要输出左 ...

  6. 线段树(区间合并) LA 3989 "Ray, Pass me the dishes!"

    题目传送门 题意:动态最大连续子序列和,静态的题目 分析:nlogn的归并思想.线段树维护结点的三个信息,最大前缀和,最大后缀和,该区间的最大和的两个端点,然后答案是三个的better.书上用pair ...

  7. UVa 1400 (线段树) "Ray, Pass me the dishes!"

    求一个区间的最大连续子序列,基本想法就是分治,这段子序列可能在区间的左半边,也可能在区间的右半边,也有可能是横跨区间中点,这样就是左子区间的最大后缀加上右子区间的最大前缀之和. 线段树维护三个信息:区 ...

  8. 1400 - "Ray, Pass me the dishes!"

    哈哈,原来题意看错了,但有多个解的时候,输出起点靠前的,如果起点一样,则输出终点靠前的,修改后AC的代码如下: #include <cstdio> #include <iostrea ...

  9. uvalive 3938 "Ray, Pass me the dishes!" 线段树 区间合并

    题意:求q次询问的静态区间连续最大和起始位置和终止位置 输出字典序最小的解. 思路:刘汝佳白书 每个节点维护三个值 pre, sub, suf 最大的前缀和, 连续和, 后缀和 然后这个题还要记录解的 ...

随机推荐

  1. 用TreeSet生成不重复自动排序随机数组

    随机数组就是在指定长度的数组中用随机数字为每个元素赋值,常用于不确定数值的环境,如拼图游戏需要随机数组来打乱图片顺序.可是同时也存在问题,就是随机数的重复问题,这个问题常常被忽略. TreeSet类的 ...

  2. intput/output 文件的复制练习

    import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStrea ...

  3. css学习_css用户界面样式

    1.css用户界面样式 a.鼠标样式(记住几个兼容性好的) cursor:default/pointer/move/text; b.轮廓 outline outline:2px solid red: ...

  4. docker+rabbitmq的安装

    docker pull rabbitmq:management docker run -d -p : -p : -p : -p : -p : -v /data/rabbitmq-data/:/var/ ...

  5. Python四线程爬取西刺代理

    import requests from bs4 import BeautifulSoup import lxml import telnetlib #验证代理的可用性 import pymysql. ...

  6. 【UML】NO.70.EBook.9.UML.4.001-【PowerDesigner 16 从入门到精通】- 基础概念

    1.0.0 Summary Tittle:[UML]NO.70.EBook.9.UML.4.001-[PowerDesigner 16 从入门到精通]-  基础概念 Style:DesignPatte ...

  7. 把spring boot发布成window Service

    一:下载Winsw, 把下载后的文件名改为你的应用如doctor.exe 二:添加xml <service> <id>doctor-api-service</id> ...

  8. Cocos Creator 使用计时器(官方文档摘录)

    在 Cocos Creator 中,我们为组件提供了方便的计时器,这个计时器源自于 Cocos2d-x 中的 cc.Scheduler,我们将它保留在了 Cocos Creator 中并适配了基于组件 ...

  9. tensorboard窥视

    运行神经网络时,跟踪网络参数,以及输入输出是很重要的,可据此判断模型是否在学习,损失函数的值是否在不断减小.Tensorboard通过可视化方法,用于分析和调试网络模型. 使用tensorboard的 ...

  10. nginx根据CPU配置多线程运行

    转自:Nginx使用教程(二):Nginx配置性能优化之worker配置 配置Nginx workers <br\>NGINX根据指定的配置运行固定数量的工作进程. 这些工作进程负责处理所 ...