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 线段树单点更新,虽然思路很简单,但我写的丑爆了!!
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <assert.h>
#include <queue> using namespace std; #define read() freopen("sw.in", "r", stdin)
#define ls l, m, rt << 1
#define rs m + 1, r, rt << 1 | 1 const int MAX = ;
const int INF = 1e9 + ;
typedef long long ll;
int N, M;
ll max_sub[ * MAX], max_suffix[ * MAX], max_prefix[ * MAX];
int id_suffix[ * MAX], id_prefix[ * MAX];
int s[ * MAX], e[ * MAX];
ll sum[ * MAX];
struct node {
ll maxsub, maxprefix, maxsuffix , sum;
int idprefix, idsuffix, ids, ide;
};
queue <node> q; void push_up(int rt) {
sum[rt] = sum[rt << ] + sum[rt << | ]; ll t[], id = ;
t[] = max_sub[rt << ];
t[] = max_suffix[rt << ] + max_prefix[rt << | ];
t[] = max_sub[rt << | ];
for (int i = ; i <= ; ++i) {
//printf("%d\n", t[i]);
if (t[id] < t[i]) {
id = i; }
} //printf("id = %d\n", id);
max_sub[rt] = t[id];
if (id == ) {
s[rt] = s[rt << ];
e[rt] = e[rt << ];
} else if (id == ) {
s[rt] = id_suffix[rt << ];
e[rt] = id_prefix[rt << | ];
} else {
s[rt] = s[rt << | ];
e[rt] = e[rt << | ];
} if (max_prefix[rt << ] < sum[rt << ] + max_prefix[rt << | ]) {
max_prefix[rt] = sum[rt << ] + max_prefix[rt << | ];
id_prefix[rt] = id_prefix[rt << | ];
} else {
max_prefix[rt] = max_prefix[rt << ] ;
id_prefix[rt] = id_prefix[rt << ];
} if (max_suffix[rt << | ] <= sum[rt << | ] + max_suffix[rt << ]) {
max_suffix[rt] = sum[rt << | ] + max_suffix[rt << ];
id_suffix[rt] = id_suffix[rt << ];
} else {
max_suffix[rt] = max_suffix[rt << | ];
id_suffix[rt] = id_suffix[rt << | ];
} } void build(int l, int r, int rt) {
if (l == r) {
cin >> sum[rt];
max_sub[rt] = max_suffix[rt] = max_prefix[rt] = sum[rt];
id_suffix[rt] = id_prefix[rt] = s[rt] = e[rt] = l;
return;
} int m = (l + r) >> ;
build(ls);
build(rs); push_up(rt); } void query(int ql, int qr, int l, int r, int rt) {
if (ql <= l && r <= qr) {
if (q.size() == ) {
node x = q.front(), a;
ll t[];
t[] = x.maxsub;
t[] = x.maxsuffix + max_prefix[rt];
t[] = max_sub[rt];
int id = ;
for (int i = ; i <= ; ++i) {
if (t[id] < t[i]) {
id = i;
}
} a.maxsub = t[id];
if (id == ) {
a.ids = x.ids;
a.ide = x.ide;
} else if (id == ) {
a.ids = x.idsuffix;
a.ide = id_prefix[rt];
} else {
a.ids = s[rt];
a.ide = e[rt];
} if (max_prefix[rt] <= x.sum + max_prefix[rt]) {
a.maxprefix = x.sum + max_prefix[rt];
a.idprefix = id_prefix[rt];
} else {
a.maxprefix = x.maxprefix;
a.idprefix = x.idprefix;
} if (max_suffix[rt] <= sum[rt] + x.maxsuffix) {
a.maxsuffix = sum[rt] + x.maxsuffix;
a.idsuffix = x.idsuffix;
} else {
a.maxsuffix = max_suffix[rt];
a.idsuffix = id_suffix[rt];
} q.pop();
q.push(a);
} else {
node a;
a.ids = s[rt]; a.ide = e[rt];
a.idprefix = id_prefix[rt]; a.idsuffix = id_suffix[rt];
a.maxprefix = max_prefix[rt]; a.maxsub = max_sub[rt];
a.maxsuffix = max_suffix[rt]; a.sum = sum[rt];
q.push(a);
} } else {
int m = (l + r) >> ;
if (ql <= m) query(ql ,qr, ls);
if (qr > m) query(ql, qr, rs);
}
} int main()
{
//read();
int ca = ;
while (~scanf("%d%d", &N, &M)) {
memset(sum, , sizeof(sum));
build(, N, );
printf("Case %d:\n", ca++);
for (int i = ; i <= M; ++i) {
int l, r;
scanf("%d%d", &l, &r);
query(l, r, , N, );
node x = q.front(); q.pop();
printf("%d %d\n", x.ids, x.ide); }
} //cout << "Hello world!" << endl;
return ;
}

