Given an array of n elements and q range queries (range sum in this article) with no updates, task is to answer these queries with efficient time and space complexity. The time complexity of a range query after applying square root decomposition comes out to be O(√n). This square-root factor can be decreased to a constant linear factor by applying square root decomposition on the block of the array which was decomposed earlier.
As we apply square root decomposition to the given array, querying a range-sum comes in O(√n) time.
Here, calculate the sum of blocks which are in between the blocks under consideration(corner blocks), which takes O(√n) iterations.
Initial Array :
Decomposition of array into blocks :
And the calculation time for the sum on the starting block and ending block both takes O(√n) iterations.
Which will leaves us per query time complexity of :
= O(√n) + O(√n) + O(√n) = 3 * O(√n) ~O(√n)
Here, we can reduce the runtime complexity of our query algorithm cleverly by calculating blockwise prefix sum and using it to calculate the sum accumulated in the blocks which lie between the blocks under consideration. Consider the code below :
interblock_sum[x1][x2] = prefixData[x2 - 1] - prefixData[x1];
Time taken for calculation of above table is :
= O(√n) * O(√n) ~O(n)
NOTE : We haven’t taken the sum of blocks x1 & x2 under consideration as they might be carrying partial data.
Prefix Array :
Suppose we want to query for the sum for range from 4 to 11, we consider the sum between block 0 and block 2 (excluding the data contained in block 0 and block 1), which can be calculated using the sum in the green coloured blocks represented in the above image.
Sum between block 0 and block 2 = 42 – 12 = 30
For calculation of rest of the sum present in the yellow blocks, consider the prefix array at the decomposition level-2 and repeat the process again.
Here, observe that we have reduced our time complexity per query significantly, though our runtime remains similar to our last approach :
Our new time complexity can be calculated as :
= O(√n) + O(1) + O(√n) = 2 * O(√n) ~O(√n)
Square-root Decomposition at Level-2 :
Further, we apply square root decomposition again on every decomposed block retained from the previous decomposition. Now at this level, we have approximately √√n sub-blocks in each block which were decomposed at last level. So, we need to run a range query on these blocks only two times, one time for starting block and one time for ending block.
Precalcuation Time taken for level 2 decomposition :
No of blocks at level 1 ~ √n
No of blocks at level 2 ~ √√n
Level-2 Decomposition Runtime of a level-1 decomposed block :
Overall runtime of level-2 decomposition over all blocks :
= O(√n) * O(√n) ~O(n)
Now, we can query our level-2 decomposed blocks in O(√√n) time.
So, we have reduced our overall time complexity from O(√n) to O(√√n)
Time complexity taken in querying edge blocks :
= O(√√n) + O(1) + O(√√n) = 2 * O(√√n) ~O(√√n)
Total Time complexity can be calculated as :
= O(√√n)+O(1)+O(√√n) = 2 * O(√√n) ~O(√√n)
Square-root Decomposition at Level-3 :
Using this method we can decompose our array again and again recursively d times to reduce our time complexity to a factor of constant linearity.
O(d * n1/(2^d)) ~ O(k), as d increases this factor converges to a constant linear term
The code presented below is a representation of triple square root decomposition where d = 3:
O(q * d * n1/(2 ^ 3)) ~ O(q * k) ~ O(q)
[where q repesents number of range queries]
44 39 58 51 25
Time Complexity : O(q * d * n1/(2^3)) ≈ O(q * k) ≈ O(q)
Auxiliary Space : O(k * n) ≈ O(n)
Note : This article is to only explain the method of decomposing the square root to further decomposition.
- Queries of nCr%p in O(1) time complexity
- How is the time complexity of Sieve of Eratosthenes is n*log(log(n))?
- Extended Knapsack Problem
- Knowing the complexity in competitive programming
- Cyclomatic Complexity
- Complexity Analysis of Binary Search
- Algorithm Library | C++ Magicians STL Algorithm
- Real time optimized KMP Algorithm for Pattern Searching
- Maximum removal from array when removal time >= waiting time
- Find the time which is palindromic and comes after the given time
- Suffix Array | Set 2 (nLogn Algorithm)
- MO's Algorithm (Query Square Root Decomposition) | Set 1 (Introduction)
- kasai’s Algorithm for Construction of LCP array from Suffix Array
- Tarjan's off-line lowest common ancestors algorithm
- A* Search Algorithm
- DDA Line generation Algorithm in Computer Graphics
- Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
- Bresenham’s Line Generation Algorithm
- Mid-Point Line Generation Algorithm
- Point Clipping Algorithm in Computer Graphics
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.