Yaoge’s maximum profit
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 315 Accepted Submission(s): 89
Problem Description
Yaoge likes to eat chicken chops late at night. Yaoge has eaten too many chicken chops, so that Yaoge knows the pattern in the world of chicken chops. There are N cities in the world numbered from 1 to N . There are some roads between some cities, and there is one and only one simple path between each pair of cities, i.e. the cities are connected like a tree. When Yaoge moves along a path, Yaoge can choose one city to buy ONE chicken chop and sell it in a city after the city Yaoge buy it. So Yaoge can get profit if Yaoge sell the chicken chop with higher price. Yaoge is famous in the world. AFTER Yaoge has completed one travel, the price of the chicken chop in each city on that travel path will be increased by V .
Input
The first line contains an integer T (0 < T ≤ 10), the number of test cases you need to solve. For each test case, the first line contains an integer N (0 < N ≤ 50000), the number of cities. For each of the next N lines, the i-th line contains an integer W
i(0 < W
i ≤ 10000), the price of the chicken chop in city i. Each of the next N - 1 lines contains two integers X Y (1 ≤ X, Y ≤ N ), describing a road between city X and city Y . The next line contains an integer Q(0 ≤ Q ≤ 50000), the number of queries. Each of the next Q lines contains three integer X Y V(1 ≤ X, Y ≤ N ; 0 < V ≤ 10000), meaning that Yaoge moves along the path from city X to city Y , and the price of the chicken chop in each city on the path will be increased by V AFTER Yaoge has completed this travel.
Output
For each query, output the maximum profit Yaoge can get. If no positive profit can be earned, output 0 instead.
Sample Input
1 5 1 2 3 4 5 1 2 2 3 3 4 4 5 5 1 5 1 5 1 1 1 1 2 5 1 1 1 2 1
Sample Output
4 0 0 1 0
题意:找出从u到v路径上的最大的一个值,这个值由路径后面的数减前面的数
思路:LCT
要维护四个值
路径最大值mx
路径最小值mn
路径从左到右的最小值L (将问题转化为了求最小值,路径前面的数减后面的)
路径从右到左的最小值R
重点是L和R怎么维护呢
分析L,可以由以下几种部分组成
1.左边链的L
2.右边链的L
3.左边链的最小值 - val
4.val - 右边链的最大值
5.左边链的最小值 - 右边链的最大值
在这几部分中取最小的就好了,R类似
然后还有一点要注意,交换左右孩子的时候要交换L和R
#include
#include
#include
#include
#include
#include
#include