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 最大的前缀和, 连续和, 后缀和 然后这个题还要记录解的 ...
随机推荐
- Eclipse的设置、调优、使用(解决启动卡顿等问题)----转
eclipse调优 一般在不对eclipse进行相关设置的时候,使用eclipse总是会觉得启动好慢,用起来好卡,其实只要对eclipse的相关参数进行一些配置,就会有很大的改善. 加快启动速度 1. ...
- 【python】——三级菜单
作业需求: 打印三级菜单 可返回上一级 可随时退出程序 #!/usr/bin/env python # -*- coding:utf-8 -*- #Author: __Json.Zzgx__ menu ...
- Python3.6 性能测试框架Locust的搭建与使用
背景 Python3.6 性能测试框架Locust的搭建与使用 基础 python版本:python3.6 方法一: pip install locustio 方法二: 开发工具:pycharm 使用 ...
- WPS生成文章目录
WPS生成文章目录 1.引用–>插入目录...即可!
- 通过ICE轻松部署WES7镜像
作者:雷志刚 转自:http://lzg-ad.blog.sohu.com/156323256.html 注:该文转自Happymy,感谢他的技术提供和分享. 本文适合的软件版本:CTP,RC 如果大 ...
- centos7磁盘在线扩容
1.添加新磁盘 2.fdisk -l查看磁盘被识别的名称 3.如果输入fdisk -l命令没有找到新的磁盘,按下面步骤操作 1)进入到cd /sys/class/scsi_host/ 2)echo & ...
- jsp (二) 练习
package cn.sasa.serv; import java.io.IOException; import java.sql.SQLException; import java.util.Lis ...
- Spring Boot 入门day01
一.Spring Boot入门 1.Spring Boot简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特 ...
- windows 创建共享文件夹
1.右键要共享的文件夹----“共享”------“指定特定用户”,出现如下对话框: 2.选择“Everyone”,点击“共享”按钮,如下: 3.打开“运行“(win+r),输入上图 双反斜杠后面的内 ...
- nio例子
跟传统io相比,nio会支持更大的并发量 nio去除了传统io的连接阻塞,和读写阻塞 服务器端 客户端 nio模型 传统io nio