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 ...
随机推荐
- 暴力破解UltraEdit
使用x32dbg(x64dbg)做为破解工具: 使用x64dbg 打开 udeit64.exe 点击运行,直到UltraEdit启动,转到符号页: 找到: CheckForUpdatesNoPromp ...
- ubuntu 宝塔安装一条龙服务
ubuntu 安装 1, wget -O install.sh http://download.bt.cn/install/install-ubuntu.sh && sudo bash ...
- 自动化测试如何解析excel文件?
前言 自动化测试中我们存放数据无非是使用文件或者数据库,那么文件可以是csv,xlsx,xml,甚至是txt文件,通常excel文件往往是我们的首选,无论是编写测试用例还是存放测试数据,excel都是 ...
- go语言的碎片整理:time
时间和日期是我们编程中经常用到的,本文主要介绍了Go语言内置的time包的基本用法. Go语言中导入包 单行导入 import "time" import "fmt&qu ...
- luogu2894 [USACO08FEB]酒店Hotel
跟线段树求区间最值一样每个节点维护左边开始的最大连续空房间数.右边开始的最大连续空房间数.这个区间内的最大连续空房间数 #include <iostream> #include <c ...
- 如何使用jmeter进行并发登录测试
第一种方案直接从数据库中获取账号和密码 1.设置线程数为20 ,我们的并发用户量就是20个用户同时登录 2.添加定时器 3.设置集合点,当用户数量达到20个的时候再同时请求进行登录操作 4.添加配置元 ...
- 数据库服务器的监控 赛门铁克 Veritas i3 APM 查找指定时间段最耗服务器资源的TopSQL
- 五、PL/SQL循环、游标、函数和过程
--PL/SQL基础知识学习 --一.PL/SQL语句块,基础语法格式 DECLARE --变量声明列表 info varchar(25); --变量声明 stu_unm integer := 15; ...
- Linux 下Python2.7解决list打印中文字符问题
在写一个爬取智联招聘数据的爬虫中,将所需内容匹配到后打印出现了utf-8字符,并没有出现中文字符. 例如: >>>listnine = ['梨', '橘子', '苹果', '香蕉'] ...
- python012 Python3 编程第一步
Python3 编程第一步在前面的教程中我们已经学习了一些 Python3 的基本语法知识,下面我们尝试来写一个斐波纳契数列.实例如下: #!/usr/bin/python3 # Fibonacci ...