plz explain the soluiton of JUNE LONG CHALLENGE CHSQARR by sparse table mathod

This problem can be reduced into trying out sub-matrices of different sizes and then applying sliding window method and calculating the max value and sum of every sub-matrix. Now if we do that the complexity for each sub-matrix check is (sub-matrix length * sub-matrix width) which becomes O(matrix length*matrix width) in the worst case. And again for the sliding window process the complexity is O(matrix length*matrix width) , so the overall complexity is O(matrix length*matrix width*matrix length*matrix width).

for n,m as much as 1000 this process isn’t even worth trying. So we need to do some optimization.

For calculating sum we can simply follow 2D cumulative sum approach which gives the result in O(1).

Now for calculating maximum element what should we do ? We can do better than Brute-Force checking by forming a 2D segment tree that reduces the complexity to O(n*m*log(n)*log(m)). But even this is not enough and causes TLE( It caused me TLE )

Now since the matrix is static(i.e. not updating) we can do better. Instead of segment tree we can use a 2D sparse table. This initialization takes O(n*m*log(n)*log(m)) time and per query time is O(1).

Now how can we construct a 2D sparse table ? This link gives all that you need

You need to figure out the sum and maximum value in all sub-matrices of size a x b for each query. Sum can be pre-computed. For maximum value, use sliding window algorithm ( http://www.geeksforgeeks.org/maximum-of-all-subarrays-of-size-k/ ) for each query. The overall complexity reduces to O( Q*N*M). Using segment tree to compute maximum values makes complexity O( Q*N*M*(log(N)+ log( M))), therefore you get TLE.

@robin_0_chef can you share me your segment tree approach? I presume u created a segment tree of segment trees of each row of the matrix?

Hey try this article http://www.geeksforgeeks.org/given-n-x-n-square-matrix-find-sum-sub-squares-size-k-x-k/

After reading and understanding the concept behind this one, you will be able to solve the question.

Instead of k*k, you have to find maximum and sum for a*b.(I assume that you know about how to find maximum in array, just extend it for 2-d array).

Happy Coding and cheers…

your code looks decent can u explain ??

@atulshanbhag I used the idea of quad tree while implementing the segment tree approach.

Here you can see my solution The reason it is TLE is due to the fact that O(1) vs O(logn) per query

You can get some more insights from here

@robin_0_chef yes i did read about it… you have used quad tree approach. basically a segment tree of segment tree is the same as a quad tree… will try your approach too… thanks for the link

@rohitangira my code which i submitted during contest uses the sliding window technique. i precomputed the sums of all sub-matrices sum[i][j] for the sub matrix defined by (0,0) to (i,j), complexity being O(MN). and then each query requires O(MN) using sliding window technique.