


'''simple docstring'''







from __future__ import annotations

import math
class 	UpperCamelCase__					:



     """simple docstring"""






     def __init__( self      ,	snake_case__   ):

            '''simple docstring'''

            _lowerCAmelCase    :							Union[str, Any]   			=       size
            # approximate the overall size of segment tree with given value
            _lowerCAmelCase    :							int   			=       [0 for i in range(0      ,	4 * size   )]
            # create array to store lazy update
            _lowerCAmelCase    :							Dict   			=       [0 for i in range(0      ,	4 * size   )]
            _lowerCAmelCase    :							Optional[Any]   			=       [0 for i in range(0      ,	4 * size   )]  # flag for lazy update







     def a	( self      ,	snake_case__   ):

            '''simple docstring'''

            return idx * 2







     def a	( self      ,	snake_case__   ):

            '''simple docstring'''

            return idx * 2 + 1







     def a	( self      ,	snake_case__      ,	snake_case__      ,	snake_case__      ,	snake_case__   ):

            '''simple docstring'''

            if left_element == right_element:
                   _lowerCAmelCase    :							Optional[int]   			=       a[left_element - 1]
            else:
                   _lowerCAmelCase    :							Tuple   			=       (left_element + right_element) // 2
                   self.build(self.left(snake_case__   )      ,	snake_case__      ,	snake_case__      ,	snake_case__   )
                   self.build(self.right(snake_case__   )      ,	mid + 1      ,	snake_case__      ,	snake_case__   )
                   _lowerCAmelCase    :							List[Any]   			=       max(
                       self.segment_tree[self.left(snake_case__   )]      ,	self.segment_tree[self.right(snake_case__   )]   )







     def a	( self      ,	snake_case__      ,	snake_case__      ,	snake_case__      ,	snake_case__      ,	snake_case__      ,	snake_case__   ):

            '''simple docstring'''

            if self.flag[idx] is True:
                   _lowerCAmelCase    :							List[Any]   			=       self.lazy[idx]
                   _lowerCAmelCase    :							Union[str, Any]   			=       False
                   if left_element != right_element:
                          _lowerCAmelCase    :							Any   			=       self.lazy[idx]
                          _lowerCAmelCase    :							Optional[int]   			=       self.lazy[idx]
                          _lowerCAmelCase    :							Optional[Any]   			=       True
                          _lowerCAmelCase    :							Optional[Any]   			=       True

            if right_element < a or left_element > b:
                   return True
            if left_element >= a and right_element <= b:
                   _lowerCAmelCase    :							List[Any]   			=       val
                   if left_element != right_element:
                          _lowerCAmelCase    :							Dict   			=       val
                          _lowerCAmelCase    :							Union[str, Any]   			=       val
                          _lowerCAmelCase    :							Tuple   			=       True
                          _lowerCAmelCase    :							List[Any]   			=       True
                   return True
            _lowerCAmelCase    :							Optional[Any]   			=       (left_element + right_element) // 2
            self.update(self.left(snake_case__   )      ,	snake_case__      ,	snake_case__      ,	snake_case__      ,	snake_case__      ,	snake_case__   )
            self.update(self.right(snake_case__   )      ,	mid + 1      ,	snake_case__      ,	snake_case__      ,	snake_case__      ,	snake_case__   )
            _lowerCAmelCase    :							Any   			=       max(
                self.segment_tree[self.left(snake_case__   )]      ,	self.segment_tree[self.right(snake_case__   )]   )
            return True







     def a	( self      ,	snake_case__      ,	snake_case__      ,	snake_case__      ,	snake_case__      ,	snake_case__   ):

            '''simple docstring'''

            if self.flag[idx] is True:
                   _lowerCAmelCase    :							Union[str, Any]   			=       self.lazy[idx]
                   _lowerCAmelCase    :							Dict   			=       False
                   if left_element != right_element:
                          _lowerCAmelCase    :							Any   			=       self.lazy[idx]
                          _lowerCAmelCase    :							List[str]   			=       self.lazy[idx]
                          _lowerCAmelCase    :							Union[str, Any]   			=       True
                          _lowerCAmelCase    :							Optional[int]   			=       True
            if right_element < a or left_element > b:
                   return -math.inf
            if left_element >= a and right_element <= b:
                   return self.segment_tree[idx]
            _lowerCAmelCase    :							Optional[int]   			=       (left_element + right_element) // 2
            _lowerCAmelCase    :							Any   			=       self.query(self.left(snake_case__   )      ,	snake_case__      ,	snake_case__      ,	snake_case__      ,	snake_case__   )
            _lowerCAmelCase    :							Tuple   			=       self.query(self.right(snake_case__   )      ,	mid + 1      ,	snake_case__      ,	snake_case__      ,	snake_case__   )
            return max(snake_case__      ,	snake_case__   )







     def __str__( self   ):

            '''simple docstring'''

            return str([self.query(1      ,	1      ,	self.size      ,	snake_case__      ,	snake_case__   ) for i in range(1      ,	self.size + 1   )]   )


if __name__ == "__main__":
       lowerCAmelCase    :     str					=						[1, 2, -4, 7, 3, -5, 6, 11, -20, 9, 14, 15, 5, 2, -8]
       lowerCAmelCase    :     str					=						15
       lowerCAmelCase    :     Union[str, Any]					=						SegmentTree(size)
       segt.build(1, 1, size, A)
       print(segt.query(1, 1, size, 4, 6))
       print(segt.query(1, 1, size, 7, 11))
       print(segt.query(1, 1, size, 7, 12))
       segt.update(1, 1, size, 1, 3, 1_11)
       print(segt.query(1, 1, size, 1, 15))
       segt.update(1, 1, size, 7, 8, 2_35)
       print(segt)





'''simple docstring'''







import unittest

from knapsack import greedy_knapsack as kp
class 	UpperCamelCase__					( unittest.TestCase	):



     """simple docstring"""






     def a	( self   ):

            '''simple docstring'''

            _lowerCAmelCase    :							Union[str, Any]   			=       [10, 20, 30, 40, 50, 60]
            _lowerCAmelCase    :							Union[str, Any]   			=       [2, 4, 6, 8, 10, 12]
            _lowerCAmelCase    :							Dict   			=       100
            self.assertEqual(kp.calc_profit(snake_case__      ,	snake_case__      ,	snake_case__   )      ,	210   )







     def a	( self   ):

            '''simple docstring'''

            self.assertRaisesRegex(snake_case__      ,	'max_weight must greater than zero.'   )







     def a	( self   ):

            '''simple docstring'''

            self.assertRaisesRegex(snake_case__      ,	'Weight can not be negative.'   )







     def a	( self   ):

            '''simple docstring'''

            self.assertRaisesRegex(snake_case__      ,	'Profit can not be negative.'   )







     def a	( self   ):

            '''simple docstring'''

            self.assertRaisesRegex(snake_case__      ,	'max_weight must greater than zero.'   )







     def a	( self   ):

            '''simple docstring'''

            self.assertRaisesRegex(
                snake_case__      ,	'The length of profit and weight must be same.'   )


if __name__ == "__main__":
       unittest.main()

