3335. Ch’s gift (Hard)

单点时限: 3.0 sec

内存限制: 256 MB

Mr. Cui is working off-campus and he misses his girlfriend very much. After a whole night tossing and turning, he decides to get to his girlfriend’s city and of course, with well-chosen gifts. He knows neither too low the price could a gift be since his girlfriend won’t like it, nor too high of it since he might consider not worth to do. So he will only buy gifts with price in the range $[a,b]$.

There are $n$ cities in the country and $n-1$ bi-directional roads. Each city can be reached from any other city. In the $i$-th city, there is a specialty of price $c_i$ Cui could buy as a gift. Cui buys at most $1$ gift in a city. Cui starts his trip from city $s$ and his girl friend is in city $t$. As mentioned above, Cui is so hurry that he will choose the quickest way to his girl friend (in other words, he won’t pass a city twice) and of course, buys as many as gifts as possible. Now he wants to know, how much money does he need to afford all the gifts?

输入格式

The first line contains two integers $n, q$ $(2 \leq n \leq 10^5, 1 \leq q \leq 10^5)$, representing the number of cities and the number of queries.

The second line contains $n$ integers $c_1,c_2,\ldots,c_n$ $(1 \leq c_i \leq 10^9)$, indicating the price of city $i$’s specialty.

Then $n-1$ lines follows. Each line has two integers $x,y$ $(1 \leq x, y \leq n, x \neq y)$, meaning there is road between city $x$ and city $y$.

$q$ line follows. In each line, there are four integers $s,t,a,b$ $(1 \leq s,t \leq n, 1 \leq a \leq b \leq 10^9)$, which indicates start city, end city, lower bound of the price, upper bound of the price, respectively. There is no guarantee for the relationship between $s$ and $t$.

输出格式

Output $q$ integers denoting the answers, respectively for $q$ queries. Use any format as you like.

样例

Input
5 3
1 2 1 3 2
1 2
2 4
3 1
2 5
4 5 1 3
1 1 1 1
3 5 2 3
Output
7 1 4

提示

This is originally a problem on HDU, but it is well-known that datasets on HDU is very weak, so that a lot of brute-force solutions got accepted. Mr. Cui is angry, as these solutions are not helpful when he is caught up with some tricky situations.

38 人解决,71 人已尝试。

86 份提交通过,共有 328 份提交。

5.3 EMB 奖励。

创建: 6 年,7 月前.

修改: 6 年,1 月前.

最后提交: 9 月,1 周前.

来源: 2017 Multi-University Training 9

题目标签