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. UVA 10859 树形DP

    很明显的树形DP了,设状态dp[i][0],dp[i][1].枚举子节点放或不放的两种状态. 在此学到一种不同于一般处理的方法,题目要求被两灯照亮的边尽量多,反过来即被一灯照亮的尽量少设为e.又需要的 ...

  2. jsp上传下载+SmartUpload插件上传

    使用之前须要自己下载jspSmartUpload.jar包 这里找到一个支持中文的jar包,下载地址例如以下: http://www.blogjava.net/Files/hijackwust/jsm ...

  3. centos下配置防火墙port失败

    问题:将规则加入到防火墙中.总是port无法开启 (1)改动文件 首先vim /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp ...

  4. Window.open()打开一个窗体不被拦截

    Window.open()打开一个窗体不被拦截     在DataGrid中建一个模板列,在模板列中放一个客户端的Button,或者直接写你要的字句,然后用<a href>连接例:< ...

  5. TCP打开文件传输(客户端code)

    #include <stdio.h>#include <stdlib.h>#include <arpa/inet.h>#include <sys/types. ...

  6. Device /dev/sdb1 not found (or ignored by filtering)

    /etc/lvm/lvm.conf filters

  7. E20170905-mk

    recursive   adj. 回归的,递归的;

  8. El表达式日期处理

    第1步:引入指令  <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt " %&g ...

  9. SPOJ GSS1 & GSS3&挂了的GSS5

    线段树然后yy一下,搞一搞. GSS1: 题意:求最大区间和. #include <cstdio> #include <algorithm> using namespace s ...

  10. strcpy自实现

    为了避免strcpy源串覆盖问题(P220),自实现strcpy. #include <stdio.h> #include <string.h> #include <as ...