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!"
求一个区间的最大连续子序列,基本想法就是分治,这段子序列可能在区间的左半边,也可能在区间的右半边,也有可能是横跨区间中点,这样就是左子区间的最大后缀加上右子区间的最大前缀之和. 线段树维护三个信息:区 ...
随机推荐
- WinXP/Win7/Win8本地用户配置文件迁移至域用户
一.概述 最近在进行加域的工作,PC大部分是Win7,使用过微软USMT 4.0和5.0工具进行迁移,但命令行报错,目前还没找到好的文章研究. 本文迁移方法很特殊,利用的是Windows默认配置文件. ...
- Jquery UI
jQuery UI简介 jQuery UI包含了许多维持状态的小部件(Widget),因此,它与典型的 jQuery 插件使用模式略有不同.所有的 jQuery UI 小部件(Widget)使用相同的 ...
- JS中字符串的true转化为boolean类型的true
var a="True"; a = eval(a.toLowerCase()); alert(typeof a); //boolean alert(a);//true 正解,eva ...
- 《Google软件测试之道》基础
<Google软件测试之道>,一直听朋友讲起这本书,出于琐事太多,一直没机会拜读,最近部门架构觉得我们IT部门的技术太low,就给我们挑选了一些书籍,让我们多看看... 个人的一种学习习惯 ...
- Anna-senpai帖子翻译与Mirai源代码使用
Anna-senpai这个人太好玩了,整件事就像没有黄段子的无聊世界那样. 无聊翻译了一下,顺便实验了效果. --------------------------------------------- ...
- 浏览器内核控制Meta标签说明文档【转】
背景介绍 由于众所周知的情况,国内的主流浏览器都是双核浏览器:基于Webkit内核用于常用网站的高速浏览.基于IE的内核用于兼容网银.旧版网站.以360的几款浏览器为例,我们优先通过Webkit内核渲 ...
- java.io.IOException: mark/reset not supported
java.io.IOException: mark/reset not supported at java.io.InputStream.reset(InputStream.java:348) at ...
- mybatis多对一关联
mybatis多对一关联查询实现 1.定义实体 定义实体的时候需要注意,若是双向关联,就是说双方的属性中都含有对方对象作为域属性出现, 那么在写toString()方法时需要注意,只让某一方输出即可, ...
- intellij idea 12 编码不可映射字符
IntelliJ IDEA中错误提示:java: Syntax error on token "Invalid Character", delete this token Inte ...
- 3sum问题的解决
其实一开始想错了,把这个问题想难了,导致没有思路,现在好了很多. 题目: Given an array S of n integers, are there elements a, b, c in S ...