LA 3938的更多相关文章

  1. LA 3938 动态最大连续和 线段树

    题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  2. la 3938(未完成)

    题意:给出一个长度为n的整数序列D,你的任务是对m个询问作出回答.对于询问(a,b), 需要找到两个下标x和y,使得a≤x≤y≤b,并且Dx+Dx+1+...+Dy尽量大. 如果有多组满足条件的x和y ...

  3. LA 3938 动态最大连续区间 线段树

    思路很清晰,实现很繁琐.分析过程可以参考LRJ,自己的总结晚些放. #include <cstdio> #include <cstring> #include <algo ...

  4. LA 3938 动态最大连续和(线段树)

    https://vjudge.net/problem/UVALive-3938 题意:给出一个长度为n的整数序列D,你的任务是对m个询问作出回答.对于询问(a,b),需要找到两个下标x和y,使得a≤x ...

  5. LA 3938 动态最大连续和

    题目链接:https://vjudge.net/contest/146667#problem/C 题意:动态的求一个区间的最大连续和. 分析: 看上去可以RMQ去做,但是,当分成两个部分,原来的部分的 ...

  6. leggere la nostra recensione del primo e del secondo

    La terra di mezzo in trail running sembra essere distorto leggermente massima di recente, e gli aggi ...

  7. Le lié à la légèreté semblait être et donc plus simple

    Il est toutefois vraiment à partir www.runmasterfr.com/free-40-flyknit-2015-hommes-c-1_58_59.html de ...

  8. 【HDU 3938】Portal (并查集+离线)

    http://acm.hdu.edu.cn/showproblem.php?pid=3938 两点之间建立传送门需要的能量为他们之间所有路径里最小的T,一条路径的T为该路径上最长的边的长度.现在 Q ...

  9. Mac Pro 使用 ll、la、l等ls的别名命令

    在 Linux 下习惯使用 ll.la.l 等ls别名的童鞋到 mac os 可就郁闷了~~ 其实只要在用户目录下建立一个脚本“.bash_profile”, vim .bash_profile 并输 ...

随机推荐

  1. linux系统读写缓存

    1.  操作系统缓存 在linux世界里,一切可读写设备都可看作是文件.文件cache设计的好坏直接影响着文件系统和磁盘的性能.最直观的是使用free命令看到的cached列. 这里面的cached列 ...

  2. Bootstrap基础--文本对齐风格

    在排版中离不开文本的对齐方式.在CSS中常常使用text-align来实现文本的对齐风格的设置.其中主要有四种风格: ☑  左对齐,取值left ☑  居中对齐,取值center ☑  右对齐,取值r ...

  3. PHP array_diff_uassoc()

    定义和用法 array_diff_uassoc() 函数使用用户自定义的回调函数 (callback) 做索引检查来计算两个或多个数组的差集.返回一个数组,该数组包括了在 array1 中但是不在任何 ...

  4. C++开发人脸性别识别总结

    历时一个月,最终在昨天把<C++开发人脸性别识别总结>系列博客完毕了,第一篇博客发表在2015年12月29日,截止昨天2016年2月29日最后一篇完毕,去除中间一个月的寒假,正好一个月,首 ...

  5. selenium使用chrome抓取自动消失弹框的方法

    selenium使用chrome抓取自动消失弹框的方法 转:https://blog.csdn.net/kennin19840715/article/details/76512394

  6. Coder-Strike 2014

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove? viewmode=contents    by---cxlove Qualification Round 略 Ro ...

  7. 在MTK平台里,,函数kal_prompt_trace起什么作用???Kal_prompt_trace的参数有表示什么?

    在MTK平台里,,函数kal_prompt_trace起什么作用???Kal_prompt_trace的参数有表示什么?一直弄不明白,但是很多函数的开头就是这个函数,,而且一般有三个参数-- kal_ ...

  8. C# 验证数字的正则表达式集

    验证数字的正则表达式集 博客分类: 正则 正则表达式 验证数字的正则表达式集 验证数字:^[0-9]*$ 验证n位的数字:^\d{n}$ 验证至少n位数字:^\d{n,}$ 验证m-n位的数字:^\d ...

  9. php 原生简版日志导出

    <?phpfunction writeLog($msg){ $logFile = date('Y-m-d').'.txt'; $msg = date('Y-m-d H:i:s').' >& ...

  10. codevs1358棋盘游戏(状压dp)

    1358 棋盘游戏  时间限制: 1 s  空间限制: 64000 KB  题目等级 : 大师 Master     题目描述 Description 这个游戏在一个有10*10个格子的棋盘上进行,初 ...