Final Matrix after incrementing submatrices by K in range given by Q queries | Function to update the given query ; Update top cell ; Update bottom left cell ; Update bottom right cell ; Update top right cell ; Function that updates the matrix mat [ ] [ ] by adding elements of aux [ ] [ ] ; Compute the prefix sum of all columns ; Compute the prefix sum of all rows ; Get the final matrix by adding mat and aux matrix at each cell ; Function that prints matrix mat [ ] ; Traverse each row ; Traverse each columns ; Function that performs each query in the given matrix and print the updated matrix after each operation performed ; Initialize all elements to 0 ; Update auxiliary matrix by traversing each query ; Update Query ; Compute the final answer ; Print the updated matrix ; Driver Code ; Given 4 atrix ; Given Queries ; Function Call
def updateQuery ( from_x , from_y , to_x , to_y , k , aux ) : NEW_LINE INDENT aux [ from_x ] [ from_y ] += k NEW_LINE if ( to_x + 1 < 3 ) : NEW_LINE INDENT aux [ to_x + 1 ] [ from_y ] -= k NEW_LINE DEDENT if ( to_x + 1 < 3 and to_y + 1 < 4 ) : NEW_LINE INDENT aux [ to_x + 1 ] [ to_y + 1 ] += k NEW_LINE DEDENT if ( to_y + 1 < 4 ) : NEW_LINE INDENT aux [ from_x ] [ to_y + 1 ] -= k NEW_LINE DEDENT return aux NEW_LINE DEDENT def updatematrix ( mat , aux ) : NEW_LINE INDENT for i in range ( 3 ) : NEW_LINE INDENT for j in range ( 1 , 4 ) : NEW_LINE INDENT aux [ i ] [ j ] += aux [ i ] [ j - 1 ] NEW_LINE DEDENT DEDENT for i in range ( 4 ) : NEW_LINE INDENT for j in range ( 1 , 3 ) : NEW_LINE INDENT aux [ j ] [ i ] += aux [ j - 1 ] [ i ] NEW_LINE DEDENT DEDENT for i in range ( 3 ) : NEW_LINE INDENT for j in range ( 4 ) : NEW_LINE INDENT mat [ i ] [ j ] += aux [ i ] [ j ] NEW_LINE DEDENT DEDENT return mat NEW_LINE DEDENT def printmatrix ( mat ) : NEW_LINE INDENT for i in range ( 3 ) : NEW_LINE INDENT for j in range ( 4 ) : NEW_LINE INDENT print ( mat [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT def matrixQuery ( mat , Q , q ) : NEW_LINE INDENT aux = [ [ 0 for i in range ( 4 ) ] for i in range ( 3 ) ] NEW_LINE for i in range ( Q ) : NEW_LINE INDENT updateQuery ( q [ i ] [ 0 ] , q [ i ] [ 1 ] , q [ i ] [ 2 ] , q [ i ] [ 3 ] , q [ i ] [ 4 ] , aux ) NEW_LINE DEDENT updatematrix ( mat , aux ) NEW_LINE printmatrix ( mat ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 1 , 0 , 1 , 2 ] , [ 0 , 2 , 4 , 1 ] , [ 1 , 2 , 1 , 0 ] ] NEW_LINE Q = 1 NEW_LINE q = [ [ 0 , 0 , 1 , 1 , 2 ] ] NEW_LINE matrixQuery ( mat , Q , q ) NEW_LINE DEDENT