How to update a given range in an array after multiple queries?

Viewed 128

Suppose we have an array of length 10 like [1,1,1,1,1,1,1,1,1,1]. After multiple queries of range, I wanted to update this array in this manner:

update(2,5)  [1,2,2,2,2,1,1,1,1,1]

update (3,4) [1,2,3,3,2,1,1,1,1,1]

update (1,3) [2,3,4,3,2,1,1,1,1,1]

update(5,6)  [2,3,4,3,3,2,1,1,1,1]

and so on.

In short the update function will increase the values within the given range by 1.

It's not necessary to print the array after each update. I want to get the array after Q queries. So is there any efficient way to do this? I already did the naive approach which took O(n^2) time.

0 Answers
Related