水果姐逛水果街Ⅰ(codevs 3304)
水果姐今天心情不错,来到了水果街。
水果街有n家水果店,呈直线结构,编号为1~n,每家店能买水果也能卖水果,并且同一家店卖与买的价格一样。
学过oi的水果姐迅速发现了一个赚钱的方法:在某家水果店买一个水果,再到另外一家店卖出去,赚差价。
就在水果姐窃喜的时候,cgh突然出现,他为了为难水果姐,给出m个问题,每个问题要求水果姐从第x家店出发到第y家店,途中只能选一家店买一个水果,然后选一家店(可以是同一家店,但不能往回走)卖出去,求每个问题中最多可以赚多少钱。
第一行n,表示有n家店
下来n个正整数,表示每家店一个苹果的价格。
下来一个整数m,表示下来有m个询问。
下来有m行,每行两个整数x和y,表示从第x家店出发到第y家店。
有m行。
每行对应一个询问,一个整数,表示面对cgh的每次询问,水果姐最多可以赚到多少钱。
10
2 8 15 1 10 5 19 19 3 5
4
6 6
2 8
2 2
6 3
0
18
0
14
0<=苹果的价格<=10^8
0<n,m<=200000
/*
对于每个区间,维护它的最小值,最大值,从左向右的最大收益,从右向左的最大收益
一个区间内的最大收益=max(左孩子最大收益,右孩子最大收益,右孩子最大值-左孩子最小值)
*/
#include<iostream>
#include<cstdio>
#define lson l,m,now*2
#define rson m+1,r,now*2+1
#define M 800010
using namespace std;
int mx[M],mn[M],lmax[M],rmax[M],a[M/];
int tot;
void push_up(int now)
{
mx[now]=max(mx[now*],mx[now*+]);
mn[now]=min(mn[now*],mn[now*+]);
lmax[now]=max(max(lmax[now*],lmax[now*+]),mx[now*+]-mn[now*]);
rmax[now]=max(max(rmax[now*],rmax[now*+]),mx[now*]-mn[now*+]);
} void build(int l,int r,int now)
{
if(l==r)
{
mx[now]=mn[now]=a[l];
return;
}
int m=(l+r)/;
build(lson);
build(rson);
push_up(now);
} int get_max(int x,int y,int l,int r,int now)
{
if (x<=l&& y>=r) return mx[now];
int m = (l+r)/;
if (x>m) return get_max(x,y,rson);
else if (y<=m) return get_max(x,y,lson);
else return max(get_max(m+,y,rson),get_max(x,m,lson));
} int get_min(int x,int y,int l,int r,int now)
{
if (x<=l&& y>=r) return mn[now];
int m = (l+r)/;
if (x>m) return get_min(x,y,rson);
else if (y<=m) return get_min(x,y,lson);
else return min(get_min(m+,y,rson),get_min(x,m,lson));
} int query_l(int x,int y,int l,int r,int now)
{
int m=(l+r)/;
if (x<=l&& y>=r) return lmax[now];
if (y<=m) return query_l(x,y,lson);
else if (x>m) return query_l(x,y,rson);
else
{
int temp = ;
temp=max(query_l(x,y,lson),query_l(x,y,rson));
temp=max(temp,get_max(x,y,rson)-get_min(x,y,lson));
return temp;
}
}
int query_r(int x,int y,int l,int r,int now)
{
int m=(l+r)/;
if (x<=l&& y>=r) return rmax[now];
if (y<=m) return query_r(x,y,lson);
else if (x>m) return query_r(x,y,rson);
else
{
int temp = ;
temp=max(query_r(x,m,lson),query_r(x,y,rson));
temp=max(temp,get_max(x,y,lson)-get_min(x,y,rson));
return temp;
}
}
int main()
{
int n,m,l,r;
scanf("%d",&n);
for (int i=;i<=n;i++) scanf("%d",&a[i]);
build(,n,);
scanf("%d",&m);
for (int i=;i<=m;i++)
{
scanf("%d%d",&l,&r);
if (l<r) printf("%d\n",query_l(l,r,,n,));
else printf("%d\n",query_r(r,l,,n,));
}
}
水果姐逛水果街Ⅰ(codevs 3304)的更多相关文章
- 水果姐逛水果街Ⅱ codevs 3305
3305 水果姐逛水果街Ⅱ 时间限制: 2 s 空间限制: 256000 KB 题目描述 Description 水果姐第二天心情也很不错,又来逛水果街. 突然,cgh又出现了.cgh施展了魔 ...
- code vs 3305 水果姐逛水果街Ⅱ
3305 水果姐逛水果街Ⅱ 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 水果姐第二天心情也很不错, ...
- Codevs 3304 水果姐逛水果街Ⅰ 线段树
题目: http://codevs.cn/problem/3304/ 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 D ...
- CodeVs——T 3304 水果姐逛水果街Ⅰ
http://codevs.cn/problem/3304/ 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Des ...
- Codevs 3305 水果姐逛水果街Ⅱ 倍增LCA
题目:http://codevs.cn/problem/3305/ 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Des ...
- CodeVs——T 3305 水果姐逛水果街Ⅱ
http://codevs.cn/problem/3305/ 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 De ...
- codevs 3305 水果姐逛水果街Ⅱ&&codevs3006
题目描述 Description 水果姐第二天心情也很不错,又来逛水果街. 突然,cgh又出现了.cgh施展了魔法,水果街变成了树结构(店与店之间只有一条唯一的路径). 同样还是n家水果店,编号为1~ ...
- codevs3305 水果姐逛水果街Ⅱ
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...
- codevs3304 水果姐逛水果街
题目描述 Description 水果姐今天心情不错,来到了水果街. 水果街有n家水果店,呈直线结构,编号为1~n,每家店能买水果也能卖水果,并且同一家店卖与买的价格一样. 学过oi的水果姐迅速发现了 ...
随机推荐
- Code Review Engine Learning
相关学习资料 https://www.owasp.org/index.php/Code_review https://www.owasp.org/images/8/8e/OWASP_Code_Revi ...
- C#图片读取和保存
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 为什么mvc里面的ModelState.IsValid一只都是true
http://zhidao.baidu.com/link?url=H69JQBpF8vbJEOUUc1RCjRZZ05gSGn6PiPL740aGgR3qIfFTT__pt4KgEg7O47lReYR ...
- Jquery-easyUI-datagrid参数之 queryParams
http://blog.163.com/xpf_designer/blog/static/19213618920117784055668/ Html <div region="cen ...
- Enum类型 枚举内部值/名
enum Days { Nothing=0, Mon=1, Stu=2 } static void Main(string[] args) { foreach (int item in Enum.Ge ...
- Win7下JDK环境变量的设置
JDK并不像Microsoft阵营vs那样智能,安装好后所有的东西都给你配置好了,我们还没需要手动配置很多东西 首先说为什么要配置JDK的环境变量在任何路径下识别java命令和java类 配置分为2个 ...
- centos 配置固定ip
centos下网络配置方法(网关.dns.ip地址配置) 来源:互联网 作者:佚名 时间:07-13 00:32:07 [大 中 小] 本文介绍了centos网络配置的方法,centos网络配置主要包 ...
- thinkphp开发规范
1.编写目的 为了更好的提高技术部的工作效率,保证开发的有效性和合理性,并可最大程度的提高程序代码的可读性和可重复利用性,指定此规范.开发团队根据自己的实际情况,可以对本规范进行补充或裁减. ...
- 线性判别分析(LDA)准则:FIsher准则、感知机准则、最小二乘(最小均方误差)准则
准则 采用一种分类形式后,就要采用准则来衡量分类的效果,最好的结果一般出现在准则函数的极值点上,因此将分类器的设计问题转化为求准则函数极值问题,即求准则函数的参数,如线性分类器中的权值向量. 分类器设 ...
- LWP 轻量级线程的意义与实现
转子 http://www.ibm.com/developerworks/cn/linux/kernel/l-thread/ 二.Linux 2.4内核中的轻量进程实现 最初的进程定义都包含程序.资源 ...