Count of intersections of M line segments with N vertical lines in XY plane | Function to create prefix sum array ; Initialize the prefix array to remove garbage values ; Marking the occurences of vertical lines ; x is the value after Index mapping ; Creating the prefix array ; Function returns the count of total letersection ; ans is the number of polets of letersection of the line segments with the vertical lines ; Index mapping ; We don 't consider a vertical  line segment because even if  it falls on a verticale line  then it just touches it and  not letersects. ; We have assumed that x1 will be left and x2 right but if not then we just swap them ; N is the number of vertical lines M is the number of line segments ; Format : x1 , y1 , x2 , y1 ; First create the prefix array ; Prlet the total number of letersections
function createPrefixArray ( n , arr , prefSize , pref ) { for ( let i = 0 ; i < prefSize ; i ++ ) { pref [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { let x = arr [ i ] + 1000000 ; pref [ x ] ++ ; } for ( let i = 1 ; i < prefSize ; i ++ ) { pref [ i ] += pref [ i - 1 ] ; } } function poletsOfLetersection ( m , segments , size , pref ) { let ans = 0 ; for ( let i = 0 ; i < m ; i ++ ) { let x1 = segments [ i ] [ 0 ] ; let x2 = segments [ i ] [ 2 ] ; x1 = x1 + 1000000 ; x2 = x2 + 1000000 ; if ( x1 != x2 ) { if ( x1 > x2 ) { let temp = x1 ; x1 = x2 ; x2 = temp ; } let Occ_Till_Right = pref [ x2 - 1 ] ; let Occ_Till_Left = pref [ x1 ] ; ans = ans + ( Occ_Till_Right - Occ_Till_Left ) ; } } return ans ; } let N = 4 ; let M = 8 ; let size = 2000000 + 2 ; let pref = Array . from ( { length : size } , ( _ , i ) => 0 ) ; let lines = [ - 5 , - 3 , 2 , 3 ] ; let segments = [ [ - 2 , 5 , 5 , - 6 ] , [ - 5 , - 2 , - 3 , - 5 ] , [ - 2 , 3 , - 6 , 1 ] , [ - 1 , - 3 , 4 , 2 ] , [ 2 , 5 , 2 , 1 ] , [ 4 , 5 , 4 , - 5 ] , [ - 2 , - 4 , 5 , 3 ] , [ 1 , 2 , - 2 , 1 ] ] ; createPrefixArray ( N , lines , size , pref ) ; document . write ( poletsOfLetersection ( M , segments , size , pref ) + " " ) ;