codevs3305 水果姐逛水果街Ⅱ
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
水果姐第二天心情也很不错,又来逛水果街。
突然,cgh又出现了。cgh施展了魔法,水果街变成了树结构(店与店之间只有一条唯一的路径)。
同样还是n家水果店,编号为1~n,每家店能买水果也能卖水果,并且同一家店卖与买的价格一样。
cgh给出m个问题,每个问题要求水果姐从第x家店出发到第y家店,途中只能选一家店买一个水果,然后选一家店(可以是同一家店,但不能往回走)卖出去。求最多可以赚多少钱。
水果姐向学过oi的你求助。
第一行n,表示有n家店
下来n个正整数,表示每家店一个苹果的价格。
下来n-1行,每行两个整数x,y,表示第x家店和第y家店有一条边。
下来一个整数m,表示下来有m个询问。
下来有m行,每行两个整数x和y,表示从第x家店出发到第y家店。
有m行。
每行对应一个询问,一个整数,表示面对cgh的每次询问,水果姐最多可以赚到多少钱。
10
16 5 1 15 15 1 8 9 9 15
1 2
1 3
2 4
2 5
2 6
6 7
4 8
1 9
1 10
6
9 1
5 1
1 7
3 3
1 1
3 6
样例输出
Sample Output
7
11
7
0
0
15
数据范围及提示
Data Size & Hint
0<=苹果的价格<=10^8
0<n<=200000
0<m<=10000
//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <stack>
using namespace std;
typedef long long LL;
const int MAXN = ;
const int inf = (<<);
int n,m,a[MAXN],deep[MAXN],ecnt,first[MAXN],to[MAXN*],next[MAXN*];
int f[MAXN][],maxl[MAXN][],minl[MAXN][],g[MAXN][],ans,p[MAXN][]; inline int getint(){
int w=,q=; char c=getchar(); while((c<''||c>'') && c!='-') c=getchar();
if(c=='-') q=,c=getchar(); while (c>=''&&c<='') w=w*+c-'',c=getchar(); return q?-w:w;
} inline void dfs(int x,int fa){
for(int i=first[x];i;i=next[i]) {
int v=to[i]; if(v==fa) continue;
f[v][]=x; deep[v]=deep[x]+;
maxl[v][]=max(a[v],a[x]); minl[v][]=min(a[v],a[x]);
g[v][]=max(,a[x]-a[v]);
p[v][]=max(,a[v]-a[x]);
dfs(v,x);
}
} inline void lca(int x,int y){
int t=; int minx=inf,maxy=; ans=;
if(deep[x]<deep[y]) {
while((<<t)<=deep[y]) t++; t--;
for(int i=t;i>=;i--)
if(deep[y]-(<<i)>=deep[x])
ans=max(ans,p[y][i]),ans=max(ans,maxy-minl[y][i]),maxy=max(maxy,maxl[y][i]),y=f[y][i];
}
else{
while((<<t)<=deep[x]) t++; t--;
for(int i=t;i>=;i--)
if(deep[x]-(<<i)>=deep[y])
ans=max(ans,g[x][i]),ans=max(ans,maxl[x][i]-minx),minx=min(minx,minl[x][i]),x=f[x][i];
}
if(x==y) return ;
for(int i=t;i>=;i--) {
if(f[x][i]!=f[y][i]) {
ans=max(g[x][i],ans);
ans=max(p[y][i],ans);
ans=max(ans,maxl[x][i]-minx);
ans=max(ans,maxy-minl[y][i]);
maxy=max(maxy,maxl[y][i]);
minx=min(minx,minl[x][i]);
x=f[x][i]; y=f[y][i];
}
}
ans=max(g[x][],ans);
ans=max(p[y][],ans);
maxy=max(maxy,maxl[y][]);
minx=min(minx,minl[x][]);
ans=max(ans,maxy-minx);
} inline void work(){
n=getint(); for(int i=;i<=n;i++) a[i]=getint(); int x,y;
for(int i=;i<n;i++) {
x=getint(); y=getint();
next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y;
next[++ecnt]=first[y]; first[y]=ecnt; to[ecnt]=x;
}
deep[]=;
dfs(,);
for(int j=;j<=;j++) {
for(int i=;i<=n;i++) {
f[i][j]=f[f[i][j-]][j-];
if(f[i][j]==) continue;
maxl[i][j]=max(maxl[i][j-],maxl[f[i][j-]][j-]);
minl[i][j]=min(minl[i][j-],minl[f[i][j-]][j-]);
g[i][j]=max(g[i][j-],g[f[i][j-]][j-]);
p[i][j]=max(p[i][j-],p[f[i][j-]][j-]);
g[i][j]=max(g[i][j],maxl[f[i][j-]][j-]-minl[i][j-]);
p[i][j]=max(p[i][j],maxl[i][j-]-minl[f[i][j-]][j-]);
}
}
m=getint();
while(m--) {
x=getint(); y=getint();
lca(x,y);
printf("%d\n",ans);
}
} int main()
{
work();
return ;
}
codevs3305 水果姐逛水果街Ⅱ的更多相关文章
- 水果姐逛水果街Ⅱ codevs 3305
3305 水果姐逛水果街Ⅱ 时间限制: 2 s 空间限制: 256000 KB 题目描述 Description 水果姐第二天心情也很不错,又来逛水果街. 突然,cgh又出现了.cgh施展了魔 ...
- code vs 3305 水果姐逛水果街Ⅱ
3305 水果姐逛水果街Ⅱ 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 水果姐第二天心情也很不错, ...
- Codevs 3305 水果姐逛水果街Ⅱ 倍增LCA
题目:http://codevs.cn/problem/3305/ 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Des ...
- codevs 3305 水果姐逛水果街Ⅱ&&codevs3006
题目描述 Description 水果姐第二天心情也很不错,又来逛水果街. 突然,cgh又出现了.cgh施展了魔法,水果街变成了树结构(店与店之间只有一条唯一的路径). 同样还是n家水果店,编号为1~ ...
- CodeVs——T 3305 水果姐逛水果街Ⅱ
http://codevs.cn/problem/3305/ 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 De ...
- codevs3304 水果姐逛水果街
题目描述 Description 水果姐今天心情不错,来到了水果街. 水果街有n家水果店,呈直线结构,编号为1~n,每家店能买水果也能卖水果,并且同一家店卖与买的价格一样. 学过oi的水果姐迅速发现了 ...
- 水果姐逛水果街Ⅰ(codevs 3304)
题目描述 Description 水果姐今天心情不错,来到了水果街. 水果街有n家水果店,呈直线结构,编号为1~n,每家店能买水果也能卖水果,并且同一家店卖与买的价格一样. 学过oi的水果姐迅速发现了 ...
- Codevs 3304 水果姐逛水果街Ⅰ 线段树
题目: http://codevs.cn/problem/3304/ 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 D ...
- codevs3304 水果姐逛水果街Ⅰ
题目描述 Description 水果姐今天心情不错,来到了水果街. 水果街有n家水果店,呈直线结构,编号为1~n,每家店能买水果也能卖水果,并且同一家店卖与买的价格一样. 学过oi的水果姐迅速发现了 ...
随机推荐
- Linux 网络编程详解三(p2p点对点聊天)
//p2p点对点聊天多进程版--服务器(信号的使用) #include <stdio.h> #include <stdlib.h> #include <string.h& ...
- Map集合 总结
(本人第一次写博客,部分内容有参照李刚老师的疯狂java系列图书,如有遗漏错误,请多指教,谢谢.) Java的集合类可分为Set.List.Map.Queue,其中Set.List.Queue都有共同 ...
- 牛X的CSS3
See the Pen Dot Wave by Rich Howell (@roborich) on CodePen
- 前端见微知著JavaScript基础篇:this or that ?
上节,我们提到了this关键字的问题,并且追加了一句很有意义的话:谁调用我,我指向谁.的确,在javascript中,在默认情况下,this会指向一个已经初始化的window对象.所以你不论有多少全局 ...
- PAT 1067. Sort with Swap(0,*)
1067. Sort with Swap(0,*) (25) Given any permutation of the numbers {0, 1, 2,..., N-1}, it is easy ...
- PRML读书会第十一章 Sampling Methods(MCMC, Markov Chain Monte Carlo,细致平稳条件,Metropolis-Hastings,Gibbs Sampling,Slice Sampling,Hamiltonian MCMC)
主讲人 网络上的尼采 (新浪微博: @Nietzsche_复杂网络机器学习) 网络上的尼采(813394698) 9:05:00 今天的主要内容:Markov Chain Monte Carlo,M ...
- 建立时间和保持时间(setup time 和 hold time)
建立时间和保持时间贯穿了整个时序分析过程.只要涉及到同步时序电路,那么必然有上升沿.下降沿采样,那么无法避免setup-time 和 hold-time这两个概念.本文内容相对独立于该系列其他文章,是 ...
- WordPress使用固定链接
WordPress安装后我们会发现,文章默认的url是很丑的,http://example.com/?p=N,其中N是文章ID,一串数字.默认链接在所有的环境下都运转良好,但和其他的类型比起来却没那么 ...
- Graphql介绍(Introduction to GraphQL)
Introduction to GraphQL GraphQL介绍 Learn about GraphQL, how it works, and how to use it in this seri ...
- android file path
问题 出现的异常为:java.lang.IllegalArgumentException: File /mnt/sdcard/crazyit.bin contains a pathseparator. ...