Subtask 1: direct updates
For each query, find the path from to by BFS and add along it starting at . This takes time and space, sufficient for .
Subtask 2: the path in label order
When the edges are , a path is an integer interval. If , add at vertex in . Otherwise, add in . Use two range-difference arrays for the coefficient of and the constant term, then take prefix sums. The complexity is time and space.
Full solution: linear functions of depth
Root the tree at vertex . Let be its depth and its parent. The root has depth zero and a virtual parent . For query , let be their LCA. Split the path into the segment from through and the segment after through .
On the first segment, the amount at vertex is
On the second segment, it is
Thus each segment receives a constant coefficient of depth and a constant intercept. Include only in the first segment.
Tree differences for ancestor segments
If is an ancestor of , adding a constant to the inclusive segment from to can be recorded as
After all records, accumulate child values into their parents in bottom-up order. The resulting value at a vertex is the sum of differences in its subtree. The contribution starting at reaches and is canceled above it by the opposite record at .
Maintain arrays for depth coefficients and for intercepts. For one query, set and , then record
The second segment excludes , so it is canceled at , not at . If , its two records cancel immediately, correctly representing an empty segment.
After processing all queries, traverse vertices in reverse of a parent-before-child order and add each vertex's and to its parent. Do not output the virtual vertex or propagate its value back into the tree. The answer at is
Correctness and complexity
The two segments partition the original path, and each linear expression equals the distance from plus one. Tree differences add exactly the specified coefficients on each segment. By linearity, combining all records before accumulation produces the same result as performing every query explicitly.
Binary lifting gives preprocessing, per LCA query, and final accumulation: time and space overall. If , there can be no queries and the answer is zero. Trees of depth are valid, so construct parents and depths and perform accumulation iteratively.
Independent verification solution
A separate HLD implementation splits each path into oriented contiguous index intervals. Append the intervals found on the side in their original order and those on the side in reverse order. Maintain the next value to add. Each interval then receives an index coefficient of or and an intercept, handled by ordinary array range differences. This uses the actual order and lengths of intervals rather than the depth formula above. Its complexity is time and space.
Integer bounds
The final integer value of any vertex is at most . All differences and negative intermediate values also fit signed 64-bit arithmetic comfortably. Do not store differences in 32-bit integers or multiply two 32-bit values before converting the result. Accumulate in 64-bit integers and take the remainder only when printing if desired.
Solution written by GPT6