The merchant
Time Limit: 3000MS   Memory Limit: 65536K
     

Description

There are N cities in a country, and there is one and only one simple path between each pair of cities. A merchant has chosen some paths and wants to earn as much money as possible in each path. When he move along a path, he can choose one city to buy some goods and sell them in a city after it. The goods in all cities are the same but the prices are different. Now your task is to calculate the maximum possible profit on each path.

Input

The first line contains N, the number of cities.
Each of the next N lines contains wi the goods' price in each city.
Each of the next N-1 lines contains labels of two cities, describing a road between the two cities.
The next line contains Q, the number of paths.
Each of the next Q lines contains labels of two cities, describing a path. The cities are numbered from 1 to N.

1 ≤ NwiQ ≤ 50000

Output

The output contains Q lines, each contains the maximum profit of the corresponding path. If no positive profit can be earned, output 0 instead.

Sample Input

4
1
5
3
2
1 3
3 2
3 4
9
1 2
1 3
1 4
2 3
2 1
2 4
3 1
3 2
3 4

Sample Output

4
2
2
0
0
0
0
2
0
分析:考虑只有三种情况,一种是买卖都在起点至LCA,第二种是LCA至终点,第三种是买在起点至LCA,卖在LCA至终点;
   LCA维护区间买卖最大值最小值即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=5e4+;
const int N=1e3+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,fa[][maxn],up[][maxn],go[][maxn],ma[][maxn],mi[][maxn],a[maxn],dep[maxn];
vi e[maxn];
void dfs(int x,int y)
{
dep[x]=dep[y]+;
fa[][x]=y;
ma[][x]=mi[][x]=a[y];
for(int i=;fa[i-][fa[i-][x]];i++)
{
fa[i][x]=fa[i-][fa[i-][x]];
ma[i][x]=max(ma[i-][x],ma[i-][fa[i-][x]]);
mi[i][x]=min(mi[i-][x],mi[i-][fa[i-][x]]);
up[i][x]=max(max(up[i-][x],up[i-][fa[i-][x]]),ma[i-][fa[i-][x]]-mi[i-][x]);
go[i][x]=max(max(go[i-][x],go[i-][fa[i-][x]]),ma[i-][x]-mi[i-][fa[i-][x]]);
}
for(int i=;i<e[x].size();i++)
{
int z=e[x][i];
if(z==y)continue;
dfs(z,x);
}
}
int lca(int x,int y)
{
if(dep[x]<dep[y])swap(x,y);
for(int i=;i>=;i--)if(dep[fa[i][x]]>=dep[y])x=fa[i][x];
if(x==y)return x;
for(int i=;i>=;i--)if(fa[i][x]!=fa[i][y])x=fa[i][x],y=fa[i][y];
return fa[][x];
}
int main()
{
int i,j;
scanf("%d",&n);
rep(i,,n)scanf("%d",&a[i]);
rep(i,,n-)scanf("%d%d",&j,&k),e[j].pb(k),e[k].pb(j);
dfs(,);
scanf("%d",&m);
while(m--)
{
int b,c,d;
scanf("%d%d",&b,&c);
d=lca(b,c);
int ret=,mii=a[b],maa=a[c];
for(i=;i>=;i--)
{
if(dep[fa[i][b]]>=dep[d])
{
ret=max(ret,up[i][b]);
ret=max(ret,ma[i][b]-mii);
mii=min(mii,mi[i][b]);
b=fa[i][b];
}
}
for(i=;i>=;i--)
{
if(dep[fa[i][c]]>=dep[d])
{
ret=max(ret,go[i][c]);
ret=max(ret,maa-mi[i][c]);
maa=max(maa,ma[i][c]);
c=fa[i][c];
}
}
ret=max(ret,maa-mii);
printf("%d\n",ret);
}
return ;
}

