"Ray, Pass me the dishes!"
uvaLive3938:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1939
题意:给你n个数,然后给你一个区间,让你查询这个区间内最大和连续子区间。
题解:哎。智商是硬伤啊,虽然线段树也做过不少题目,但是遇到这样的题目还是不会处理啊,看了别人的代码才明白了怎么做。用那个线段树维护区间最大前缀,最大后缀,以及真正的最大区间。要注意父节点这三个变量是怎么由子节点推导出来的。还是贴代码吧。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int ll[N*],rr[N*];
struct Node{
int pre,suf;
int vx,vy;
}num[N*];
long long sum[N];
void pushup(int rt,int l,int r){
if(sum[num[rt<<].pre]-sum[l-]>=sum[num[rt<<|].pre]-sum[l-])
num[rt].pre=num[rt<<].pre;
else
num[rt].pre=num[rt<<|].pre;
if(sum[r]-sum[num[rt<<].suf-]>=sum[r]-sum[num[rt<<|].suf-])
num[rt].suf=num[rt<<].suf;
else
num[rt].suf=num[rt<<|].suf;
long long v1=sum[num[rt<<].vy]-sum[num[rt<<].vx-];
long long v2=sum[num[rt<<|].vy]-sum[num[rt<<|].vx-];
long long v0=sum[num[rt<<|].pre]-sum[num[rt<<].suf-];
if(v1>=v2){
num[rt].vx=num[rt<<].vx;
num[rt].vy=num[rt<<].vy;
}
else{
num[rt].vx=num[rt<<|].vx;
num[rt].vy=num[rt<<|].vy;
}
if(v0==sum[num[rt].vy]-sum[num[rt].vx-]){
if(num[rt<<].suf<num[rt].vx){
num[rt].vx=num[rt<<].suf;
num[rt].vy=num[rt<<|].pre;
}
}
if(v0>sum[num[rt].vy]-sum[num[rt].vx-]){
num[rt].vx=num[rt<<].suf;
num[rt].vy=num[rt<<|].pre;
}
}
void build(int rt,int l,int r){
ll[rt]=l;
rr[rt]=r;
if(l==r){
num[rt].pre=num[rt].suf=num[rt].vx=num[rt].vy=l;
return;
}
int mid=(l+r)/;
build(rt<<,l,mid);
build(rt<<|,mid+,r);
pushup(rt,l,r);
}
Node query(int rt,int s,int t){
if(ll[rt]==s&&rr[rt]==t)
return num[rt];
int mid=(ll[rt]+rr[rt])/;
if(mid>=t)return query(rt<<,s,t);
else if(mid<s)return query(rt<<|,s,t);
else{
Node temp1=query(rt<<,s,mid);
Node temp2=query(rt<<|,mid+,t);
Node temp;
if(sum[temp1.pre]-sum[s-]>=sum[temp2.pre]-sum[s-])
temp.pre=temp1.pre;
else
temp.pre=temp2.pre;
if(sum[t]-sum[temp1.suf-]>=sum[t]-sum[temp2.suf-])
temp.suf=temp1.suf;
else
temp.suf=temp2.suf;
long long v1=sum[temp1.vy]-sum[temp1.vx-];
long long v2=sum[temp2.vy]-sum[temp2.vx-];
long long v0=sum[temp2.pre]-sum[temp1.suf-];
if(v1>=v2){
temp.vx=temp1.vx;
temp.vy=temp1.vy;
}
else{
temp.vx=temp2.vx;
temp.vy=temp2.vy;
}
if(v0==sum[temp.vy]-sum[temp.vx-]){
if(temp1.suf<temp.vx){
temp.vx=temp1.suf;
temp.vy=temp2.pre;
}
}
if(v0>sum[temp.vy]-sum[temp.vx-]){
temp.vx=temp1.suf;
temp.vy=temp2.pre;
}
return temp;
}
}
int main(){
int cas=,n,m,l,r;
long long tp;
while(~scanf("%d%d",&n,&m)){
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++){
scanf("%lld",&tp);
sum[i]=sum[i-]+tp;
}
build(,,n);
printf("Case %d:\n",++cas);
for(int i=;i<=m;i++){
scanf("%d%d",&l,&r);
Node ans=query(,l,r);
printf("%d %d\n",ans.vx,ans.vy);
}
} }
"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 ...
- 线段树(区间合并) 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 最大的前缀和, 连续和, 后缀和 然后这个题还要记录解的 ...
- uva 1400 - "Ray, Pass me the dishes!"
又是一道线段树区间更新的题: #include<cstdio> #include<algorithm> #include<cstring> #define ll l ...
随机推荐
- OpenSSL使用指南
OpenSSL使用指南 1 介绍 OpenSSL是使用非常广泛的SSL的开源实现.由于其中实现了为SSL所用的各种加密算法,因此OpenSSL也是被广泛使用的加密函数库. 1.1 SSL ...
- common-lang——StringUtils
1.文字省略处理 最多显示几个字 StringUtils.abbreviate("中华人民共和国", 5); // output:中华.. 2.文字中间省略 最多显示几个字符 St ...
- Struts2 OGNL调用公共静态方法
在实现一个功能的时候用到了Struts2的OGNL 调用后台的静态方法,弄了半天没有出来结果,原来是自己没有在Struts的配置文件里面申明可以在前台使用后台的静态方法 <constant na ...
- Java基础知识强化之集合框架笔记14:List集合存储字符串并遍历
1. List集合存储学生对象并遍历: 需求:存储字符串并遍历 分析: (1)创建集合对象 (2)创建字符串对象 (3)添加字符串对象到集合中 (4)遍历集合 2. 代码示例: package cn. ...
- 第九篇:python高级之操作数据库
python高级之操作数据库 python高级之操作数据库 本节内容 pymysql介绍及安装 使用pymysql执行sql 获取新建数据自增ID fetch数据类型设置 1.pymysql介绍及 ...
- win 10 安装 mysql解压版 步骤
参考资料:win 10 安装 mysql 5.7 网址:http://blog.sina.com.cn/s/blog_5f39af320102wbk0.html 本文参考上面的网址的教程,感谢作者分享 ...
- post get
/// <summary> /// Get方法 /// </summary> /// <param name="serverUrl">url地址 ...
- CSS3条件判断——@supports/window.CSS.supports()(转)
CSS3条件判断,听起来"不明觉厉",如果你对CSS稍为熟悉一点的话,你会发现CSS中的"@media"就是条件判断之一.是的,在CSS3的条件判断规范文档中包 ...
- (转)smarty实现多级分类的方法
--http://www.aspku.com/kaifa/php/44679.html 这篇文章主要介绍了smarty实现多级分类的方法,涉及循环读取的技巧,非常具有实用价值,需要的朋友可以参考下 ...
- 在线预览文件(pdf)
1.flash版(借助flexpaper工具) 可以把pdf文件用pdf2swf工具转换成swf文件.下载地址http://www.swftools.org/download.html 转换代码如下: ...