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. .NET几大热点问题(.NET人员必读)

    下面收集了关于.NET几大热点问题及简要答案,防止我们回答问题不专业的尴尬.同一时候还将一些.NET资源的相关网址罗列一二. 一.什么是.Net?它主要包含什么?  .Net是为简化在第三代因特网的高 ...

  2. c# POST和GET方式通过server地址提交数据

    1:POST方式提交: <strong><span style="font-size:14px;">private static string HttpPo ...

  3. 2014秋C++ 第8周项目 分支程序设计

    课程主页在http://blog.csdn.net/sxhelijian/article/details/39152703.课程资源在云学堂"贺老师课堂"同步展示,使用的帐号请到课 ...

  4. 3D数学读书笔记——多坐标系和向量基础

    本系列文章由birdlove1987编写,转载请注明出处. 文章链接: http://blog.csdn.net/zhurui_idea/article/details/24662453 第一个知识点 ...

  5. 【Java并发编程实战】—– AQS(四):CLH同步队列

    在[Java并发编程实战]-–"J.U.C":CLH队列锁提过,AQS里面的CLH队列是CLH同步锁的一种变形. 其主要从双方面进行了改造:节点的结构与节点等待机制.在结构上引入了 ...

  6. 人见人爱A+B(杭电2033)

    /*人见人爱A+B Problem Description HDOJ上面已经有10来道A+B的题目了,相信这些题目以前是大家的最爱,希望今天的这个A+B能给大家带来好运.也希望这个题目能唤起大家对AC ...

  7. 【数据结构】链式向前星知识点&代码

    代码: struct NODE{ int to; int nxt; int c; }node[MM];//链式向前星 ; void add(int a,int b,int c){ node[lcnt] ...

  8. Linux Framebuffer 驱动框架之一概念介绍及LCD硬件原理【转】

    本文转载自:http://blog.csdn.net/liuxd3000/article/details/17464779 一.基本概念 帧缓冲(Framebuffer)是Linux系统为显示设备提供 ...

  9. 【撸码caffe四】 solver.cpp&&sgd_solver.cpp

    caffe中solver的作用就是交替低啊用前向(forward)算法和后向(backward)算法来更新参数,从而最小化loss,实际上就是一种迭代的优化算法. solver.cpp中的Solver ...

  10. leetcode数组相关

    目录 4寻找两个有序数组的中位数 11盛最多水的容器,42接雨水 15三数之和,16最接近的三数之和,18四数之和 26/80删除排序数组中的重复项, 27移除元素 31下一个排列 53最大子序和 5 ...