Print matrix after applying increment operations in M ranges | Function to update and print the matrix after performing queries ; Add 1 to the first element of the sub - matrix ; If there is an element after the last element of the sub - matrix then decrement it by 1 ; Calculate the running sum ; Print the updated element ; Next line ; Size of the matrix ; Queries
< ? php function updateMatrix ( $ n , $ q , $ mat ) { for ( $ i = 0 ; $ i < sizeof ( $ q ) ; $ i ++ ) { $ X1 = $ q [ $ i ] [ 0 ] ; $ Y1 = $ q [ $ i ] [ 1 ] ; $ X2 = $ q [ $ i ] [ 2 ] ; $ Y2 = $ q [ $ i ] [ 3 ] ; $ mat [ $ X1 ] [ $ Y1 ] ++ ; if ( $ Y2 + 1 < $ n ) $ mat [ $ X2 ] [ $ Y2 + 1 ] -- ; else if ( $ X2 + 1 < $ n ) $ mat [ $ X2 + 1 ] [ 0 ] -- ; } $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ sum += $ mat [ $ i ] [ $ j ] ; echo ( $ sum . " " ) ; } echo ( " STRNEWLINE " ) ; } } $ n = 5 ; $ mat = array_fill ( 0 , $ n , array_fill ( 0 , $ n , 0 ) ) ; $ q = array ( array ( 0 , 0 , 1 , 2 ) , array ( 1 , 2 , 3 , 4 ) , array ( 1 , 4 , 3 , 4 ) ) ; updateMatrix ( $ n , $ q , $ mat ) ; ? >