UVA 1400 - "Ray, Pass me the dishes!"

option=com_onlinejudge&Itemid=8&page=show_problem&category=501&problem=4146&mosmsg=Submission+received+with+ID+13958986" target="_blank" style="">题目链接

题意:给定一个序列,每次询问一个[L,R]区间。求出这个区间的最大连续子序列和

思路:线段树,每一个节点维护3个值。最大连续子序列。最大连续前缀序列,最大连续后缀序列,那么每次pushup的时候,依据这3个序列去拼凑得到新的一个结点就可以

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; #define lson(x) ((x<<1) + 1)
#define rson(x) ((x<<1) + 2)
#define MP(a, b) make_pair(a, b) typedef long long ll;
typedef pair<int, int> Point; const int N = 500005; int n, m;
ll a[N], sum[N]; struct Node {
int l, r;
int prex, sufx;
Point sub;
} node[4 * N]; ll get(Point x) {
return sum[x.second] - sum[x.first - 1];
} bool Max(Point a, Point b) {
long long sa = get(a);
long long sb = get(b);
if (sa != sb) return sa > sb;
return a < b;
} Point Maxsub(Node a, Node b) {
Point ans;
if (Max(a.sub, b.sub)) ans = a.sub;
else ans = b.sub;
if (Max(MP(a.sufx, b.prex), ans)) ans = MP(a.sufx, b.prex);
return ans;
} int Maxpre(Node a, Node b) {
Point ans = MP(a.l, a.prex);
if (Max(MP(a.l, b.prex), ans)) ans = MP(a.l, b.prex);
return ans.second;
} int Maxsuf(Node a, Node b) {
Point ans = MP(b.sufx, b.r);
if (Max(MP(a.sufx, b.r), ans)) ans = MP(a.sufx, b.r);
return ans.first;
} Node pushup(Node a, Node b) {
Node ans;
ans.l = a.l; ans.r = b.r;
ans.sub = Maxsub(a, b);
ans.prex = Maxpre(a, b);
ans.sufx = Maxsuf(a, b);
return ans;
} void build(int l, int r, int x) {
if (l == r) {
node[x].l = l; node[x].r = r;
node[x].prex = node[x].sufx = l;
node[x].sub = MP(l, l);
return ;
}
int mid = (l + r) / 2;
build(l, mid, lson(x));
build(mid + 1, r, rson(x));
node[x] = pushup(node[lson(x)], node[rson(x)]);
} Node Query(int l, int r, int x) {
if (l <= node[x].l && r >= node[x].r)
return node[x];
int mid = (node[x].l + node[x].r) / 2;
Node ans;
if (l <= mid && r > mid)
ans = pushup(Query(l, r, lson(x)), Query(l, r, rson(x)));
else if (l <= mid) ans = Query(l, r, lson(x));
else if (r > mid) ans = Query(l, r, rson(x));
return ans;
} int main() {
int cas = 0;
while (~scanf("%d%d", &n, &m)) {
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
sum[i] = sum[i - 1] + a[i];
}
build(1, n, 0);
printf("Case %d:\n", ++cas);
int a, b;
while (m--) {
scanf("%d%d", &a, &b);
Node ans = Query(a, b, 0);
printf("%d %d\n", ans.sub.first, ans.sub.second);
}
}
return 0;
}

