
from collections.abc import Sequence
from queue import Queue

class __snake_case		:
    def __init__(  self		,snake_case		,snake_case		,snake_case		,snake_case=None		,snake_case=None				):



           '''simple docstring'''







           lowercase							:					Any	     =				start
           lowercase							:					Optional[int]	     =				end
           lowercase							:					str	     =				val
           lowercase							:					Optional[Any]	     =				(start + end) // 2
           lowercase							:					Optional[int]	     =				left
           lowercase							:					Optional[Any]	     =				right
    def __repr__(  self				):



           '''simple docstring'''







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






class __snake_case		:
    def __init__(  self		,snake_case		,snake_case				):



           '''simple docstring'''







           lowercase							:					Optional[Any]	     =				collection
           lowercase							:					Any	     =				function
           if self.collection:
                  lowercase							:					Dict	     =				self._build_tree(0		,len(snake_case				) - 1				)
    def 				_SCREAMING_SNAKE_CASE	(  self		,snake_case		,snake_case				):



           '''simple docstring'''







           self._update_tree(self.root		,snake_case		,snake_case				)
    def 				_SCREAMING_SNAKE_CASE	(  self		,snake_case		,snake_case				):



           '''simple docstring'''







           return self._query_range(self.root		,snake_case		,snake_case				)
    def 				_SCREAMING_SNAKE_CASE	(  self		,snake_case		,snake_case				):



           '''simple docstring'''







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



           '''simple docstring'''







           if node.start == i and node.end == i:
                  lowercase							:					int	     =				val
                  return
           if i <= node.mid:
                  self._update_tree(node.left		,snake_case		,snake_case				)
           else:
                  self._update_tree(node.right		,snake_case		,snake_case				)
           lowercase							:					Any	     =				self.fn(node.left.val		,node.right.val				)
    def 				_SCREAMING_SNAKE_CASE	(  self		,snake_case		,snake_case		,snake_case				):



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



           '''simple docstring'''







           if self.root is not None:
                  lowercase							:					Any	     =				Queue()
                  queue.put(self.root				)
                  while not queue.empty():
                         lowercase							:					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)
    lowercase  : Union[str, Any]						   =							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 __future__ import annotations

from itertools import permutations
from random import randint
from timeit import repeat







def 						_snake_case(  )			->   tuple[list[int], int]:
       lowercase							:					List[Any]	     =				[randint(-1_000 ,     1_000    ) for i in range(10    )]
       lowercase							:					Tuple	     =				randint(-5_000 ,     5_000    )
       return (arr, r)


lowercase  : List[Any]						   =							make_dataset()







def 						_snake_case(  SCREAMING_SNAKE_CASE__ ,     SCREAMING_SNAKE_CASE__    )			->   tuple[int, ...]:
       for triplet in permutations(SCREAMING_SNAKE_CASE__ ,     3    ):
              if sum(SCREAMING_SNAKE_CASE__    ) == target:
                     return tuple(sorted(SCREAMING_SNAKE_CASE__    )    )
       return (0, 0, 0)







def 						_snake_case(  SCREAMING_SNAKE_CASE__ ,     SCREAMING_SNAKE_CASE__    )			->   tuple[int, int, int]:
       arr.sort()
       lowercase							:					Optional[int]	     =				len(SCREAMING_SNAKE_CASE__    )
       for i in range(n - 1    ):
              lowercase     ,      lowercase							:					Optional[Any]	     =				i + 1, n - 1
              while left < right:
                     if arr[i] + arr[left] + arr[right] == target:
                            return (arr[i], arr[left], arr[right])
                     elif arr[i] + arr[left] + arr[right] < target:
                            left += 1
                     elif arr[i] + arr[left] + arr[right] > target:
                            right -= 1
       return (0, 0, 0)







def 						_snake_case(  )			->   tuple[float, float]:
       lowercase							:					Dict	     =				"""
from __main__ import dataset, triplet_sum1, triplet_sum2
"""
       lowercase							:					Tuple	     =				"""
triplet_sum1(*dataset)
"""
       lowercase							:					int	     =				"""
triplet_sum2(*dataset)
"""
       lowercase							:					str	     =				repeat(setup=SCREAMING_SNAKE_CASE__ ,     stmt=SCREAMING_SNAKE_CASE__ ,     repeat=5 ,     number=10_000    )
       lowercase							:					Dict	     =				repeat(setup=SCREAMING_SNAKE_CASE__ ,     stmt=SCREAMING_SNAKE_CASE__ ,     repeat=5 ,     number=10_000    )
       return (min(SCREAMING_SNAKE_CASE__    ), min(SCREAMING_SNAKE_CASE__    ))


if __name__ == "__main__":
  from doctest import testmod

  testmod()
  lowercase  : Union[str, Any]						   =							solution_times()
  print(F'''The time for naive implementation is {times[0]}.''')
  print(F'''The time for optimized implementation is {times[1]}.''')
