LA 3938
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 [a, b] <tex2html_verbatim_mark>(inclusive) to represent all the dishes a, a + 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 (n, m < 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的更多相关文章
- LA 3938 动态最大连续和 线段树
题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- la 3938(未完成)
题意:给出一个长度为n的整数序列D,你的任务是对m个询问作出回答.对于询问(a,b), 需要找到两个下标x和y,使得a≤x≤y≤b,并且Dx+Dx+1+...+Dy尽量大. 如果有多组满足条件的x和y ...
- LA 3938 动态最大连续区间 线段树
思路很清晰,实现很繁琐.分析过程可以参考LRJ,自己的总结晚些放. #include <cstdio> #include <cstring> #include <algo ...
- LA 3938 动态最大连续和(线段树)
https://vjudge.net/problem/UVALive-3938 题意:给出一个长度为n的整数序列D,你的任务是对m个询问作出回答.对于询问(a,b),需要找到两个下标x和y,使得a≤x ...
- LA 3938 动态最大连续和
题目链接:https://vjudge.net/contest/146667#problem/C 题意:动态的求一个区间的最大连续和. 分析: 看上去可以RMQ去做,但是,当分成两个部分,原来的部分的 ...
- 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 ...
- 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 ...
- 【HDU 3938】Portal (并查集+离线)
http://acm.hdu.edu.cn/showproblem.php?pid=3938 两点之间建立传送门需要的能量为他们之间所有路径里最小的T,一条路径的T为该路径上最长的边的长度.现在 Q ...
- Mac Pro 使用 ll、la、l等ls的别名命令
在 Linux 下习惯使用 ll.la.l 等ls别名的童鞋到 mac os 可就郁闷了~~ 其实只要在用户目录下建立一个脚本“.bash_profile”, vim .bash_profile 并输 ...
随机推荐
- Codeforces Round #303 (Div. 2) E
五道水题,但要手快才好...我手慢了,E题目都没看完TAT.... 想了一发,很水,就是一遍Dijk即可,使用优先队列,同时记录由哪条边转移而来 #include <iostream> # ...
- HDU 4363
这题是记忆化搜索很容易想到,但状态却不好设 dp[i][j][u][d][l][r][k].对于矩形为i*j,它的四周的颜色分别为u,d,l,r,横竖切的状态为k的种数. 其中要注意一个问题是,停止不 ...
- ZOJ 3213
/* ZOJ 3213 好吧,看过那种括号表示法后,就崩溃了,实在受不了.情况复杂,写了两天,人也有点傻X了,只能放弃,转而用最小表示法. 最小表示法不难写: 1)首先,要承认路径上有格子不选的情况, ...
- 2.3-STP生成树
2.3-STP生成树 单点失效(signle point of failure)及其解决方法: 当两个Segement之间只有一条物理连接时就有可能出现单点失效→ 单方面的故障导致全网 ...
- hadoop常见操作命令
1.查看指定文件夹下内容 hadoop dfs –ls [文件文件夹] eg: hadoop dfs –ls /user/wangkai.pt 2.打开某个已存在文件 hadoop dfs –cat ...
- ubuntu下C程序的编辑、编译、运行
均以hello world程序为例 一.vim hello.c/hello.cpp 创建hello.c/hello.cpp文件,并进入vim界面 二.此时按键盘上的很多键均不会有任何反应,键入i,进入 ...
- openSTack manual 整合调优
- 82.角色管理Extjs 页面
1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8&quo ...
- python使用MySQLdb向mySQL批量插入数据的方法
该功能通过调用mySQLdb python库中的 cursor.executemany()函数完成批量处理. 今天用这个函数完成了批量插入 例程: def test_insertDB(options) ...
- 《疯狂Python讲义》重要笔记——Python简介
简介 Python是一种面向对象.解释型.弱类型的脚本语言,同时也是一种功能强大的通过语言,它提供了高效的高级数据结构,还有简单有效的面向对象编程. 在大数据.人工智能(AI)领域应用广泛,因此变得流 ...