UVA 1400 1400 - &quot;Ray, Pass me the dishes!&quot;(线段树)的更多相关文章

  1. UVA 1400."Ray, Pass me the dishes!" -分治+线段树区间合并(常规操作+维护端点)并输出最优的区间的左右端点-(洛谷 小白逛公园 升级版)

    "Ray, Pass me the dishes!" UVA - 1400 题意就是线段树区间子段最大和,线段树区间合并,但是这道题还要求输出最大和的子段的左右端点.要求字典序最小 ...

  2. UVALive3938 &quot;Ray, Pass me the dishes!&quot; 线段树动态区间最大和

    AC得相当辛苦的一道题.似乎不难,可是须要想细致, 開始的时候的错误思路----是受之前做过的区间最长连续子串影响http://blog.csdn.net/u011026968/article/det ...

  3. UvaLA 3938 "Ray, Pass me the dishes!"

                            "Ray, Pass me the dishes!" Time Limit: 3000MS   Memory Limit: Unkn ...

  4. 【LA3938】"Ray, Pass me the dishes!"

    原题链接 Description After doing Ray a great favor to collect sticks for Ray, Poor Neal becomes very hun ...

  5. UVa 1400 (线段树) "Ray, Pass me the dishes!"

    求一个区间的最大连续子序列,基本想法就是分治,这段子序列可能在区间的左半边,也可能在区间的右半边,也有可能是横跨区间中点,这样就是左子区间的最大后缀加上右子区间的最大前缀之和. 线段树维护三个信息:区 ...

  6. uva 1400 - "Ray, Pass me the dishes!"

    又是一道线段树区间更新的题: #include<cstdio> #include<algorithm> #include<cstring> #define ll l ...

  7. uva 1400 "Ray, Pass me the dishes!" (区间合并 最大子段和+输出左右边界)

    题目链接:https://vjudge.net/problem/UVA-1400 题意:给一串序列,求最大子段,如果有多个,输出字典序最小的那个的左右端点 思路: 之前写过类似的,这个麻烦点需要输出左 ...

  8. 1400 - "Ray, Pass me the dishes!"

    哈哈,原来题意看错了,但有多个解的时候,输出起点靠前的,如果起点一样,则输出终点靠前的,修改后AC的代码如下: #include <cstdio> #include <iostrea ...

  9. 线段树(区间合并) LA 3989 "Ray, Pass me the dishes!"

    题目传送门 题意:动态最大连续子序列和,静态的题目 分析:nlogn的归并思想.线段树维护结点的三个信息,最大前缀和,最大后缀和,该区间的最大和的两个端点,然后答案是三个的better.书上用pair ...

随机推荐

  1. JavaScript中函数的调用

    JavaScript中函数的调用 制作人:全心全意 在JavaScript中,函数定义后并不会自动执行,要执行一个函数需要在特定的位置调用该函数,调用函数需要创建调用语句,调用语句包含函数名称和参数. ...

  2. Turtle库学习

    Python Turtle (Python绘图工具) 导入库 import turtle as t ps:为了方便调用我们这里给这个模块在本程序内重命名为 t 1. 画布 顾名思义就是用于绘图的区域 ...

  3. Centos6.5安装Nexus及安装时的一些错误

    注意:此篇博文未有配置部分,有需求的同学只能自行寻找了-- 1.下载: https://www.sonatype.com/download-oss-sonatype 2.官方推荐安装在/opt目录下 ...

  4. Android开发——获取应用数据/缓存大小并清理缓存

    1. 获取应用数据/缓存大小 其中pm为实例化的PackageManager,因为需要遍历所有的已安装的应用.因此需要开启子线程进行处理. 还有需要注意的是,在Android4.2之前getPacka ...

  5. python3 时间复杂度

    时间复杂度 (1)时间频度 一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才能知道.但我们不可能也没有必要对每个算法都上机测试,只需知道哪个算法花费的时间多,哪个算法花费的时间少就 ...

  6. RPC实现的底层原理及应用

    摘要:RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC协议假定某些传输协议 ...

  7. [luoguP2129] L国的战斗续之多路出击(模拟 || 矩阵)

    传送门 1.模拟 easy #include <cstdio> #define N 500001 int n, m; int X[N], Y[N], x[N], y[N], a = 1, ...

  8. hdu3516 Tree Construction (四边形不等式)

    题意:给定一些点(xi,yi)(xj,yj)满足:i<j,xi<xj,yi>yj.用下面的连起来,使得所有边的长度最小? 题解:直接给出吧 f[i][j]=min(f[i][k]+f ...

  9. Java设计模式之(工厂模式)

    工厂模式: 工厂模式可以分为三类: 1)简单工厂模式(Simple Factory) 2)工厂方法模式(Factory Method) 3)抽象工厂模式(Abstract Factory) 简单工厂模 ...

  10. Python 列表的复制操作

    2013-10-18 10:07:03|   import copy a = [1,2,3,['a','b']] b = a c = a[:] d = copy.copy(a) e = copy.de ...