Apply 3 range updates to an array of 8 zeros. Instead of touching every index in each range, record where the change starts and where it stops. The extra slot 8 catches changes that run to the end.
A difference array of 8 plus one slots, all zero. Applying 3 range updates by recording only the endpoints.
Update 1: add 5 to indices 1 to 3. Two writes: +5 at 1, where it starts, and -5 at 4, where it stops. The 2 cells between them are never touched.
Add 5 to indices 1 through 3 by writing +5 at slot 1 and -5 at slot 4.
Update 2: add 2 to indices 2 to 6. Two writes: +2 at 2, where it starts, and -2 at 7, where it stops. The 4 cells between them are never touched.
Add 2 to indices 2 through 6 by writing +2 at slot 2 and -2 at slot 7.
Update 3: add -3 to indices 0 to 4. Two writes: -3 at 0, where it starts, and +3 at 5, where it stops. The 4 cells between them are never touched.
Add -3 to indices 0 through 4 by writing -3 at slot 0 and +3 at slot 5.
All 3 updates recorded with 6 writes. Now one prefix pass: each cell becomes the running sum of the deltas so far, which is exactly the updates covering that index.
All updates are recorded. A prefix sum pass now turns the deltas into final values.
Index 0: delta -3. Running sum 0 -3 = -3, which is the final value at 0.
Index 0 becomes -3.
Index 1: delta +5. Running sum -3 +5 = 2, which is the final value at 1.
Index 1 becomes 2.
Index 2: delta +2. Running sum 2 +2 = 4, which is the final value at 2.
Index 2 becomes 4.
Index 3: no delta here, so the running sum stays 4 and that is the value.
Index 3 becomes 4.
Index 4: delta -5. Running sum 4 -5 = -1, which is the final value at 4.
Index 4 becomes -1.
Index 5: delta +3. Running sum -1 +3 = 2, which is the final value at 5.
Index 5 becomes 2.
Index 6: no delta here, so the running sum stays 2 and that is the value.
Index 6 becomes 2.
Index 7: delta -2. Running sum 2 -2 = 0, which is the final value at 7.
Index 7 becomes 0.
Final array: -3, 2, 4, 4, -1, 2, 2, 0. The slot at 8 is only ever a place for deltas to stop. 3 updates cost 2 writes each plus one pass of 8, instead of walking every range.
The final values are -3, 2, 4, 4, -1, 2, 2, 0. Each update cost two writes plus one shared pass.