3304 水果姐逛水果街Ⅰ

 时间限制: 2 s
 空间限制: 256000 KB
 题目等级 : 钻石 Diamond
 
题目描述 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

/*
这一道题是有一排商店,可以买水果也可以卖水果,买水果和卖水果的价钱一样。
问你从商店x走到商店y,买卖所得最大收益是多少。
我们可以发现朴素的办法是一路扫过去,记录当前最小值,然后更新收益。
这样应该会T(我没试过) 这样丢失了很多信息。
我们考虑一下能不能存起来。 发现解满足区间加法。
即【L,R】中最大的收益要么是【L,K】中的收益,要么是【K,R】中的收益(端点重合不影响),要么是【K,R】中的最大值减去【L,K】中的最小值。
这是针对从左往右走的。
于是乎我们可以用线段树来维护这些信息。
因为我们有可能从左往右走,也可能从右往左走,所以记个f数组,f[0]表示从线段树中下标小的向下标大的走所得的最大收益。
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 200010
#define inf 0x7fffffff using namespace std;
int n,m,num,a[maxn];
struct node
{
int lc,rc;
int l,r;
int smin,smax;
int ansl,ansr;
}t[maxn*]; int init()
{
int x=;char s;bool f=;s=getchar();
while(s<''||s>'')s=getchar();
while(s>=''&&s<='')
{
x=x*+s-'';
s=getchar();
}
return x;
} void update(int k)
{
t[k].smin=min(t[t[k].lc].smin,t[t[k].rc].smin);
t[k].smax=max(t[t[k].lc].smax,t[t[k].rc].smax);
t[k].ansl=max(t[t[k].lc].ansl,t[t[k].rc].ansl);
t[k].ansl=max(t[k].ansl,t[t[k].rc].smax-t[t[k].lc].smin);
t[k].ansr=max(t[t[k].lc].ansr,t[t[k].rc].ansr);
t[k].ansr=max(t[k].ansr,t[t[k].lc].smax-t[t[k].rc].smin);
} void Build(int ll,int rr)
{
int k=++num;
t[k].l=ll;t[k].r=rr;
if(ll==rr)
{
t[k].smin=t[k].smax=a[ll];
return;
}
t[k].lc=num+;
Build(ll,(ll+rr)/);
t[k].rc=num+;
Build((ll+rr)/+,rr);
update(k);
} int findmin(int k,int ll,int rr)
{
if(ll==t[k].l&&rr==t[k].r)return t[k].smin;
int mid=(t[k].l+t[k].r)/;
if(rr<=mid)return findmin(t[k].lc,ll,rr);
else if(ll>mid)return findmin(t[k].rc,ll,rr);
else return min(findmin(t[k].lc,ll,mid),findmin(t[k].rc,mid+,rr));
} int findmax(int k,int ll,int rr)
{
if(ll==t[k].l&&rr==t[k].r)return t[k].smax;
int mid=(t[k].l+t[k].r)/;
if(rr<=mid)return findmax(t[k].lc,ll,rr);
else if(ll>mid)return findmax(t[k].rc,ll,rr);
else return max(findmax(t[k].lc,ll,mid),findmax(t[k].rc,mid+,rr));
} int findl(int k,int ll,int rr)
{
if(ll==t[k].l&&rr==t[k].r)return t[k].ansl;
int mid=(t[k].l+t[k].r)/;
if(rr<=mid)return findl(t[k].lc,ll,rr);
else if(ll>mid)return findl(t[k].rc,ll,rr);
else
{
int maxx=;
maxx=max(findl(t[k].lc,ll,mid),findl(t[k].rc,mid+,rr));
maxx=max(maxx,findmax(t[k].rc,mid+,rr)-findmin(t[k].lc,ll,mid));
return maxx;
}
} int findr(int k,int ll,int rr)
{
if(ll==t[k].l&&rr==t[k].r)return t[k].ansr;
int mid=(t[k].l+t[k].r)/;
if(rr<=mid)return findr(t[k].lc,ll,rr);
else if(ll>mid)return findr(t[k].rc,ll,rr);
else
{
int maxx=;
maxx=max(findr(t[k].lc,ll,mid),findr(t[k].rc,mid+,rr));
maxx=max(maxx,findmax(t[k].lc,ll,mid)-findmin(t[k].rc,mid+,rr));
return maxx;
}
} int main()
{
n=init();
for(int i=;i<=n;i++)
a[i]=init();
for(int i=;i<=maxn*;i++)
t[i].smin=inf;
Build(,n);
int x,y;
m=init();
for(int i=;i<=m;i++)
{
x=init();y=init();
if(x==y)
{
printf("0\n");
continue;
}
if(x<y)printf("%d\n",findl(,x,y));
else printf("%d\n",findr(,y,x));
}
return ;
}

心若向阳,无言悲伤

codevs3304水果姐逛街(线段数)的更多相关文章

  1. codevs3304 水果姐逛水果街

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

  2. codevs3304 水果姐逛水果街Ⅰ

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

  3. codevs3304水果姐逛水果街

    /* 线段树开到*4 *4 *4 *4 ! 维护 4个值 区间最大值 区间最小值 从左往右跑最大收益 从右往左跑最大收益 */ #include<iostream> #include< ...

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

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

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

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

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

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

  7. codevs3305 水果姐逛水果街Ⅱ

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

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

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

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

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

随机推荐

  1. axios在vue项目中的一种封装方法

    记录下之前领导封装的axios请求 npm install axios // 安装 单独写个文件配置axios,此处为request.js import axios from 'axios' //自定 ...

  2. SIMD学习 -- 用SSE2指令作点乘和累加计算

    这几天在做学校的一个学习小项目,需要用到SIMD指令计算提速.也是第一次碰这个,看了一些资料和代码,模仿着写了两个函数. void sse_mul_float(float *A, float *B, ...

  3. PAT 1127 ZigZagging on a Tree

    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...

  4. linux 简单实用小操作

    mysql改密码 通过root以后,(root密码忘记就没法了) alter user username@'%' identified by 'password' 端口被占用 sudo fuser - ...

  5. 【10】AngularJS SQL

    AngularJS SQL 使用 PHP 从 MySQL 中获取数据 <div ng-app="myApp" ng-controller="customersCtr ...

  6. [USACO07OCT]障碍路线Obstacle Course

    题目描述 Consider an N x N (1 <= N <= 100) square field composed of 1 by 1 tiles. Some of these ti ...

  7. 洛谷—— P1122 最大子树和

    https://www.luogu.org/problem/show?pid=1122 题目描述 小明对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题.一天他早晨骑车去上课 ...

  8. Java使用JNI调用DLL库

    JNI是Java自带的方法,不需要引入第三方jar包,优点是因为是java自带的方法,兼容性较好,缺点就是代码书写繁琐 新建Java项目Test --> 新建测试类TestNative,声明本地 ...

  9. Oracle Multitenant Environment (一) About

    About oracle mulittenant environment The multitenant architecture enables an Oracle database to func ...

  10. android推断是否连接wifi和网络状态的推断

    <span style="font-size:18px;">// 是否连接WIFI public static boolean isWifiConnected(Cont ...