UvaLA 3938 "Ray, Pass me the dishes!"
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description

After doing Ray a great favor to collect sticks for Ray, Poor Neal becomes very hungry. In return for Neal's help, Ray makes a great dinner for Neal. When it is time for dinner, Ray arranges all the dishes he makes in a single line (actually this line is very long ... <tex2html_verbatim_mark>, the dishes are represented by 1, 2, 3 ... <tex2html_verbatim_mark>). ``You make me work hard and don't pay me! You refuse to teach me Latin Dance! Now it is time for you to serve me", Neal says to himself.
Every dish has its own value represented by an integer whose absolute value is less than 1,000,000,000. Before having dinner, Neal is wondering about the total value of the dishes he will eat. So he raises many questions about the values of dishes he would have.
For each question Neal asks, he will first write down an interval [a, b] <tex2html_verbatim_mark>(inclusive) to represent all the dishes a, a + 1,..., b <tex2html_verbatim_mark>, where a <tex2html_verbatim_mark>and b <tex2html_verbatim_mark>are positive integers, and then asks Ray which sequence of consecutive dishes in the interval has the most total value. Now Ray needs your help.
Input
The input file contains multiple test cases. For each test case, there are two integers n <tex2html_verbatim_mark>and m <tex2html_verbatim_mark>in the first line (n, m < 500000) <tex2html_verbatim_mark>. n <tex2html_verbatim_mark>is the number of dishes and m <tex2html_verbatim_mark>is the number of questions Neal asks.
Then n <tex2html_verbatim_mark>numbers come in the second line, which are the values of the dishes from left to right. Next m <tex2html_verbatim_mark>lines are the questions and each line contains two numbers a <tex2html_verbatim_mark>, b <tex2html_verbatim_mark>as described above. Proceed to the end of the input file.
Output
For each test case, output m <tex2html_verbatim_mark>lines. Each line contains two numbers, indicating the beginning position and end position of the sequence. If there are multiple solutions, output the one with the smallest beginning position. If there are still multiple solutions then, just output the one with the smallest end position. Please output the result as in the Sample Output.
Sample Input
3 1
1 2 3
1 1
Sample Output
Case 1:
1 1
也叫UVA 1400.。。。。
给一个区间{a,b},求区间内最大的连续和的范围 i,j
非常麻烦的线段树,调了好长时间。。。。。
每个节点维护,最大前缀和的位置pref,最大后缀和的位置suff,以及最大连续和的区间范围sub。。。。
向上更新的时候,sub={两个子结点的sub,两个子结点交接处的和}
pref={左子结点的pref,延续到右子结点pref的和} suff也一样
查询的时候,i,j可能是子结点的sub,也可能是左右结点并出来的值。。。
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 typedef long long int LL;
typedef pair<int,int> Interval;
const int maxn=; LL pref_sum[maxn];
LL sum(int a,int b){return pref_sum[b]-pref_sum[a-];}
LL sum(Interval i){return sum(i.first,i.second);} Interval better(Interval a,Interval b)
{
LL A=sum(a),B=sum(b);
if(A==B)
{
if(a<b) return a; else return b;
}
else if(A>B) return a;
else if(A<B) return b;
} struct IntervalTree
{
LL max_pref[maxn<<],max_suff[maxn<<];
Interval max_sub[maxn<<];
///pushUP build query_suff query_pref query
void pushUP(int l,int r,int rt)
{
///sub
int lc=rt<<;
int rc=rt<<|;
max_sub[rt]=better(max_sub[lc],max_sub[rc]);
max_sub[rt]=better(max_sub[rt],make_pair(max_suff[lc],max_pref[rc])); ///prex
LL v1=sum(l,max_pref[lc]);
LL v2=sum(l,max_pref[rc]);
if(v1==v2)
max_pref[rt]=max_pref[lc];
else
max_pref[rt]=v1>v2?max_pref[lc]:max_pref[rc]; ///suffx
v1=sum(max_suff[rc],r);
v2=sum(max_suff[lc],r);
if(v1==v2)
max_suff[rt]=max_suff[lc];
else
max_suff[rt]=v1>v2?max_suff[rc]:max_suff[lc];
}
void build(int l,int r,int rt)
{
if(l==r)
{
max_suff[rt]=max_pref[rt]=l;
max_sub[rt]=make_pair(l,l);
return ;
}
int m=(l+r)>>;
build(lson);
build(rson);
pushUP(l,r,rt);
}
Interval query_pref(int L,int R,int l,int r,int rt)
{
if(max_pref[rt]<=R) return make_pair(L,max_pref[rt]);
int m=(l+r)>>;
if(m>=R) return query_pref(L,R,lson);
Interval i=query_pref(L,R,rson);
i.first=L;
return better(i,make_pair(L,max_pref[rt<<]));
}
Interval query_suff(int L,int R,int l,int r,int rt)
{
if(max_suff[rt]>=L) return make_pair(max_suff[rt],R);
int m=(l+r)>>;
if(m<L) return query_suff(L,R,rson);
Interval i=query_suff(L,R,lson);
i.second=R;
return better(i,make_pair(max_suff[rt<<|],R));
}
Interval query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R) return max_sub[rt];
int m=(l+r)>>;
if(R<=m) return query(L,R,lson);
if(L>m) return query(L,R,rson);
Interval i1=query_pref(L,R,rson);
Interval i2=query_suff(L,R,lson);
Interval i3=better(query(L,R,lson),query(L,R,rson));
return better(i3,make_pair(i2.first,i1.second));
}
}; IntervalTree tree; int main()
{
int cas=,a,b,n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
pref_sum[]=;
for(int i=;i<=n;i++)
{
scanf("%d",&a);
pref_sum[i]=pref_sum[i-]+a;
}
tree.build(,n,);
printf("Case %d:\n",cas++);
while(m--)
{
scanf("%d%d",&a,&b);
Interval ans=tree.query(a,b,,n,);
printf("%d %d\n",ans.first,ans.second);
}
}
return ;
}
UvaLA 3938 "Ray, Pass me the dishes!"的更多相关文章
- uvalive 3938 "Ray, Pass me the dishes!" 线段树 区间合并
题意:求q次询问的静态区间连续最大和起始位置和终止位置 输出字典序最小的解. 思路:刘汝佳白书 每个节点维护三个值 pre, sub, suf 最大的前缀和, 连续和, 后缀和 然后这个题还要记录解的 ...
- UVALive 3938 - "Ray, Pass me the dishes!" - [最大连续子列和+线段树]
题目链接:https://cn.vjudge.net/problem/UVALive-3938 参考刘汝佳书上说的: 题意: 给出一个长度为n的序列, 再给出m个询问, 每个询问是在序列 $[a,b] ...
- UVALive 3938 Ray, Pass me the dishes! (动态最大连续和)
题意:求一个动态区间的最大连续和. 静态版本的O(n)算法显示不适用了,但是可以用线段树分治,因为一个连续和要在两边的区间,要么跨越两边,对于一个结点维护最大前缀和,后缀和,子区间连续和. 题目要求输 ...
- 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 ...
- UVALive - 3938:"Ray, Pass me the dishes!"
优美的线段树 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring& ...
- 线段树(区间合并) LA 3989 "Ray, Pass me the dishes!"
题目传送门 题意:动态最大连续子序列和,静态的题目 分析:nlogn的归并思想.线段树维护结点的三个信息,最大前缀和,最大后缀和,该区间的最大和的两个端点,然后答案是三个的better.书上用pair ...
- UVa 1400 (线段树) "Ray, Pass me the dishes!"
求一个区间的最大连续子序列,基本想法就是分治,这段子序列可能在区间的左半边,也可能在区间的右半边,也有可能是横跨区间中点,这样就是左子区间的最大后缀加上右子区间的最大前缀之和. 线段树维护三个信息:区 ...
随机推荐
- VS2015+Win10 调试DirectX 报错
安装完Win10调试程序突然在这个地方报错: #if (defined(DEBUG) || defined(_DEBUG)) deviceFlags |= D3D11_CREATE_DEVICE_DE ...
- 优化IPOL网站中基于DCT(离散余弦变换)的图像去噪算法(附源代码)。
在您阅读本文前,先需要告诉你的是:即使是本文优化过的算法,DCT去噪的计算量依旧很大,请不要向这个算法提出实时运行的苛刻要求. 言归正传,在IPOL网站中有一篇基于DCT的图像去噪文章,具体的链接地址 ...
- linux查看主板型号及内存硬件信息
公司服务器内存不够用了. 想看看买啥型号的. 购买内存条注意点: ddr3 or4 频率 块钱. 内存槽及内存条: dmidecode |grep -A16 "Memory Device ...
- .net(C#)中this关键字
使用this关键字引用成员变量使用this关键字在自身构造方法内部引用其它构造方法使用this关键字代表自身类的对象使用this关键字引用成员方法 在一个类的方法或构造方法内部,可以使用"t ...
- python基础-面向对象进阶
一.什么是反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它首先被 ...
- Java学习笔记(四)
字符串 字符串应用主要分为String类操作与字符串生成器 在程序中频繁的进行附加字符串则使用字符串生成器StringBuilder 数组 概述 数组是具有相同数据类型的一组数据的集合 数组创建 先声 ...
- python venv下安装mysql出错 解决方法
1.首先使用exe文件安装python-mysql.链接: http://pan.baidu.com/s/1kVqILTX 密码: manj. 2.虚拟环境创建后,我们把已经在公共环境使用exe安装好 ...
- 【poj2096】 Collecting Bugs
http://poj.org/problem?id=2096 (题目链接) 题意 有一个程序,其中有s个子结构,每个子结构出bug的概率相等.bug总共分成n类,每种bug出现的概率相等.每天找出一个 ...
- crodova打包apk个人总结
1.安装nodejs 2.安装 cordova npm install -g cordova 3.安装Java JDK,官网下载地址 系统变量→新建 JAVA_HOME 变量 . 变量值填写jdk的安 ...
- Linux之:Ubuntu速学笔记(1)
撰写日期:2016-7-2 17:11:28 Saturday 课程资源: web程序员角度ubuntu自修速学课程 链接来源:程序员在囧途, VMware: VMware Workstation1 ...