题目描述 Description

水果姐今天心情不错,来到了水果街。

水果街有n家水果店,呈直线结构,编号为1~n,每家店能买水果也能卖水果,并且同一家店卖与买的价格一样。

学过oi的水果姐迅速发现了一个赚钱的方法:在某家水果店买一个水果,再到另外一家店卖出去,赚差价。

就在水果姐窃喜的时候,cgh突然出现,他为了为难水果姐,给出m个问题,每个问题要求水果姐从第x家店出发到第y家店,途中只能选一家店买一个水果,然后选一家店(可以是同一家店,但不能往回走)卖出去,求每个问题中最多可以赚多少钱。

输入描述 Input Description

第一行n,表示有n家店

下来n个正整数,表示每家店一个苹果的价格。

下来一个整数m,表示下来有m个询问。

下来有m行,每行两个整数x和y,表示从第x家店出发到第y家店。

输出描述 Output Description

有m行。

每行对应一个询问,一个整数,表示面对cgh的每次询问,水果姐最多可以赚到多少钱。

样例输入 Sample Input

10
2 8 15 1 10 5 19 19 3 5 
4
6 6
2 8
2 2
6 3

样例输出 Sample Output

0
18
0
14

数据范围及提示 Data Size & Hint

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)的更多相关文章

  1. 水果姐逛水果街Ⅱ codevs 3305

    3305 水果姐逛水果街Ⅱ  时间限制: 2 s  空间限制: 256000 KB   题目描述 Description 水果姐第二天心情也很不错,又来逛水果街. 突然,cgh又出现了.cgh施展了魔 ...

  2. code vs 3305 水果姐逛水果街Ⅱ

    3305 水果姐逛水果街Ⅱ  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 水果姐第二天心情也很不错, ...

  3. Codevs 3304 水果姐逛水果街Ⅰ 线段树

    题目: http://codevs.cn/problem/3304/   时间限制: 2 s   空间限制: 256000 KB   题目等级 : 钻石 Diamond 题解       题目描述 D ...

  4. CodeVs——T 3304 水果姐逛水果街Ⅰ

    http://codevs.cn/problem/3304/ 时间限制: 2 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Des ...

  5. Codevs 3305 水果姐逛水果街Ⅱ 倍增LCA

    题目:http://codevs.cn/problem/3305/  时间限制: 2 s   空间限制: 256000 KB   题目等级 : 钻石 Diamond 题解       题目描述 Des ...

  6. CodeVs——T 3305 水果姐逛水果街Ⅱ

    http://codevs.cn/problem/3305/  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 De ...

  7. codevs 3305 水果姐逛水果街Ⅱ&&codevs3006

    题目描述 Description 水果姐第二天心情也很不错,又来逛水果街. 突然,cgh又出现了.cgh施展了魔法,水果街变成了树结构(店与店之间只有一条唯一的路径). 同样还是n家水果店,编号为1~ ...

  8. codevs3305 水果姐逛水果街Ⅱ

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  9. codevs3304 水果姐逛水果街

    题目描述 Description 水果姐今天心情不错,来到了水果街. 水果街有n家水果店,呈直线结构,编号为1~n,每家店能买水果也能卖水果,并且同一家店卖与买的价格一样. 学过oi的水果姐迅速发现了 ...

随机推荐

  1. Linux 基础网络设置

    一.查看以及测试网络 查看及测试网络配置是管理Linux网络服务的第一步,本节将学习Linux系统中的网络查看以及测试命令.其中讲解的大多数命令以普通用户权限就可以完成操作,但是普通用户在执行&quo ...

  2. Ubuntu14.04编译安装mysql5.6.26

    Ubuntu14.04编译安装mysql5.6.26 (1)安装编译源码需要的包 sudo apt-get install make cmake gcc g++ bison libncurses5-d ...

  3. Ognl基本使用

    ---恢复内容开始--- Ognl默认是从“根”中取数据的 下面Demo中用的是 Ognl.getValue(String expression, Map context, Object root) ...

  4. Max批量导出工具

    Max批量导出工具 http://www.paulneale.com/scripts/batchItMax/batchItMax.htm Scripts Batch It Max: Batch It ...

  5. ssh事务配置

    <!-- 配置业务层 --> <bean id="employeeService" class="cn.bdqn.jboa.service.impl.E ...

  6. mybatis 使用resultMap实现数据库的操作

    resultType:直接表示返回类型 resultMap:对外部resultMap的引用 二者不能同时使用 创建一个实体类Role和User public class Role { private ...

  7. 通过开源程序同时解决DNS劫持和DNS污染的问题

    我们知道,某些网络运营商为了某些目的,对DNS进行了某些操作,导致使用ISP的正常上网设置无法通过域名取得正确的IP地址.常用的手段有:DNS劫持和DNS污染.关于DNS劫持和DNS污染的区别,请查找 ...

  8. Cannot find class [org.apache.commons.dbcp.BasicDataSource]

    错误:Cannot find class [org.apache.commons.dbcp.BasicDataSource] 原因:缺少commons-dbcp.jar

  9. sudo: unable to resolve host ubuntu提示的解决

    http://blog.sina.com.cn/s/blog_6c9d65a1010180mg.html

  10. [转载]angularjs学习总结 详细教程

    http://blog.csdn.net/yy374864125/article/details/41349417#t75 目录(?)[-] 前言 AngularJS概述 AngularJS是什么 A ...