Data Structures19 sections · 729 units
Open in Course

Dynamic Path Solution

With lazy propagation

function add(u, v, x)
    makeRoot(u)
    access(v)
    // Path u-v is v's auxiliary tree
    v.lazy := v.lazy + x
    // Don't pushDown yet - lazy stays until needed

function query(u, v)
    makeRoot(u)
    access(v)
    return v.subtreeSum

Remember to:

  • Push down lazy before accessing children
  • Push up sums after rotations
  • Handle reversal flag correctly with lazy add

The combination of reversal and lazy add requires careful ordering in pushDown.