‘N’ = 4, ‘K’ = 3, ‘coins’ = {0, 2, 3}, {1, 3, -5}, {0, 0, 7}.
Now in this example, after the first operation, the value on the boxes will be {0, 0, 0, 0} -> {3, 3, 3, 0}, after the second operation, the values will be {3, 3, 3, 0} -> {3, -2, -2, -5} and after the last operation the values are {3, -2, -2, 5} -> {10, -2, -2, 5}. Hence the answer is {10, -2, -2, 5}.
The first line of input format contains ‘T’, denoting the number of test cases. Then each test case follows.
The first line of each test case contains two integers ‘N’ and ‘K’, denoting the number of boxes Tanziro has and the number of operations Nezuko wants to perform, respectively.
The next ‘K’ lines of the test case contain 3 integers, the starting index of the box, the ending index, and the value to be updated.
For each test case, print an array denoting the final values on the boxes.
Output for every query will be printed in a separate line.
You are not required to print anything explicitly. It has already been taken care of. Just implement the functions.
1 <= T <= 10
1 <= ‘N’ <= 5000
1 <= ‘K’ <= 5000
0 <= ‘value[i][0]’, ‘value[i][1]’ <= N - 1
-5000 <= ‘value[i][2]’ <= 5000
Time Limit: 1 second
The approach is simple, we will iterate through all the operations from 1 to K and for each operation, we will iterate through the starting index and go to the ending index and update every value by the given value.
The steps are as follows:
Let’s look at the problem in a different way. We will not update the whole array, we will just update ‘answer’ at starting index with a positive value of ‘value[i][2]’ and the ending index with a negative value, and when all the operations are finished then we will iterate through the ‘answer’ array and we will apply cumulative sum operation on the whole array i.e. we will update ‘answer[i]’ with ‘answer[i]’ + ‘answer[i - 1]’.
The steps are as follows: