def       A__   (						snake_case_			:		Union[str, Any]   ):
 SCREAMING_SNAKE_CASE__: Optional[Any]=   0
 SCREAMING_SNAKE_CASE__: int=   len(snake_case_   )

 for i in range(n - 1   ):
  for j in range(i + 1							,					snake_case_   ):
   if arr[i] > arr[j]:
    num_inversions += 1

 return num_inversions







def       A__   (						snake_case_			:		List[Any]   ):
 if len(snake_case_   ) <= 1:
  return arr, 0
 SCREAMING_SNAKE_CASE__: List[str]=   len(snake_case_   ) // 2
 SCREAMING_SNAKE_CASE__: Dict=   arr[0:mid]
 SCREAMING_SNAKE_CASE__: Optional[int]=   arr[mid:]

 SCREAMING_SNAKE_CASE__,						SCREAMING_SNAKE_CASE__: Any=   count_inversions_recursive(snake_case_   )
 SCREAMING_SNAKE_CASE__,						SCREAMING_SNAKE_CASE__: Union[str, Any]=   count_inversions_recursive(snake_case_   )
 SCREAMING_SNAKE_CASE__,						SCREAMING_SNAKE_CASE__: int=   _count_cross_inversions(snake_case_							,					snake_case_   )

 SCREAMING_SNAKE_CASE__: List[Any]=   inversion_p + inversions_q + cross_inversions
 return c, num_inversions







def       A__   (						snake_case_			:		Dict							,					snake_case_			:		List[Any]   ):
 SCREAMING_SNAKE_CASE__: List[Any]=   []
 SCREAMING_SNAKE_CASE__: Dict=   0
 while i < len(snake_case_   ) and j < len(snake_case_   ):
  if p[i] > q[j]:
   # if P[1] > Q[j], then P[k] > Q[k] for all  i < k <= len(P)
   # These are all inversions. The claim emerges from the
   # property that P is sorted.
   num_inversion += len(snake_case_   ) - i
   r.append(q[j]   )
   j += 1
  else:
   r.append(p[i]   )
   i += 1

 if i < len(snake_case_   ):
  r.extend(p[i:]   )
 else:
  r.extend(q[j:]   )

 return r, num_inversion







def       A__   (						):
 SCREAMING_SNAKE_CASE__: List[Any]=   [10, 2, 1, 5, 5, 2, 11]

 # this arr has 8 inversions:
 # (10, 2), (10, 1), (10, 5), (10, 5), (10, 2), (2, 1), (5, 2), (5, 2)

 SCREAMING_SNAKE_CASE__: Optional[int]=   count_inversions_bf(snake_case_   )
 SCREAMING_SNAKE_CASE__,						SCREAMING_SNAKE_CASE__: Optional[Any]=   count_inversions_recursive(snake_case_   )

 assert num_inversions_bf == num_inversions_recursive == 8

 print('''number of inversions = '''							,					snake_case_   )

 # testing an array with zero inversion (a sorted arr_1)

 arr_a.sort()
 SCREAMING_SNAKE_CASE__: Any=   count_inversions_bf(snake_case_   )
 SCREAMING_SNAKE_CASE__,						SCREAMING_SNAKE_CASE__: Optional[Any]=   count_inversions_recursive(snake_case_   )

 assert num_inversions_bf == num_inversions_recursive == 0
 print('''number of inversions = '''							,					snake_case_   )

 # an empty list should also have zero inversions
 SCREAMING_SNAKE_CASE__: Optional[Any]=   []
 SCREAMING_SNAKE_CASE__: List[str]=   count_inversions_bf(snake_case_   )
 SCREAMING_SNAKE_CASE__,						SCREAMING_SNAKE_CASE__: Any=   count_inversions_recursive(snake_case_   )

 assert num_inversions_bf == num_inversions_recursive == 0
 print('''number of inversions = '''							,					snake_case_   )


if __name__ == "__main__":
      main()








"""simple docstring"""




class 			__UpperCamelCase							:  # Public class to implement a graph






							def __init__(  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					None:
								a :						int      =							row
								a :						Tuple      =							col
								a :						Optional[int]      =							graph






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					bool:
								return (
								    0 <= i < self.ROW
								    and 0 <= j < self.COL
								    and not visited[i][j]
								    and self.graph[i][j]
								)






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					None:
								# Checking all 8 elements surrounding nth element
								a :						Optional[Any]      =							[-1, -1, -1, 0, 0, 1, 1, 1]  # Coordinate order
								a :						str      =							[-1, 0, 1, -1, 1, -1, 0, 1]
								a :						str      =							True  # Make those cells visited
								for k in range(8 ):
									if self.is_safe(i + row_nbr[k] ,   j + col_nbr[k] ,   lowerCAmelCase__ ):
										self.diffs(i + row_nbr[k] ,   j + col_nbr[k] ,   lowerCAmelCase__ )






							def        __a  (  self )      ->					int:  # And finally, count all islands.
								a :						Union[str, Any]      =							[[False for j in range(self.COL )] for i in range(self.ROW )]
								a :						Dict      =							0
								for i in range(self.ROW ):
									for j in range(self.COL ):
										if visited[i][j] is False and self.graph[i][j] == 1:
											self.diffs(lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ )
											count += 1
								return count


