Data Structures19 sections · 729 units
Open in Course

Difference Array Insight

How updates work

To add vv to all elements from index ll to rr, you only need to modify two positions in the difference array: diff[l]+=vdiff[l] += v diff[r+1]=vdiff[r+1] -= v That's it.

Two operations, regardless of how big the range is. Why does this work?

When you later compute the prefix sum of diffdiff, adding vv at position ll propagates forward to all later positions. Subtracting vv at r+1r+1 cancels it for positions after rr.