题目描述 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. 使用python来调试串口

    串口模块的波特率比较特别,找了几个串口工具都不支持...所以,干脆用python自己来写了,其实已经好奇好久了,别人的工具各种不顺手. 需要pyserial的支持,兼容各种平台,不需要新编译二进制文件 ...

  2. excel公式处理成绩表

    一共有2个需求: 1.平均分:所有每个人的成绩/29;及格率:60分的/29;优秀率:80分/29 2.对总分进行排序,并在另一列中生成排名 平均分:=(c3+c4+.......c31)/29 及格 ...

  3. java + jquery + ajax + json 交互

    前端js部分: $.ajax({ async:true, cache:false, type:"POST", dataType : 'json', url:"/shopp ...

  4. uva 10723 Cyborg Genes(LCS变形)

    题目:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=107450#problem/C 题意:输入两个字符串,找一个最短的串,使得输入的两个 ...

  5. 项目总结—jQuery EasyUI- DataGrid使用

    http://blog.csdn.net/zwk626542417/article/details/18839349 概要 jQuery EasyUI是一个基于jquery的集成了各种用户界面的框架, ...

  6. 输入你的性别,身高及体重,判断你的身材是否标准。(用if......else语句)

    Console.WriteLine("请输入你的性别,身高和体重:"); string s = Console.ReadLine(); double h = double.Pars ...

  7. Hibernate动态更新

    Hibernate一级缓存: Hibernate有一级和二级缓存.一级缓存就是Session. 在Hibernate启动的时候,SessionFactory解析映射文件的时候,会根据映射文件中的字段生 ...

  8. Java基础之理解Annotation(与@有关,即是注释)

    Java基础之理解Annotation 一.概念 Annontation是Java5开始引入的新特征.中文名称一般叫注解.它提供了一种安全的类似注释的机制,用来将任何的信息或元数据(metadata) ...

  9. Linux rpm 命令参数使用详解[介绍和应用](转)

    RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序” rpm 执行安装包二进制包(Binary)以及源代码包(Source)两种 ...

  10. JAVA 利用JNI加密class文件/自定义ClassLoader 类

    利用 JNI 对bytecode 加密.不影响java程序员的正常开发.09年的时候写的,现在拿出来晒晒————————————————————————————混淆才是王道,如果混淆再加密就更酷了.. ...