Count of Ks in the Array for a given range of indices after array updates for Q queries | Python3 program for the above approach ; Function to build the segment tree ; Base case ; Since the count of zero is required set leaf node as 1 ; If the value in array is not zero , store 0 in the leaf node ; Find the mid ; Recursive call for left subtree ; Recursive call for right subtree ; Parent nodes contains the count of zero in range tl to tr ; Function to find the count of 0 s in range l to r ; Base Case ; Case when no two segment are combining ; Finding the mid ; When it is required to combine left subtree and right subtree to get the range l to r ; Function that updates the segment tree nodes ; Base Case ; If array element is 0 ; If array element is not 0 ; Otherwise ; Find the mid ; Update the tree or count which is stored in parent node ; Function to solve all the queries ; When query type is 1 ; When query type is 2 ; Driver code
a = [ ] NEW_LINE seg_tree = [ ] NEW_LINE query = [ ] NEW_LINE def build_tree ( v , tl , tr ) : NEW_LINE INDENT global a , seg_tree , query NEW_LINE if ( tl != tr ) : NEW_LINE INDENT if ( a [ tl ] == 0 ) : NEW_LINE INDENT seg_tree [ v ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT seg_tree [ v ] = 0 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT tm = int ( ( tl + tr ) / 2 ) NEW_LINE build_tree ( v * 2 , tl , tm ) NEW_LINE build_tree ( v * 2 + 1 , tm + 1 , tr ) NEW_LINE seg_tree [ v ] = seg_tree [ v * 2 ] + seg_tree [ v * 2 + 1 ] NEW_LINE DEDENT DEDENT def frequency_zero ( v , tl , tr , l , r ) : NEW_LINE INDENT global a , seg_tree , query NEW_LINE if ( l > r ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( l == tl and r == tr ) : NEW_LINE INDENT return seg_tree [ v ] NEW_LINE DEDENT tm = int ( ( tl + tr ) / 2 ) NEW_LINE return frequency_zero ( v * 2 , tl , tm , l , min ( r , tm ) ) + frequency_zero ( v * 2 + 1 , tm + 1 , tr , max ( l , tm + 1 ) , r ) NEW_LINE DEDENT def update ( v , tl , tr , pos , new_val ) : NEW_LINE INDENT global a , seg_tree , query NEW_LINE if ( tl == tr ) : NEW_LINE INDENT if ( new_val == 0 ) : NEW_LINE INDENT seg_tree [ v ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT seg_tree [ v ] = 0 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT tm = int ( ( tl + tr ) / 2 ) NEW_LINE if ( pos <= tm ) : NEW_LINE INDENT update ( v * 2 , tl , tm , pos , new_val ) NEW_LINE DEDENT else : NEW_LINE INDENT update ( v * 2 + 1 , tm + 1 , tr , pos , new_val ) NEW_LINE DEDENT seg_tree [ v ] = seg_tree [ v * 2 ] + seg_tree [ v * 2 + 1 ] NEW_LINE DEDENT DEDENT def solve ( n , q ) : NEW_LINE INDENT global a , seg_tree , query NEW_LINE qu = [ 5 , 3 , 6 ] NEW_LINE seg_tree = [ 0 ] * ( 4 * n + 1 ) NEW_LINE build_tree ( 1 , 0 , n - 1 ) NEW_LINE for i in range ( len ( qu ) ) : NEW_LINE INDENT print ( qu [ i ] ) NEW_LINE DEDENT for i in range ( q , q ) : NEW_LINE INDENT if query [ i - 1 ] [ 0 ] == 1 : NEW_LINE INDENT l = query [ i - 1 ] [ 1 ] NEW_LINE r = query [ i - 1 ] [ 2 ] NEW_LINE print ( frequency_zero ( 1 , 0 , n - 1 , l , r ) ) NEW_LINE DEDENT else : NEW_LINE INDENT a [ query [ i - 1 ] [ 1 ] ] = query [ i - 1 ] [ 2 ] NEW_LINE pos = query [ i - 1 ] [ 1 ] NEW_LINE new_val = query [ i - 1 ] [ 2 ] NEW_LINE update ( 1 , 0 , n - 1 , pos , new_val ) NEW_LINE DEDENT DEDENT DEDENT a = [ 9 , 5 , 7 , 6 , 9 , 0 , 0 , 0 , 0 , 5 , 6 , 7 , 3 , 9 , 0 , 7 , 0 , 9 , 0 ] NEW_LINE Q = 5 NEW_LINE query = [ [ 1 , 5 , 14 ] , [ 2 , 6 , 1 ] , [ 1 , 0 , 8 ] , [ 2 , 13 , 0 ] , [ 1 , 6 , 18 ] ] NEW_LINE N = len ( a ) NEW_LINE solve ( N , Q ) NEW_LINE