Problem 214
Range Increment Updates
Medium · 20 pts · Array, Prefix Sum, Difference Array
Problem
An array of N elements starts with every value 0. You receive Q updates of the form L R V, each adding V to every element from index L to index R, inclusive (zero-based). V can be negative. Print the array after all updates. Applying each update element by element can take O(N × Q). Use a difference array instead: add V at index L and subtract V at index R + 1. After all updates, the running sum of the difference array gives the final values.
Input
First line: integers N and Q. Next Q lines: L R V.
Output
Print the N final values, space-separated.
Constraints
1 <= N, Q <= 100000; 0 <= L <= R < N; -10000 <= V <= 10000
Example input
5 2 1 3 2 2 4 3
Example output
0 2 5 5 3
Explanation
Adding 2 to indices 1 to 3 gives 0 2 2 2 0. Adding 3 to indices 2 to 4 then gives 0 2 5 5 3.