/*我尼玛 又一个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. 商务通代码API

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="content-type" content ...

  2. Webservices-2.C#创建web服务,及引用访问、代码访问

    注:web服务简介Webservices-1.web服务定义简介 以下均以C#语言为例 一.创建web服务(简单介绍,主要讨论客户端引用) 打开VS创建网站项目,在网站项目中添加“WEB服务(ASMX ...

  3. BZOJ 1018 堵塞的交通

    Description 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常奇特,整个国家的交通系统可以被看成是一个2行C列的矩形网格,网格上的每个点代表一个城市,相邻的城市之间有一 ...

  4. Parencodings

    Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two diff ...

  5. 大众点评试水O2O新模式:实体店试穿,扫描二维码付款 现场取货

    在餐饮美食行业取得不错的成绩之后,大众点评将触角延伸到了线下的传统商铺,开始涉足线下商品的 O2O 团购.和传统的线上下单,线下消费的 O2O 模式不同.大众点评的 O2O 团购用户,可在店内试穿后通 ...

  6. 【HDOJ】1401 Solitaire

    双向BFS+状态压缩. /* 1401 */ #include <iostream> #include <queue> #include <map> #includ ...

  7. ZOJ-2112-Dynamic Rankings(线段树套splay树)

    题意: 完成两个操作: 1.询问一个区间里第k小的数: 2.修改数列中一个数的值. 分析: 线段树套平衡树,线段树中的每个节点都有一棵平衡树,维护线段树所记录的这个区间的元素.这样处理空间上是O(nl ...

  8. 【转】ConcurrentModificationException异常解决办法 --不错

    原文网址:http://blog.sina.com.cn/s/blog_465bcfba01000ds7.html 1月30日java.util.ConcurrentModificationExcep ...

  9. (转载)PHP怎么获取MySQL执行sql语句的查询时间

    (转载自CSDN) 方法一: //计时开始 runtime(); //执行查询 mysql_query($sql); //计时结束. echo runtime(1); //计时函数 function ...

  10. 贪心:SPOJ Backup Files

    BACKUP - Backup Files no tags  You run an IT company that backs up computer data for large offices. ...