水果姐逛水果街Ⅰ(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的水果姐迅速发现了 ...
随机推荐
- Windows Management Instrumentation WMI Security Technology Learning
目录 . 引言 . WMI(Windows Management Instrumentation)简介 . 基于WMI的攻击向量 . WMI编程示例 0. 引言 在进行服务器主机的入侵检测.安全攻防的 ...
- java可变参数例子:求学生成绩信息,不确定课程数
可变参数特点: 1)...只能出现在参数列表的最后2)...位于变量类型和变量名之间3)调用可变参数的方法时,编译器为该可变参数隐含创建一个数组,在方法体中以数组的形式访问可变参数 //可变参数也可用 ...
- Linux下修改计算机名
SuSe操作系统: 1. 修改/etc/HOSTNAME 文件 ,其内容为计算机名. 输入命令:vi /etc/HOSTNAME 使用键盘上的 x 键一个一个删除所有内容 ,然后使用键盘上的 i ...
- C# CryptoStream
using System; using System.IO; using System.Security.Cryptography; namespace RijndaelManaged_Example ...
- swift中文文档翻译之--字符串和字符
字符串和字符 A string is an ordered collection of characters, such as "hello, world" or "al ...
- ASP.NET MVC学习笔记-----ActionInvoker
还是这张图: 当ControllerFactory生成Controller实例后,这时就需要使用ActionInvoker来选择调用一个合适的Action执行.ASP.NET MVC提供的基类Cont ...
- C语言中的struct和typedef struct<转载>
原文:http://www.nowamagic.net/librarys/veda/detail/1785 typedef为C语言的关键字,作用是为一种数据类型定义一个新名字.这里的数据类型包括内部数 ...
- 自动获取wordpress日志中的第一张图片作为缩略图
图片在博客中算是吸引访客阅读欲望的一种方法,在日志列表如果有一张吸引力十足的图片作为缩略图,70%的游客会点击浏览具体的文章.既然那样,赶紧去加缩略图吧. 我们知道 WordPress 有个日志缩略图 ...
- 新一代 Tor发布,它牛在哪里?
导读 知名匿名搜索引擎Tor,最近发布了基于火狐浏览器45-ESR的6.0版本,增强了对HTML5的支持,并更新了用来保护加密流量及其更新机制的安全功能.火狐45-ESR版本的全称为Firefox E ...
- OJ 1188 全排列---康托展开
题目描述 求n的从小到大第m个全排列(n≤20). 输入 n和m 输出 输出第m个全排列,两个数之间有一空格. 样例输入 3 2 样例输出 1 3 2 #include<cstdio> # ...