/*我尼玛 又一个min打成max 看了半天....*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 200010
#define inf 0x7fffffff
using namespace std;
int n,m,head[maxn],num,v[maxn],fa[maxn][],c[maxn];
int mii[maxn][],mxx[maxn][],up[maxn][],down[maxn][];
struct node{
int v,pre;
}e[maxn*];
int init(){
int x=;char s=getchar();
while(s<''||s>'')s=getchar();
while(s>=''&&s<=''){x=x*+s-'';s=getchar();}
return x;
}
void Add(int from,int to){
num++;e[num].v=to;
e[num].pre=head[from];
head[from]=num;
}
void Dfs(int now,int from,int dep,int val){
fa[now][]=from;c[now]=dep;
mii[now][]=min(val,v[now]);
mxx[now][]=max(val,v[now]);
up[now][]=max(,v[from]-v[now]);
down[now][]=max(,v[now]-v[from]);
for(int i=head[now];i;i=e[i].pre){
if(e[i].v==from)continue;
Dfs(e[i].v,now,dep+,v[now]);
}
}
void Get_fa(){
for(int j=;j<=;j++)
for(int i=;i<=n;i++){
fa[i][j]=fa[fa[i][j-]][j-];
mii[i][j]=min(mii[i][j-],mii[fa[i][j-]][j-]);
mxx[i][j]=max(mxx[i][j-],mxx[fa[i][j-]][j-]);
up[i][j]=max(up[i][j-],up[fa[i][j-]][j-]);
up[i][j]=max(up[i][j],mxx[fa[i][j-]][j-]-mii[i][j-]);
down[i][j]=max(down[i][j-],down[fa[i][j-]][j-]);
down[i][j]=max(down[i][j],mxx[i][j-]-mii[fa[i][j-]][j-]);
}
}
int LCA(int a,int b){
if(c[a]<c[b])swap(a,b);
int t=c[a]-c[b];
for(int i=;i<=;i++)
if((<<i)&t)a=fa[a][i];
if(a==b)return a;
for(int i=;i>=;i--)
if(fa[a][i]!=fa[b][i]){
a=fa[a][i];
b=fa[b][i];
}
return fa[a][];
}
int Up_lca(int a,int b){
int t=c[a]-c[b],ret=,minner=inf;
for(int i=;i<=;i++)
if((<<i)&t){
ret=max(ret,up[a][i]);
ret=max(ret,mxx[a][i]-minner);
minner=min(minner,mii[a][i]);
a=fa[a][i];
}
return ret;
}
int Down_lca(int a,int b){
int t=c[a]-c[b],ret=,maxxer=;
for(int i=;i<=;i++)
if((<<i)&t){
ret=max(ret,down[a][i]);
ret=max(ret,maxxer-mii[a][i]);
maxxer=max(maxxer,mxx[a][i]);
a=fa[a][i];
}
return ret;
}
int Query_max(int a,int b){
int t=c[a]-c[b],maxxer=v[a];
for(int i=;i<=;i++)
if((<<i)&t){
maxxer=max(maxxer,mxx[a][i]);
a=fa[a][i];
}
return maxxer;
}
int Query_min(int a,int b){
int t=c[a]-c[b],minner=v[a];
for(int i=;i<=;i++)
if((<<i)&t){
minner=min(minner,mii[a][i]);
a=fa[a][i];
}
return minner;
}
int main()
{
n=init();
for(int i=;i<=n;i++)
v[i]=init();
int u,v;
for(int i=;i<n;i++){
u=init();v=init();
Add(u,v);Add(v,u);
}
memset(mii,,sizeof(mii));
Dfs(,,,);Get_fa();
m=init();
while(m--){
u=init();v=init();
int anc=LCA(u,v);
//printf("%d\n",anc);
int ans=;
ans=max(Up_lca(u,anc),Down_lca(v,anc));
ans=max(ans,Query_max(v,anc)-Query_min(u,anc));
printf("%d\n",ans);
}
return ;
}

codevs 3305 水果姐逛水果街Ⅱ的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. 水果姐逛水果街Ⅰ(codevs 3304)

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

  9. codevs3305 水果姐逛水果街Ⅱ

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

  10. codevs3304 水果姐逛水果街

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

随机推荐

  1. 常用jQuery选择器总结【转】

    在Dom 编程中我们只能使用有限的函数根据id 或者TagName 获取Dom 对象. 然而在jQuery 中则完全不同,jQuery 提供了异常强大的选择器用来帮助我们获取页面上的对象, 并且将对象 ...

  2. A Good Beginning Helps To Make A Good End

    Hello Guys, It seems nothing would be not good after posting this blog link in my MS application. Se ...

  3. 请教 WINDOWSPHONE 有个人录了个传感器等硬件的视频,并且项目是完全开源的,大家有知道地址的吗?或者叫什么。

    请教 WINDOWSPHONE 有个人录了个传感器等硬件的视频,并且项目是完全开源的,大家有知道地址的吗?或者叫什么.

  4. C#执行oracle返回游标类型的存储过程

    存储过程代码为: create or replace procedure proc_test(pCursor OUT pak_pub.ut_cursor) AS begin -- 使用游标 open ...

  5. iscc2016-basic-心灵鸡汤

    用winhex打开发现 ISCCCongratulations! You need remember: DEath IS JUST A PaRT oF lIFE,sOMeTHInG wE'RE aLL ...

  6. PHPStorm自动提示方法

    第一种: /** * 一定要写@return static * @return static */ public static function getInstance() { $className ...

  7. NSNumber

    integerfloatc 在Objective-c中有int的数据类型,那为什么还要使用数字对象NSNumber?这是因为很多类(如NSArray)都要求使用对象,而int不是对象.NSNumber ...

  8. AES加密跨平台出现的问题

    javax.crypto.BadPaddingException: Given final block not properly padded at com.sun.crypto.provider.S ...

  9. dns智能解析对网站排名的影响

    网站排名是所有建站者都关系的问题,如何提升网站排名有很多因素,网站是否健康也与网站排名有关,下面智儒科技网站建设为你研究下如何判断自己的网站是否健康. 一般情况下,网站的排名在优化的基础上,怎么也上不 ...

  10. .net 开发框架

    .NET开发人员值得关注的七个开源项目 2010年07月02日09:33 it168网站原创 作者:黄永兵 编译 编辑:胡铭娅 我要评论(0) [IT168技术分析]微软近几年在.NET社区开源项目方 ...