You are given an undirected tree on n vertices (1..n) described by an (n-1)-by-2 edge matrix edges, an initial value array init_vals (length n, non-negative integers), and a q-by-5 matrix of queries:
- Type 1: [1, u, v, a, b] — For every vertex w on the unique simple path between u and v (inclusive), update:
val(w) = (a * val(w) + b) mod M
- Type 2: [2, u, v, 0, 0] — Compute the sum of val(w) over all vertices w on the unique simple path between u and v (inclusive), modulo M.
Here M = 1000000007 (10^9 + 7).
Return a column vector containing the exact result (as double integers in [0, M-1]) of every Type 2 query in order.
Constraints:
- n <= 20,000, q <= 15,000.
Solution Stats
Solution Comments
Show comments
Loading...
Problem Recent Solvers1
Suggested Problems
More from this Author31
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!