from collections.abc import Sequence
from queue import Queue
class     a_			:






 '''simple docstring'''






 def __init__(      self :  int      ,   lowercase__ :  Dict      ,   lowercase__ :  List[Any]      ,   lowercase__ :  List[str]      ,   lowercase__ :  int=None      ,   lowercase__ :  List[str]=None):







        '''simple docstring'''



        lowerCAmelCase__					     = start
        lowerCAmelCase__					     = end
        lowerCAmelCase__					     = val
        lowerCAmelCase__					     = (start + end) // 2
        lowerCAmelCase__					     = left
        lowerCAmelCase__					     = right




 def __repr__(      self :  Optional[int]):







        '''simple docstring'''



        return F"""SegmentTreeNode(start={self.start}, end={self.end}, val={self.val})"""






class     a_			:






 '''simple docstring'''






 def __init__(      self :  Tuple      ,   lowercase__ :  Sequence      ,   lowercase__ :  Optional[int]):







        '''simple docstring'''



        lowerCAmelCase__					     = collection
        lowerCAmelCase__					     = function
        if self.collection:
               lowerCAmelCase__					     = self._build_tree(0      ,   len(lowercase__) - 1)




 def 		__snake_case						(      self :  Any      ,   lowercase__ :  Optional[int]      ,   lowercase__ :  List[str]):







        '''simple docstring'''



        self._update_tree(self.root      ,   lowercase__      ,   lowercase__)




 def 		__snake_case						(      self :  List[str]      ,   lowercase__ :  int      ,   lowercase__ :  Optional[int]):







        '''simple docstring'''



        return self._query_range(self.root      ,   lowercase__      ,   lowercase__)




 def 		__snake_case						(      self :  str      ,   lowercase__ :  str      ,   lowercase__ :  Tuple):







        '''simple docstring'''



        if start == end:
               return SegmentTreeNode(lowercase__      ,   lowercase__      ,   self.collection[start])
        lowerCAmelCase__					     = (start + end) // 2
        lowerCAmelCase__					     = self._build_tree(lowercase__      ,   lowercase__)
        lowerCAmelCase__					     = self._build_tree(mid + 1      ,   lowercase__)
        return SegmentTreeNode(lowercase__      ,   lowercase__      ,   self.fn(left.val      ,   right.val)      ,   lowercase__      ,   lowercase__)




 def 		__snake_case						(      self :  Dict      ,   lowercase__ :  Dict      ,   lowercase__ :  str      ,   lowercase__ :  Union[str, Any]):







        '''simple docstring'''



        if node.start == i and node.end == i:
               lowerCAmelCase__					     = val
               return
        if i <= node.mid:
               self._update_tree(node.left      ,   lowercase__      ,   lowercase__)
        else:
               self._update_tree(node.right      ,   lowercase__      ,   lowercase__)
        lowerCAmelCase__					     = self.fn(node.left.val      ,   node.right.val)




 def 		__snake_case						(      self :  Optional[int]      ,   lowercase__ :  List[Any]      ,   lowercase__ :  List[str]      ,   lowercase__ :  List[str]):







        '''simple docstring'''



        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      ,   lowercase__      ,   lowercase__)
               else:
                      # range in left child tree and right child tree
                      return self.fn(
                          self._query_range(node.left      ,   lowercase__      ,   node.mid)      ,   self._query_range(node.right      ,   node.mid + 1      ,   lowercase__)      ,   )
        else:
               # range in right child tree
               return self._query_range(node.right      ,   lowercase__      ,   lowercase__)




 def 		__snake_case						(      self :  List[Any]):







        '''simple docstring'''



        if self.root is not None:
               lowerCAmelCase__					     = Queue()
               queue.put(self.root)
               while not queue.empty():
                      lowerCAmelCase__					     = 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)
        lowerCAmelCase__     						=			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()

import copy
from dataclasses import dataclass
from pathlib import Path
from typing import Dict, Optional, Union
@dataclass
class     a_			:






 '''simple docstring'''






 UpperCAmelCase_     			= None
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= None
 UpperCAmelCase_     			= None
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= True
 UpperCAmelCase_     			= None
 UpperCAmelCase_     			= 1
 UpperCAmelCase_     			= None
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= None
 UpperCAmelCase_     			= None




 def 		__snake_case						(      self :  Dict):







        '''simple docstring'''



        return self.__class__(**{k: copy.deepcopy(lowercase__) for k, v in self.__dict__.items()})
