Array range queries over range queries | Function to create the record array ; Driver Code ; Build query matrix ; If query is of type 2 then function call to record_sum ; If query is of type 1 then simply add 1 to the record array ; for type 1 queries adding the contains of record array to the main array record array ; printing the array
function record_sum ( record , l , r , n , adder ) { for ( let i = l ; i <= r ; i ++ ) { record [ i ] += adder ; } } let n = 5 , m = 5 ; let arr = new Array ( n ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { arr [ i ] = 0 ; } let query = [ [ 1 , 1 , 2 ] , [ 1 , 4 , 5 ] , [ 2 , 1 , 2 ] , [ 2 , 1 , 3 ] , [ 2 , 3 , 4 ] ] ; let record = new Array ( m ) ; for ( let i = 0 ; i < record . length ; i ++ ) { record [ i ] = 0 ; } for ( let i = m - 1 ; i >= 0 ; i -- ) { if ( query [ i ] [ 0 ] == 2 ) { record_sum ( record , query [ i ] [ 1 ] - 1 , query [ i ] [ 2 ] - 1 , m , record [ i ] + 1 ) ; } else { record_sum ( record , i , i , m , 1 ) ; } } for ( let i = 0 ; i < m ; i ++ ) { if ( query [ i ] [ 0 ] == 1 ) { record_sum ( arr , query [ i ] [ 1 ] - 1 , query [ i ] [ 2 ] - 1 , n , record [ i ] ) ; } } for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; }