For a path between u and v, increment a counter at u and v, and decrement twice at lca(u, v). Also decrement once at the parent of the LCA if it exists. Why?
When you propagate counts upward in the tree (each node adds its count to its parent), the increments at u and v will bubble up to the LCA. The decrements at the LCA cancel out the extra counts that should not go higher.
This is similar to a difference array on a linear range, but adapted for trees. Mark endpoints with , mark the split point (LCA) with , and let the tree structure propagate the values correctly.
The trick converts O(path length) work per path into O() work per path.