The merchant
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 4800 | Accepted: 1666 |
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 ≤ N, wi, Q ≤ 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
Source
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; int n,a[50010],head[50010],to[100020],nextt[100020],tot,q,d[50010]; int fa[50010][20], minn[50010][20], maxx[50010][20], lmax[50010][20], rmin[50010][20]; void add(int x, int y) { to[tot] = y; nextt[tot] = head[x]; head[x] = tot++; } void dfs(int u, int from,int dist) { d[u] = dist; fa[u][0] = from; for (int i = head[u]; i != -1; i = nextt[i]) { int v = to[i]; if (from != v) dfs(v, u,dist + 1); } } void init() { dfs(1, 0, 0); minn[1][0] = maxx[1][0] = a[1]; lmax[1][0] = -1 << 30; rmin[1][0] = 1 << 30; for (int i = 2; i <= n; i++) { minn[i][0] = min(a[i], a[fa[i][0]]); maxx[i][0] = max(a[i], a[fa[i][0]]); lmax[i][0] = max(a[fa[i][0]] - a[i], 0); rmin[i][0] = min(a[fa[i][0]] - a[i], 0); } for (int j = 1; (1 << j) < n; j++) for (int i = 1; i <= n; i++) { fa[i][j] = fa[fa[i][j - 1]][j - 1]; maxx[i][j] = max(maxx[i][j - 1], maxx[fa[i][j - 1]][j - 1]); minn[i][j] = min(minn[i][j - 1], minn[fa[i][j - 1]][j - 1]); lmax[i][j] = max(max(lmax[i][j - 1], lmax[fa[i][j - 1]][j - 1]), maxx[fa[i][j-1]][j - 1] - minn[i][j - 1]); rmin[i][j] = min(min(rmin[i][j - 1], rmin[fa[i][j - 1]][j - 1]), minn[fa[i][j - 1]][j - 1] - maxx[i][j - 1]); } } int LCA(int x, int y) { int maxxx = 0, miny = 0, minx = a[x], maxy = a[y]; for (int i = 16; i >= 0 && d[x] != d[y]; i--) { if (abs(d[x] - d[y]) >= 1 << i) { if (d[y] < d[x]) { maxxx = max(max(maxxx, lmax[x][i]), maxx[x][i] - minx); minx = min(minx, minn[x][i]); x = fa[x][i]; } else { miny = min(min(miny, rmin[y][i]), minn[y][i] - maxy); maxy = max(maxy, maxx[y][i]); y = fa[y][i]; } } } if (x == y) return max(max(maxxx, -miny), maxy - minx); for (int i = 16; i >= 0;i--) if (fa[x][i] != fa[y][i] && fa[x][i] && fa[y][i]) { maxxx = max(max(maxxx, lmax[x][i]), maxx[x][i] - minx); minx = min(minx, minn[x][i]); x = fa[x][i]; miny = min(min(miny, rmin[y][i]), minn[y][i] - maxy); maxy = max(maxy, maxx[y][i]); y = fa[y][i]; } maxxx = max(max(maxxx, lmax[x][0]), maxx[x][0] - minx); minx = min(minx, minn[x][0]); miny = min(min(miny, rmin[y][0]), minn[y][0] - maxy); maxy = max(maxy, maxx[y][0]); return max(max(maxxx, -miny), maxy - minx); } int main() { memset(head, -1, sizeof(head)); scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n - 1; i++) { int u, v; scanf("%d%d", &u, &v); add(u, v); add(v, u); } init(); scanf("%d", &q); while (q--) { int x, y; scanf("%d%d", &x, &y); printf("%d ", LCA(x, y)); } return 0; }