The merchant的更多相关文章

  1. [最近公共祖先] POJ 3728 The merchant

    The merchant Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 4556   Accepted: 1576 Desc ...

  2. POJ 3278 The merchant

    传送门 Time Limit: 3000MS Memory Limit: 65536K Description There are N cities in a country, and there i ...

  3. poj 3728 The merchant(LCA)

    Description There are N cities in a country, and there is one and only one simple path between each ...

  4. ThoughtWorks Merchant's Guide To The Galaxy

    ThoughtWorks笔试题之Merchant's Guide To The Galaxy解析 一.背景 在某网站上看到ThoughtWorks在武汉招人,待遇在本地还算不错,就投递了简历.第二天H ...

  5. [POJ 3728]The merchant

    Description There are N cities in a country, and there is one and only one simple path between each ...

  6. opencart3图片Google Merchant Center验证通过不了的解决方法

    最近在做一个opencart项目,有对接Google Merchant Center,但是一直提示产品图片验证无法通过,ytkah看了一下图片路径,/image/cache/catalog/demo/ ...

  7. 如何查看卖家ID (Merchant ID) 亚马逊哪里找?

    如何查看卖家ID (Merchant ID) 亚马逊哪里找? 如何查看卖家ID (Merchant ID) 亚马逊哪里找? 1. 找到想要获取ID的卖家,点击店铺名(跟卖的卖家会收在”Other Se ...

  8. poj3728The merchant 【倍增】【LCA】

    There are N cities in a country, and there is one and only one simple path between each pair of citi ...

  9. POJ 3728 The merchant(LCA+DP)

    The merchant Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total ...

  10. 雅礼集训 Day6 T1 Merchant 解题报告

    Merchant 题目描述 有\(n\)个物品,第\(i\)个物品有两个属性\(k_i,b_i\),表示它在时刻\(x\)的价值为\(k_i\times x+b_i\). 当前处于时刻\(0\),你可 ...

随机推荐

  1. 【SCOI 2011】 糖果

    [题目链接] 点击打开链接 [算法] 当x = 1时,连边(a,b,0)和(b,a,0) 当x = 2时,连边(a,b,1) 当x = 3时,连边(b,a,0) 当x = 4时,连边(b,a,1) 当 ...

  2. bzoj1227

    离散化+树状数组+排列组合 很久以前就看到过这道题,现在依然不会做...看完题解发现思路很简单,就是有点难写 我们先将坐标离散化,x和y最大是w,然后我们就有了一个暴力做法, 枚举每块墓地,统计,因为 ...

  3. bzoj4082

    贪心+倍增 首先如果这个问题在序列上,好像可以按右端点排序,然后从起点开始向能到的最远的地方走. 但是环上不可以,因为随即一个起点可能不是最小的. 然后神思路来了:我们先将环展开倍增,再将区间按右端点 ...

  4. celery定时执行ansible api返回为空的问题

    有两种方法解决这个问题,就是关闭assert:1.在celery 的worker启动窗口设置export PYTHONOPTIMIZE=1或打开celery这个参数-O OPTIMIZATION2.注 ...

  5. 【转】Linux之printf命令

    转自:http://blog.chinaunix.net/uid-9525959-id-2001528.html printf FORMAT [ARGUMENT]... printf OPTION [ ...

  6. Winform中Treeview控件失去焦点,将选择的节点设置为高亮显示 (2012-07-16 13:47:07)转载▼

    Winform中Treeview控件失去焦点,将选择的节点设置为高亮显示 (2012-07-16 13:47:07)转载▼标签: winform treeview drawnode Treeview控 ...

  7. TensorFlow-Gpu环境搭建——Win10+ Python+Anaconda+cuda

    参考:http://blog.csdn.net/sb19931201/article/details/53648615 https://segmentfault.com/a/1190000009803 ...

  8. MySQL 执行计划中Extra的浅薄理解

    1.using where: Extra中出现“Using where”,通常来说,意味着全表扫描或者在查找使用索引的情况下,但是还有查询条件不在索引字段当中. 如果需要回表也是用这个. 2.usin ...

  9. JS高级——Function原型链

    基本概念 1.函数可以通过Function new出来,那么Function可以被称作构造函数,被new出来的函数可以被称为一个对象 2.Function既然是构造函数,那么肯定也有原型,它的原型是一 ...

  10. JS——对象创建

    1.原始创建 <script> person = new Object();//不要var person.firstname = "Bill"; person.last ...