Given a rectangular matrix M[0…n-1][0…m-1], and queries are asked to find the sum / minimum / maximum on some sub-rectangles M[a…b][e…f], as well as queries for modification of individual matrix elements (i.e M[x] [y] = p ).
We can also answer sub-matrix queries using Two Dimensional Binary Indexed Tree.
In this article, We will focus on solving sub-matrix queries using two dimensional segment tree.Two dimensional segment tree is nothing but segment tree of segment trees.
Prerequisite : Segment Tree – Sum of given range
We will build a two-dimensional tree of segments by the following principle:
1 . In First step, We will construct an ordinary one-dimensional segment tree, working only with the first coordinate say ‘x’ and ‘y’ as constant. Here, we will not write number in inside the node as in the one-dimensional segment tree, but an entire tree of segments.
2. The second step is to combine the values of segmented trees. Assume that in second step instead of combining the elements we are combining the segment trees obtained from the step first.
Consider the below example. Suppose we have to find the sum of all numbers inside the highlighted red area
Step 1 : We will first create the segment tree of each strip of y- axis.We repesent the segment tree here as an array where child node is 2n and 2n+1 where n > 0.
Step 2: In this step, we create the segment tree for the rectangular matrix where the base node are the strips of y-axis given above.The task is to merge above segment trees.
Sum Query :
Thanks to Sahil Bansal for contributing this image.
Processing Query :
We will respond to the two-dimensional query by the following principle: first to break the query on the first coordinate, and then, when we reached some vertex of the tree of segments with the first coordinate and then we call the corresponding tree of segments on the second coordinate.
This function works in time O(logn*log m), because it first descends the tree in the first coordinate, and for each traversed vertex of that tree, it makes a query from the usual tree of segments along the second coordinate.
Modification Query :
We want to learn how to modify the tree of segments in accordance with the change in the value of an element M[x] [y] = p .It is clear that the changes will occur only in those vertices of the first tree of segments that cover the coordinate x, and for the trees of the segments corresponding to them, the changes will only occur in those vertices that cover the coordinate y. Therefore, the implementation of the modification request will not be very different from the one-dimensional case, only now we first descend the first coordinate, and then the second.
Output for the highlighted area will be 25.
Below is the implementation of above approach :
The sum of the submatrix (y1, y2)->(2, 3), (x1, x2)->(2, 3) is 25 The sum of the submatrix (y1, y2)->(2, 3), (x1, x2)->(2, 3) is 118
Time complexity :
Processing Query : O(logn*logm)
Modification Query: O(2*n*logn*logm)
Space Complexity : O(4*m*n)
- Two Dimensional Binary Indexed Tree or Fenwick Tree
- K Dimensional Tree | Set 3 (Delete)
- K Dimensional Tree | Set 1 (Search and Insert)
- K Dimensional Tree | Set 2 (Find Minimum)
- Overview of Data Structures | Set 3 (Graph, Trie, Segment Tree and Suffix Tree)
- LIS using Segment Tree
- Segment Tree | Set 1 (Sum of given range)
- Segment Tree | Set 3 (XOR of given range)
- Segment Tree | (XOR of a given range )
- Reconstructing Segment Tree
- Lazy Propagation in Segment Tree
- Lazy Propagation in Segment Tree | Set 2
- Persistent Segment Tree | Set 1 (Introduction)
- Segment tree | Efficient implementation
- Segment Tree | Set 2 (Range Minimum Query)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.