We are given a N x M matrix. There are two types of queries - 0 and 1. For the queries of type 0 we need to update the value at a given index to a given value. For the queries of type 1 we need to print the sum of elements of the matrix in the given range.
For the queries of type 0, you just need to update the value at the given index to the given value. For the given values of indices x and y and a value val :-
a[x][y] = val
For the queries of type 1, you need to answer the queries. You are given 4 indices x1, y1, x2 and y2. You need to print the sum of all the elements between the bounds of (x1, y1) and (x2, y2). Simply run two loops for the given bounds and add up the elements. The pseudocode will look somewhat like this :-
sum = 0
for(i = x1; i <= x2; i++)
for(j = y1; j <= y2; j++)
sum := sum + a[i][j]
In programming languages like C, C++ you need to declare sum as a long long int, otherwise it will overflow.
You can find the authors solution below.
AUTHOR’S AND EDITORIALIST’S SOLUTIONS:
Author’s and editorialist’s solution can be found here.