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

[Submit]   [Go Back]   [Status]

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 [ab] <tex2html_verbatim_mark>(inclusive) to represent all the dishes aa + 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 (nm < 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

[Submit]   [Go Back]   [Status]

也叫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 ;
}

最新文章

  1. Struts2:类型转换器
  2. Leetcode 299 Bulls and Cows 字符串处理 统计
  3. C语言原子接口与实现
  4. CUBRID学习笔记 44 UPDATE 触发器 更新多表 教程
  5. 解密 Uber 数据团队的基础数据架构优化之路
  6. WP8.1和Win8.1的不同之处
  7. C#应用程序获取项目路径的方法总结
  8. webAPI---发布(IIS)--发布遇到问题(500.19,500.21,404.8,404.3)
  9. ASP.NET Core中的OWASP Top 10 十大风险-跨站点脚本攻击 (XSS)
  10. java 11 标准Java异步HTTP客户端
  11. 小白用阿里云搭载wordpress个人博客
  12. Java版统计文件中的每个单词出现次数
  13. JSP:注册&amp;登录
  14. TCP 协议相关
  15. Android Developers:保存文件
  16. Android程序员眼中世界上最遥远的距离
  17. 将远程UI分支克隆到本地UI分支
  18. iOS private-api-checker私有API检测
  19. 反向代理负载均衡-----nginx
  20. POJ_1984 Navigation Nightmare 【并查集】

热门文章

  1. 表单元素——checkbox样式美化
  2. ngx_http_core_module模块.md
  3. 使用Guid做主键和int做主键性能比较
  4. 【原】Learning Spark (Python版) 学习笔记(四)----Spark Sreaming与MLlib机器学习
  5. 「坐上时光机,查找编译压缩后的文件最初的样子」gulp-sourcemaps 使用说明
  6. archlinux安裝手记(Win10+Arch、GPT+UEFI、lvm)
  7. neo4j-java连接
  8. error: failed to push some refs to &#39;https://github.com/github账号/learn_git.git&#39; hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caus
  9. 【翻译】XV6-DRAFT as of September 3,2014 第0章 操作系统接口
  10. 【Android群英传】学习笔记(二)