






from collections.abc import Sequence
from queue import Queue




class 							_lowerCAmelCase:





       """simple docstring"""







       def __init__(   self    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase=None    ,     _lowerCamelCase=None				):
         UpperCamelCase_:   Tuple        =							start
         UpperCamelCase_:   Tuple        =							end
         UpperCamelCase_:   Optional[Any]        =							val
         UpperCamelCase_:   List[str]        =							(start + end) // 2
         UpperCamelCase_:   List[str]        =							left
         UpperCamelCase_:   List[Any]        =							right





       def __repr__(   self				):
         return f'''SegmentTreeNode(start={self.start}, end={self.end}, val={self.val})'''







class 							_lowerCAmelCase:





       """simple docstring"""







       def __init__(   self    ,     _lowerCamelCase    ,     _lowerCamelCase				):
         UpperCamelCase_:   str        =							collection
         UpperCamelCase_:   Optional[Any]        =							function
         if self.collection:
           UpperCamelCase_:   str        =							self._build_tree(0    ,     len(_lowerCamelCase				) - 1				)





       def      _a       (   self    ,     _lowerCamelCase    ,     _lowerCamelCase				):
         self._update_tree(self.root    ,     _lowerCamelCase    ,     _lowerCamelCase				)





       def      _a       (   self    ,     _lowerCamelCase    ,     _lowerCamelCase				):
         return self._query_range(self.root    ,     _lowerCamelCase    ,     _lowerCamelCase				)





       def      _a       (   self    ,     _lowerCamelCase    ,     _lowerCamelCase				):
         if start == end:
           return SegmentTreeNode(_lowerCamelCase    ,     _lowerCamelCase    ,     self.collection[start]				)
         UpperCamelCase_:   int        =							(start + end) // 2
         UpperCamelCase_:   List[str]        =							self._build_tree(_lowerCamelCase    ,     _lowerCamelCase				)
         UpperCamelCase_:   Any        =							self._build_tree(mid + 1    ,     _lowerCamelCase				)
         return SegmentTreeNode(_lowerCamelCase    ,     _lowerCamelCase    ,     self.fn(left.val    ,     right.val				)    ,     _lowerCamelCase    ,     _lowerCamelCase				)





       def      _a       (   self    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase				):
         if node.start == i and node.end == i:
           UpperCamelCase_:   List[Any]        =							val
           return
         if i <= node.mid:
           self._update_tree(node.left    ,     _lowerCamelCase    ,     _lowerCamelCase				)
         else:
           self._update_tree(node.right    ,     _lowerCamelCase    ,     _lowerCamelCase				)
         UpperCamelCase_:   Any        =							self.fn(node.left.val    ,     node.right.val				)





       def      _a       (   self    ,     _lowerCamelCase    ,     _lowerCamelCase    ,     _lowerCamelCase				):
         if node.start == i and node.end == j:
           return node.val

         if i <= node.mid:
           if j <= node.mid:
             # range in left child tree
             return self._query_range(node.left    ,     _lowerCamelCase    ,     _lowerCamelCase				)
           else:
             # range in left child tree and right child tree
             return self.fn(
                 self._query_range(node.left    ,     _lowerCamelCase    ,     node.mid				)    ,     self._query_range(node.right    ,     node.mid + 1    ,     _lowerCamelCase				)    ,     )
         else:
           # range in right child tree
           return self._query_range(node.right    ,     _lowerCamelCase    ,     _lowerCamelCase				)





       def      _a       (   self				):
         if self.root is not None:
           UpperCamelCase_:   Union[str, Any]        =							Queue()
           queue.put(self.root				)
           while not queue.empty():
             UpperCamelCase_:   List[str]        =							queue.get()
             yield node

             if node.left is not None:
               queue.put(node.left				)

             if node.right is not None:
               queue.put(node.right				)


if __name__ == "__main__":
   import operator

   for fn in [operator.add, max, min]:
      print('*' * 50)
      A_						:						List[str]						      =					SegmentTree([2, 1, 5, 3, 4], fn)
      for node in arr.traverse():
         print(node)
      print()

      arr.update(1, 5)
      for node in arr.traverse():
         print(node)
      print()

      print(arr.query_range(3, 4))  # 7
      print(arr.query_range(2, 2))  # 5
      print(arr.query_range(1, 3))  # 13
      print()



from ..utils import DummyObject, requires_backends






class    _UpperCamelCase     (       metaclass=__snake_case					):

				"""simple docstring"""







				lowerCAmelCase					=			['note_seq']

				def __init__(    self					, *a__					, **a__ )						->					Optional[int]:
					requires_backends(self					, ["""note_seq"""] )

				@classmethod
				def   _UpperCAmelCase       (    cls					, *a__					, **a__ )						->					Dict:
					requires_backends(cls					, ["""note_seq"""] )

				@classmethod
				def   _UpperCAmelCase       (    cls					, *a__					, **a__ )						->					Optional[int]:
					requires_backends(cls					, ["""note_seq"""] )



