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 最大的前缀和, 连续和, 后缀和 然后这个题还要记录解的 ...
随机推荐
- python3 小工具
扫描IP的端口是否开放:Porttest.py # -*- coding: utf-8 -*- import sys import os import socket #扫描 def scanport( ...
- js中 let 与 var 的区别
一: 变量提升与否 var: console.log(a); // undefined var a = 'abc'; // 这段代码实际执行顺序是: var a; //变量声明提升至当前作用域顶部 c ...
- linux的基本操作(mysql 的基本操作)
Mysql 的基本操作 在前面两个章节中已经介绍过MySQL的安装了,但是光会安装还不够,还需要会一些基本的相关操作.当然了,关于MySQL的内容也是非常多的,只不过对于linux系统管理员来讲,一些 ...
- mac 远程连接 云服务器
之前mac 命令行连接云端服务器,一直失败,今天问题突然间解决了,如果遇到类似的问题,按照方法解决不了,可以在下面留言,共同探讨. 首先,在云端先判断一下云端服务器是否安装了 ssh服务器:op ...
- 关于c++深拷贝与浅拷贝
首先看一段代码: #include<iostream> #include<cstring> #include<malloc.h> using namespace s ...
- zepto.js的touch模块
touch库实现了什么和引入背景 touch模块是基于zepto.js的. click事件在移动端上会有 300ms 的延迟,同时因为需要 长按 , 双触击 等富交互,所以我们通常都会引入类似 ze ...
- jquery操作checkBox 一次取消选中后不能再选中
$("input[type='checkbox']").each(function(){ $(this).attr("checked","checke ...
- ERROR org.hibernate.hql.internal.ast.ErrorCounter unexpected token: form 异常解决
ERROR org.hibernate.hql.internal.ast.ErrorCounter unexpected token: form 异常解决 根据异常提示:我找了我的MySQL语句:果然 ...
- ArrayList 的代码
public class user { private String userName; //类的构造方法 public user (String userName ){ this.userName= ...
- [AI][tensorflow][keras] archlinux下 tersorflow and keras 安装
tensorflow TensorFlow is an open-source machine learning library for research and production. https: ...