Is my implementation is correct?
The time complexity of the solution is O(Q)?
Any further optimization I can do?
What if I want to first input all the queries then output the result of each query one by one?
Is my implementation is correct?
The time complexity of the solution is O(Q)?
Any further optimization I can do?
What if I want to first input all the queries then output the result of each query one by one?
@shreyanshvarshney
Take a look at this implementation https://ide.codingblocks.com/s/184797 14.
And for queries ,you will simply need to take input for how many queries there are and then simply run a loop for accepting that many queries and using your formula that many times to compute the answer.
aux matrix is the matrix in which we are precomputing the prefix sums of the matrix elements so that we can answer the queries in O(1) time. I would suggest you to dry run the code for a 3X3 matrix example to see how we are calculating it and how the formula is working.