UVA 1400 1400 - "Ray, Pass me the dishes!"(线段树)
UVA 1400 - "Ray, Pass me the dishes!"
题意:给定一个序列,每次询问一个[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 - "Ray, Pass me the dishes!"(线段树)的更多相关文章
- UVA 1400."Ray, Pass me the dishes!" -分治+线段树区间合并(常规操作+维护端点)并输出最优的区间的左右端点-(洛谷 小白逛公园 升级版)
"Ray, Pass me the dishes!" UVA - 1400 题意就是线段树区间子段最大和,线段树区间合并,但是这道题还要求输出最大和的子段的左右端点.要求字典序最小 ...
- UVALive3938 "Ray, Pass me the dishes!" 线段树动态区间最大和
AC得相当辛苦的一道题.似乎不难,可是须要想细致, 開始的时候的错误思路----是受之前做过的区间最长连续子串影响http://blog.csdn.net/u011026968/article/det ...
- UvaLA 3938 "Ray, Pass me the dishes!"
"Ray, Pass me the dishes!" Time Limit: 3000MS Memory Limit: Unkn ...
- 【LA3938】"Ray, Pass me the dishes!"
原题链接 Description After doing Ray a great favor to collect sticks for Ray, Poor Neal becomes very hun ...
- UVa 1400 (线段树) "Ray, Pass me the dishes!"
求一个区间的最大连续子序列,基本想法就是分治,这段子序列可能在区间的左半边,也可能在区间的右半边,也有可能是横跨区间中点,这样就是左子区间的最大后缀加上右子区间的最大前缀之和. 线段树维护三个信息:区 ...
- uva 1400 - "Ray, Pass me the dishes!"
又是一道线段树区间更新的题: #include<cstdio> #include<algorithm> #include<cstring> #define ll l ...
- uva 1400 "Ray, Pass me the dishes!" (区间合并 最大子段和+输出左右边界)
题目链接:https://vjudge.net/problem/UVA-1400 题意:给一串序列,求最大子段,如果有多个,输出字典序最小的那个的左右端点 思路: 之前写过类似的,这个麻烦点需要输出左 ...
- 1400 - "Ray, Pass me the dishes!"
哈哈,原来题意看错了,但有多个解的时候,输出起点靠前的,如果起点一样,则输出终点靠前的,修改后AC的代码如下: #include <cstdio> #include <iostrea ...
- 线段树(区间合并) LA 3989 "Ray, Pass me the dishes!"
题目传送门 题意:动态最大连续子序列和,静态的题目 分析:nlogn的归并思想.线段树维护结点的三个信息,最大前缀和,最大后缀和,该区间的最大和的两个端点,然后答案是三个的better.书上用pair ...
随机推荐
- 记录一个scrapy的坑
在win10上用命令 pip install scrapy 安装scrapy的时候提示: Could not find a version that satisfies the requirement ...
- luogu P2078 朋友
题目背景 小明在A公司工作,小红在B公司工作. 题目描述 这两个公司的员工有一个特点:一个公司的员工都是同性. A公司有N名员工,其中有P对朋友关系.B公司有M名员工,其中有Q对朋友关系.朋友的朋友一 ...
- InnoDB体系架构总结(一)
缓冲池: 是一块内存区域,通过内存的速度来弥补磁盘速度较慢对数据库性能的影响.在数据库中读取的页数据会存放到缓冲池中,下次再读取相同页的时候,会首先判断该页是否在缓冲池中.对于数据库中页的修改操 ...
- POJ 1463 Strategic game(树形DP入门)
题意: 给定一棵树, 问最少要占据多少个点才能守护所有边 分析: 树形DP枚举每个点放与不放 树形DP: #include<cstdio> #include<iostream> ...
- Insert or Merge
7-13 Insert or Merge(25 分) According to Wikipedia: Insertion sort iterates, consuming one input elem ...
- Spring异常——BeanNotOfRequiredTypeException
使用junit测试ssh搭建的框架的时候,遇到了一个异常: 异常消息: org.springframework.beans.factory.BeanCreationException: Error c ...
- 【Ts 4】ftp服务器搭建
一.为什么需要ftp? 分布式环境一般都有一个专门的图片服务器存放图片.我们使用虚拟机搭建一个专门的服务器来存放图片.在此服务器上安装一个nginx来提供http服务,安装一个ftp服务器来提供图片上 ...
- hexo干货系列:(三)hexo的Jacman主题优化
前言 上一篇介绍了Jacman主题的安装和配置,今天根据上次的基础做了些优化,让博客看起来很舒服. 正文 首页文章展示摘要 该主题首页文章列表默认是全部展开,感觉不好,我关闭掉了,只展示少量摘要. 修 ...
- HDU 2222 最简单的AC自动机套模板应用
HDU 2222 题意:给出N(N<=10,000)个单词,每个单词长度不超过50.再给出一个字符串S,字符串长度不超过1,000,000.问有多少个单词出现在了字符串S中.(单词可能重复,单词 ...
- springmvc和dubbo整合时,不配置spring listener报错找不到/WEB-INF/config/applicationContext.xml
原因,dubbo2.6.3版本开始就需要先在listener中配置容器,否则报错,2.6.2版本则不需要