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. 【LeetCode】Longest Substring Without Repeating Characters 解题报告

    [题意] Given a string, find the length of the longest substring without repeating characters. For exam ...

  2. [Vue] Lazy Load a Route by using the Dynamic Import in Vue.js

    By default, vue-router doesn’t lazy load the routes unless you tell it to do it. Lazy loading of the ...

  3. spring mvc文件上传,request对象转换异常

    spring 文件上传有现成的工具用起来也挺简单.就是在还不是非常熟悉的时候可能会出一些错. 近期碰到了 org.apache.catalina.connector.RequestFacade can ...

  4. oop_day02_类、重载_20150810

    oop_day02_类.重载_20150810 1.怎样创建类?怎样创建对象? 2.引用类型之间画等号: 家门钥匙 1)指向同一个对象(数据有一份) 2)对当中一个引用的改动.会影响另外一个引用 基本 ...

  5. 设置用root用户telnet到linux系统

    默认情况下,ROOT用户不能以telnet方式连接Linux操作系统,而且也是不安全的.但从技术上来讲,是可以实现的. #mv /etc/securetty /etc/securetty.bak 保存 ...

  6. jQuery总结03

    1 控制网页元素属性和样式的 jQuery 方法有哪些? 2 利用 jQuery 插入网页节点的方法有哪些? 3 jQuery 中绑定事件是什么,如何解除绑定? 4 jQuery 中的动画效果包括哪些 ...

  7. poj--3061--Subsequence(贪心)

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10882   Accepted: 4498 Desc ...

  8. PCB 周期计算采用 SQL 函数调用.net Dll 标量函数 实现

    PCB 周期计算采用 SQL函数调用.net Dll实现 (实现代码重用目的) 玩过SQL SERVER数据库经常经需要写存储过程,函数之类的,当业务逻辑过于复杂,用SQL去写简直是恶梦, 这里以PC ...

  9. Integer应该用==还是equals

    问题引出:“Integer应该用==还是equals” 讨论这个问题之前我们先放一段代码 public static void main(String[] args) { Integer a1 = 2 ...

  10. 【NOI1999、LOJ#10019】生日蛋糕(搜索、最优化剪枝、可行性剪枝)

    主要是剪枝的问题,见代码,讲的很详细 #include<iostream> #include<cstdio> #include<cmath> #include< ...