A prefix sum stores running totals so any range total becomes one subtraction. If prefix[i] is the total of the first i values, the range from left to right is prefix[right + 1] - prefix[left]. The extra zero at the front is what lets a range start at index 0 without a special case.
Reach for prefix sums when a problem asks many range questions about data that does not change, or asks you to count ranges with a given total. Counting ranges pairs prefix sums with a hash map: a range ending here sums to the target when an earlier prefix equals the current total minus the target. That works with negative numbers, where a sliding window cannot decide when to shrink.
A difference array runs the idea backward. Instead of storing values, store where each change starts and where it stops, then take a running sum at the end. The same subtraction trick extends to grids: a 2-D prefix table answers any rectangle total with four lookups.
A range total is the difference of two prefix sums.