Tree Path Queries
You are given a tree with n nodes (numbered 1 to n). Each node i has an integer value value[i]. The tree is rooted at node 1.
You must handle q queries of two types:
- LCA query: Given two nodes
uandv, output their lowest common ancestor. - Path sum query: Given two nodes
uandv, output the sum of values of all nodes on the unique path fromutov(inclusive).
Input
The first line contains n and q (1 ≤ n ≤ 10^5, 1 ≤ q ≤ 10^5).
The second line contains n space-separated integers: value[1], value[2], ..., value[n] where -10^9 ≤ value[i] ≤ 10^9.
The next n-1 lines describe the tree edges. Line i+2 contains two integers u and v (1 ≤ u, v ≤ n), indicating an undirected edge between nodes u and v. The tree is guaranteed to be connected.
Each of the next q lines contains a query:
1 u v: LCA query, output the lowest common ancestor of nodesuandv.2 u v: Path sum query, output the sum of values on the path fromutov.
Output
For each LCA query, print the node number of the lowest common ancestor.
For each path sum query, print the sum as a 64-bit signed integer.
Notes
The path from u to v includes both endpoints and all intermediate nodes on the unique path in the tree.
Constraints
- 1 ≤ n ≤ 10^5
- 1 ≤ q ≤ 10^5
- -10^9 ≤ value[i] ≤ 10^9
- Path sums fit in a 64-bit signed integer