思路

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

还是维护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. css处理文本溢出

    css: .box{ width: 100px; overflow:hidden; white-space:nowrap; text-overflow:ellipsis; } white-space: ...

  2. Nestjs 上传文件

    Docs: https://docs.nestjs.com/techniques/file-upload 上传单文件 @Post('upload') @UseInterceptors(FileInte ...

  3. centos 安装 python36

    centos6 安装 python36 临时方法: https://www.softwarecollections.org/en/scls/rhscl/rh-python36/ 方法二: http:/ ...

  4. 给table加边框的样式

    <style> .tb { width: 1600px; text-align: center; border-collapse: collapse; } .tb tr td { bord ...

  5. 【微信小程序——开发步骤1】

    知识点:   view,image,text编写文本框架 使用弹性盒子动态布局 使用rpx调试分辨率 在wxml中查看默认样式属性 步骤: 1.以如图页面实例说明如何写出微信文本内容 先对页面写出整体 ...

  6. LIBS入门

    样品汽化产生自由原子,原子电子的激发诱导光辐射产生表征原子的分立光谱,采集和分析光辐射. 光源:1064nm Nd:YAG固态激光器,10ns脉冲,焦点光密度1 GW·cm−2 可见和紫外光源.   ...

  7. Gym 101981K - Kangaroo Puzzle - [玄学][2018-2019 ACM-ICPC Asia Nanjing Regional Contest Problem K]

    题目链接:http://codeforces.com/gym/101981/problem/K Your friend has made a computer video game called “K ...

  8. 【RabbitMQ】工作模式介绍

    一.前言 之前,笔者写过< CentOS 7.2 安装 RabbitMQ> 这篇文章,今天整理一下 RabbitMQ 相关的笔记便于以后复习. 二.模式介绍 在 RabbitMQ 官网上提 ...

  9. JMeter学习-041-响应数据中文乱码解决方法

    华夏子孙,中文为母语.因而在接口测试过程中,响应数据含有中文是再也正常不过的事情.同时,初学JMeter的童鞋,经常会遇到响应数据中中文乱码的问题. 本文中提供两种方式的修正方法,仅供大家参考,谢谢. ...

  10. 如何使用Shell判断版本号的大小

    如果你想通过shell来比较两个版本号字符串,比如两个版本号1.1.2和1.2.1这两个版本谁是比较新的. 最简单的就是使用sort命令.加上参数"-V"后sort命令就可以把文本 ...