VOOZH about

URL: https://www.geeksforgeeks.org/dsa/sum-and-update-queries-on-2d-matrix/

⇱ Sum and Update Queries on 2D Matrix - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sum and Update Queries on 2D Matrix

Last Updated : 23 Jul, 2025

Given a 2D matrix mat[][] of size NXN with all its elements initialized to 0, the task is to answer Q queries. Each query[i] can be one of the following types:

  • Type 1 [1, x, y, ele]: Update the value of cell(x, y) to val.
  • Type 2 [2, x1, y1 ,x2, y2]: Print the sum of the submatrix (x1, y1) to (x2, y2)

Print the answer of all the queries of type 2. The matrix and queries follow 0-based indexing.

Examples:

Input: N = 3, queries[][] = {{1, 2, 0, 2} , {2, 0, 0, 2, 2}, {1, 0, 0, 1}, {2, 0, 0, 2, 0} }
Output: [2, 3]
Explanation:

👁 fenwick_1

The first query is to update the value of cell (2, 0) to 2. The second query is to take the sum of the submatrix (0, 0) to (2, 2) which is equal to 2. The third query is to update the value of cell (0, 0) to 1. The fourth query is to take the sum of the submatrix (0, 0) to submatrix (2, 0) which is equal to 3.

Input: N = 3, queries = { { 1, 0, 2, 2 }, { 1, 0, 0, 1 }, { 2, 0, 0, 2, 2 }, { 1, 0, 1, 1 }, { 2, 0, 0, 0, 2 } }
Output: [3, 4]
Explanation: The first query is to update the value of cell (0, 2) to 2. The second query is to update the value of cell (0, 0) to 1. The third query is to take the sum of the submatrix (0, 0) to (2, 2), which is equal to 3. The fourth query is to update the value of cell (0, 1) to 1 and the fifth query is to take the sum of the submatrix (0, 0) to submatrix (0, 2) which is equal to 4.

Approach: The problem can be solved using the following approach:

Since we need to answer the update and sum queries in log(N) time, we can solve them using Fenwick tree on 2D matrix. We can maintain a 2D Binary Indexed Tree to store the partial sums of the matrices.

Point Update: Whenever we have to update a cell (x, y), we start with the current row x and update all the cells in row x which stores the partial sum of cell (x, y) by changing y to y + (y & -y). Then, we move to the next row which stores the partial sum of cell (x, y) by changing x to x + (x & -x).

Range Sum: Whenever we have to find the range sum of submatrix (x1, y1) to (x2, y2) then we can calculate it using the formula: prefixSum(x2, y2) - prefixSum(x2, y1) - prefixSum(x1, y2) + prefixSum(x2, y2)

Steps to solve the problem:

  • Initialize a 2-D Binary Indexed Tree of size (N+1) * (N + 1), because of 1-based indexing.
  • To handle the updates, maintain a function update(x, y, ele) which updates all the submatrices which store the partial sum of cell (x, y)
  • To handle the queries, maintain a method getSum(x, y) which calculate the prefix sum of of submatrix (x, y) and use the formula prefixSum(x2, y2) - prefixSum(x2, y1) - prefixSum(x1, y2) + prefixSum(x2, y2) to calculate the sum of submatrices and print the answer.

Below is the implementation of the above approach:


Output
2 3 

Time Complexity: O((N * N + Q)*log(N)*log(N)), where N is the number of rows and columns in input matrix mat[][] and Q is the number of queries.

  • Both update(x, y, ele) function and getSum(x, y) function takes O(log(N)*log(N)) time.
  • Building the Binary Indexed Tree takes O(N * N * log(N)*log(N)) time.
  • Since in each of the queries we are calling getSum(x, y) function so answering all the Q queries takes O(Q*log(N)*log(N)) time.

So overall time complexity becomes O((N * N + Q)*log(N)*log(N)).

Auxiliary Space: O(N * N)

Comment