#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 nn nodes connected by n1n - 1 weighted undirected edges. Each node has its corresponding index and weight viv_{i}. The root of the whole tree is the node with index 11.

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 a,b,ca, b, c can be any integers. Also, let did_i denote the sum of the edge weights on the path from node ii to the root.

Now genius Z will make TT queries. In each query, four positive integers l1,r1,l2,r2l_{1}, r_{1}, l_{2}, r_{2} are given. You need to choose one node pp from the nodes whose indices are in the interval [l1,r1][l_{1}, r_{1}], and choose one node qq from the nodes whose indices are in the interval [l2,r2][l_{2}, r_{2}]. Of course, the two chosen nodes must satisfy pqp \neq q. Let rr be the lowest common ancestor of pp and qq. 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 nn and TT, representing the number of nodes in the tree and the number of queries.

The second line contains nn integers. The ii-th number is the weight of the node with index ii.

The next n1n - 1 lines each contain three integers u,v,wu, v, w, indicating that there is an undirected edge with weight ww between nodes uu and vv.

The next TT lines each contain four integers l1,r1,l2,r2l_{1}, r_{1}, l_{2}, r_{2}, indicating one query (with the meaning described above).

Output Format

Output TT 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 44 from the first interval and node 66 from the second interval, obtaining the answer 1921119211.

For the second query, each interval can only choose one node, so the answer is 33.

Constraints

This problem uses bundled testdata.

  • Subtask 1 (5 points): 1n,T101 \leqslant n, T \leqslant 10.
  • Subtask 2 (10 points): 1n,T1001 \leqslant n, T \leqslant 100.
  • Subtask 3 (30 points): 1n,T30001 \leqslant n, T \leqslant 3000.
  • Subtask 4 (55 points): no special restrictions.

For all data, 1n,T2×1051 \leqslant n, T \leqslant 2 \times 10^5, 0w250 \leqslant |w| \leqslant 25, 1vx1091 \leqslant v_{x} \leqslant 10^9, 1l1r1n1 \leqslant l_{1} \leqslant r_{1} \leqslant n, 1l2r2n1 \leqslant l_{2} \leqslant r_{2} \leqslant n. It is guaranteed that the maximum depth of the tree does not exceed 100100.

Note: the two intervals [l1,r1][l_{1}, r_{1}] and [l2,r2][l_{2}, r_{2}] may overlap.

Translated by ChatGPT 5