Difference (thought)
Given n numbers, and then Q queries, each query gives l, r, x, and asks you to add x to every value from l to r, and only gives you O(n) time range ,How to do?
Think about it:
1. If you are violent, just click l and r and make you O(n^2)T a dog.
2. Use a line segment tree or tree array to do it. Sorry, this complexity is O (Qlogn), but it will still be T (although they solve other problems very NB)
3. Difference, yes, it is the title, I am very happy O(n)+constant...
So how to solve it with difference?
------In the inquiry, we do not go to for to add x, but make a mark, and add it together at the end
Realization:
First open a special difference array (size = sequence length in the question)
eg:
Original sequence: 1, 2, 3, 4, 5, 6, 7
First operation: (l, r, x) = (2, 4, 7)
We add one to the 2 position in the difference array 7, then subtract 7 from the position of 4+1 ;
The second operation: (l, r, x) = (1, 3, 4)
We add a 4 to the 1 position in the difference array , and then subtract 4 from the 3+1 position ;
The difference array is: 4 7 0 -4 -7 0 0
The actual operation is: cf[i]=cf[i-1]+cf[i]
(cf: difference)
The modified difference array is: 4 11 11 7 0 0 0
Then apply this difference array to the original array