#P9704. 「TFOI R1」Tree Home
「TFOI R1」Tree Home
Background
The sunny and cheerful genius boy Z is going to confess his love to Jiao Tailang today!
As everyone knows, Jiao Tailang is a very cute girl. In the past, genius Z always worried that he would be laughed at if his confession failed. But today, genius Z is going to do the most important thing in his life: confess sincerely, no matter what the result is.
Unexpectedly, Jiao Tailang also likes genius Z!
Genius Z is as happy as a 0#.
But not long after that, genius Z got dumped. The reason is that Jiao Tailang found out that genius Z had improper thoughts about her best friend.
Genius Z pulled out his tree-shaped family tree and started greeting his ancestors.
Problem Description
There is a tree with nodes connected by weighted undirected edges. Each node has its corresponding index and weight . The root of the whole tree is the node with index .
Let $f(a, b, c) = (a - b) \times \left[a^2 + b^2 + a \times b + 3 \times c \times (a + b + c)\right]$, where can be any integers. Also, let denote the sum of the edge weights on the path from node to the root.
Now genius Z will make queries. In each query, four positive integers are given. You need to choose one node from the nodes whose indices are in the interval , and choose one node from the nodes whose indices are in the interval . Of course, the two chosen nodes must satisfy . Let be the lowest common ancestor of and . You need to maximize the value of $|f(d_{p} - d_{r}, d_{q} - d_{r}, d_{r})| + |v_{p} - v_{q}|$, and output this maximum value for each query.
Input Format
The first line contains two positive integers and , representing the number of nodes in the tree and the number of queries.
The second line contains integers. The -th number is the weight of the node with index .
The next lines each contain three integers , indicating that there is an undirected edge with weight between nodes and .
The next lines each contain four integers , indicating one query (with the meaning described above).
Output Format
Output lines. Each line contains one integer, representing the answer to each query.
7 2
5 1 7 12 5 9 6
1 2 5
3 1 1
6 2 9
4 6 14
7 6 4
5 2 10
2 4 5 7
1 1 3 3
19211
3
Hint
Sample Explanation
For the first query, we can take node from the first interval and node from the second interval, obtaining the answer .
For the second query, each interval can only choose one node, so the answer is .
Constraints
This problem uses bundled testdata.
- Subtask 1 (5 points): .
- Subtask 2 (10 points): .
- Subtask 3 (30 points): .
- Subtask 4 (55 points): no special restrictions.
For all data, , , , , . It is guaranteed that the maximum depth of the tree does not exceed .
Note: the two intervals and may overlap.
Translated by ChatGPT 5