human
What is a code to final Matrix after incrementing submatrices by K in range given by Q queries and the other steps required given the following:
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"
Solution in Python:
assistant
#Here is code to final Matrix after incrementing submatrices by K in range given by Q queries and the other steps required.
 def updateQuery ( from_x , from_y , to_x , to_y , k , aux ) :
	aux [ from_x ] [ from_y ] += k
	if ( to_x + 1 < 3 ) :
		aux [ to_x + 1 ] [ from_y ] -= k
	if ( to_x + 1 < 3 and to_y + 1 < 4 ) :
		aux [ to_x + 1 ] [ to_y + 1 ] += k
	if ( to_y + 1 < 4 ) :
		aux [ from_x ] [ to_y + 1 ] -= k
	return aux
def updatematrix ( mat , aux ) :
	for i in range ( 3 ) :
		for j in range ( 1 , 4 ) :
			aux [ i ] [ j ] += aux [ i ] [ j - 1 ]
	for i in range ( 4 ) :
		for j in range ( 1 , 3 ) :
			aux [ j ] [ i ] += aux [ j - 1 ] [ i ]
	for i in range ( 3 ) :
		for j in range ( 4 ) :
			mat [ i ] [ j ] += aux [ i ] [ j ]
	return mat
def printmatrix ( mat ) :
	for i in range ( 3 ) :
		for j in range ( 4 ) :
			print ( mat [ i ] [ j ] , end = " ▁ " )
		print ( )
def matrixQuery ( mat , Q , q ) :
	aux = [ [ 0 for i in range ( 4 ) ] for i in range ( 3 ) ]
	for i in range ( Q ) :
		updateQuery ( q [ i ] [ 0 ] , q [ i ] [ 1 ] , q [ i ] [ 2 ] , q [ i ] [ 3 ] , q [ i ] [ 4 ] , aux )
	updatematrix ( mat , aux )
	printmatrix ( mat )
if __name__ == ' _ _ main _ _ ' :
	mat = [ [ 1 , 0 , 1 , 2 ] , [ 0 , 2 , 4 , 1 ] , [ 1 , 2 , 1 , 0 ] ]
	Q = 1
	q = [ [ 0 , 0 , 1 , 1 , 2 ] ]
	matrixQuery ( mat , Q , q )