from __future__ import annotations






def 					SCREAMING_SNAKE_CASE_			(		lowerCAmelCase_: list ,	lowerCAmelCase_: list     ):
      snake_case_     :			List[Any]       =       [None] * (len(lowerCAmelCase_     ) + len(lowerCAmelCase_     ))

      snake_case_     :			Any       =       0  # pointer to current index for left Half
      snake_case_     :			Dict       =       0  # pointer to current index for the right Half
      snake_case_     :			int       =       0  # pointer to current index for the sorted array Half

      while pointera < len(lowerCAmelCase_     ) and pointera < len(lowerCAmelCase_     ):
            if left_half[pointera] < right_half[pointera]:
                  snake_case_     :			Tuple       =       left_half[pointera]
                  pointera += 1
                  index += 1
            else:
                  snake_case_     :			Optional[Any]       =       right_half[pointera]
                  pointera += 1
                  index += 1
      while pointera < len(lowerCAmelCase_     ):
            snake_case_     :			int       =       left_half[pointera]
            pointera += 1
            index += 1

      while pointera < len(lowerCAmelCase_     ):
            snake_case_     :			int       =       right_half[pointera]
            pointera += 1
            index += 1

      return sorted_array






def 					SCREAMING_SNAKE_CASE_			(		lowerCAmelCase_: list     ):
      if len(lowerCAmelCase_     ) <= 1:
            return array
      # the actual formula to calculate the middle element = left + (right - left) // 2
      # this avoids integer overflow in case of large N
      snake_case_     :			Any       =       0 + (len(lowerCAmelCase_     ) - 0) // 2

      # Split the array into halves till the array length becomes equal to One
      # merge the arrays of single length returned by mergeSort function and
      # pass them into the merge arrays function which merges the array
      snake_case_     :			List[str]       =       array[:middle]
      snake_case_     :			Any       =       array[middle:]

      return merge(merge_sort(lowerCAmelCase_     ) ,	merge_sort(lowerCAmelCase_     )     )


if __name__ == "__main__":
     import doctest

     doctest.testmod()




import math



class 					snake_case__       :



   def __init__(				self   :   int			,	A__   :   str   )							->     int:




         '''simple docstring'''




         snake_case_     :			Tuple       =       len(A__   )
         snake_case_     :			Union[str, Any]       =       [0] * (
             4 * self.N
         )  # approximate the overall size of segment tree with array N
         self.build(1			,	0			,	self.N - 1   )



   def 					UpperCAmelCase__       (				self   :   Optional[Any]			,	A__   :   Tuple   )							->     Optional[int]:




         '''simple docstring'''




         return idx * 2



   def 					UpperCAmelCase__       (				self   :   Union[str, Any]			,	A__   :   List[str]   )							->     Dict:




         '''simple docstring'''




         return idx * 2 + 1



   def 					UpperCAmelCase__       (				self   :   str			,	A__   :   Any			,	A__   :   Optional[int]			,	A__   :   Tuple   )							->     List[str]:  # noqa: E741




         '''simple docstring'''




         if l == r:
               snake_case_     :			str       =       A[l]
         else:
               snake_case_     :			List[str]       =       (l + r) // 2
               self.build(self.left(A__   )			,	A__			,	A__   )
               self.build(self.right(A__   )			,	mid + 1			,	A__   )
               snake_case_     :			Union[str, Any]       =       max(self.st[self.left(A__   )]			,	self.st[self.right(A__   )]   )



   def 					UpperCAmelCase__       (				self   :   Optional[Any]			,	A__   :   List[str]			,	A__   :   Union[str, Any]			,	A__   :   str   )							->     int:




         '''simple docstring'''




         return self.update_recursive(1			,	0			,	self.N - 1			,	a - 1			,	b - 1			,	A__   )



   def 					UpperCAmelCase__       (				self   :   Optional[int]			,	A__   :   Optional[Any]			,	A__   :   List[str]			,	A__   :   Optional[Any]			,	A__   :   Union[str, Any]			,	A__   :   List[Any]			,	A__   :   str   )							->     Any:  # noqa: E741




         '''simple docstring'''




         if r < a or l > b:
               return True
         if l == r:
               snake_case_     :			List[str]       =       val
               return True
         snake_case_     :			Dict       =       (l + r) // 2
         self.update_recursive(self.left(A__   )			,	A__			,	A__			,	A__			,	A__			,	A__   )
         self.update_recursive(self.right(A__   )			,	mid + 1			,	A__			,	A__			,	A__			,	A__   )
         snake_case_     :			Tuple       =       max(self.st[self.left(A__   )]			,	self.st[self.right(A__   )]   )
         return True



   def 					UpperCAmelCase__       (				self   :   Optional[int]			,	A__   :   List[Any]			,	A__   :   Optional[Any]   )							->     str:




         '''simple docstring'''




         return self.query_recursive(1			,	0			,	self.N - 1			,	a - 1			,	b - 1   )



   def 					UpperCAmelCase__       (				self   :   Union[str, Any]			,	A__   :   Any			,	A__   :   int			,	A__   :   Tuple			,	A__   :   Tuple			,	A__   :   Dict   )							->     str:  # noqa: E741




         '''simple docstring'''




         if r < a or l > b:
               return -math.inf
         if l >= a and r <= b:
               return self.st[idx]
         snake_case_     :			Dict       =       (l + r) // 2
         snake_case_     :			Optional[Any]       =       self.query_recursive(self.left(A__   )			,	A__			,	A__			,	A__			,	A__   )
         snake_case_     :			Any       =       self.query_recursive(self.right(A__   )			,	mid + 1			,	A__			,	A__			,	A__   )
         return max(A__			,	A__   )



   def 					UpperCAmelCase__       (				self   :   Dict   )							->     Optional[Any]:




         '''simple docstring'''




         snake_case_     :			int       =       []
         for i in range(1			,	N + 1   ):
               show_list += [self.query(A__			,	A__   )]
         print(A__   )


if __name__ == "__main__":
     UpperCAmelCase							=       [1, 2, -4, 7, 3, -5, 6, 1_1, -2_0, 9, 1_4, 1_5, 5, 2, -8]
     UpperCAmelCase							=       1_5
     UpperCAmelCase							=       SegmentTree(A)
     print(segt.query(4, 6))
     print(segt.query(7, 1_1))
     print(segt.query(7, 1_2))
     segt.update(1, 3, 1_1_1)
     print(segt.query(1, 1_5))
     segt.update(7, 8, 2_3_5)
     segt.show_data()



