Competitive Programming - Prefix Sums (4/7)

Опубликовано: 04 Сентябрь 2026
на канале: Erik Krohn
8
0

We introduce difference arrays, a technique for efficiently handling a large number of range updates. We start with the naïve approach of updating every element in a range, then learn how to instead record only where each update starts and stops.

Using a difference array, each range update takes (O(1)) work. We then use a prefix sum to reconstruct the final array in (O(n)) time.

The key pattern is: Record changes -- Prefix Sum -- Recover values

This lecture shows how an idea you've already learned, prefix sums, can be used in a new way to turn potentially expensive range updates into an efficient solution.