Find the element having maximum set bits in the given range for Q queries | Structure to store two values in one node ; Function that returns the count of set bits in a number ; Parity will store the count of set bits ; Function to build the segment tree ; Condition to check if there is only one element in the array ; If there are more than one elements , then recur for left and right subtrees ; Condition to check the maximum set bits is greater in two subtrees ; Condition when maximum set bits are equal in both subtrees ; Function to do the range query in the segment tree ; If segment of this node is outside the given range , then return the minimum value . ; If segment of this node is a part of given range , then return the node of the segment ; If left segment of this node falls out of range , then recur in the right side of the tree ; If right segment of this node falls out of range , then recur in the left side of the tree ; If a part of this segment overlaps with the given range ; Returns the value ; Driver code ; Calculates the length of array ; Build Segment Tree ; Find the max set bits value between 1 st and 4 th index of array ; Find the max set bits value between 0 th and 2 nd index of array
from typing import List NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self ) -> None : NEW_LINE INDENT self . value = 0 NEW_LINE self . max_set_bits = 0 NEW_LINE DEDENT DEDENT tree = [ Node ( ) for _ in range ( 4 * 10000 ) ] NEW_LINE def setBits ( x : int ) -> int : NEW_LINE INDENT parity = 0 NEW_LINE while ( x != 0 ) : NEW_LINE INDENT if ( x & 1 ) : NEW_LINE INDENT parity += 1 NEW_LINE DEDENT x = x >> 1 NEW_LINE DEDENT return parity NEW_LINE DEDENT def buildSegmentTree ( a : List [ int ] , index : int , beg : int , end : int ) -> None : NEW_LINE INDENT if ( beg == end ) : NEW_LINE INDENT tree [ index ] . value = a [ beg ] NEW_LINE tree [ index ] . max_set_bits = setBits ( a [ beg ] ) NEW_LINE DEDENT else : NEW_LINE INDENT mid = ( beg + end ) // 2 NEW_LINE buildSegmentTree ( a , 2 * index + 1 , beg , mid ) NEW_LINE buildSegmentTree ( a , 2 * index + 2 , mid + 1 , end ) NEW_LINE if ( tree [ 2 * index + 1 ] . max_set_bits > tree [ 2 * index + 2 ] . max_set_bits ) : NEW_LINE INDENT tree [ index ] . max_set_bits = tree [ 2 * index + 1 ] . max_set_bits NEW_LINE tree [ index ] . value = tree [ 2 * index + 1 ] . value NEW_LINE DEDENT elif ( tree [ 2 * index + 2 ] . max_set_bits > tree [ 2 * index + 1 ] . max_set_bits ) : NEW_LINE INDENT tree [ index ] . max_set_bits = tree [ 2 * index + 2 ] . max_set_bits NEW_LINE tree [ index ] . value = tree [ 2 * index + 2 ] . value NEW_LINE DEDENT else : NEW_LINE INDENT tree [ index ] . max_set_bits = tree [ 2 * index + 2 ] . max_set_bits NEW_LINE tree [ index ] . value = max ( tree [ 2 * index + 2 ] . value , tree [ 2 * index + 1 ] . value ) NEW_LINE DEDENT DEDENT DEDENT def query ( index : int , beg : int , end : int , l : int , r : int ) -> Node : NEW_LINE INDENT result = Node ( ) NEW_LINE result . value = result . max_set_bits = - 1 NEW_LINE if ( beg > r or end < l ) : NEW_LINE INDENT return result NEW_LINE DEDENT if ( beg >= l and end <= r ) : NEW_LINE INDENT return tree [ index ] NEW_LINE DEDENT mid = ( beg + end ) // 2 NEW_LINE if ( l > mid ) : NEW_LINE INDENT return query ( 2 * index + 2 , mid + 1 , end , l , r ) NEW_LINE DEDENT if ( r <= mid ) : NEW_LINE INDENT return query ( 2 * index + 1 , beg , mid , l , r ) NEW_LINE DEDENT left = query ( 2 * index + 1 , beg , mid , l , r ) NEW_LINE right = query ( 2 * index + 2 , mid + 1 , end , l , r ) NEW_LINE if ( left . max_set_bits > right . max_set_bits ) : NEW_LINE INDENT result . max_set_bits = left . max_set_bits NEW_LINE result . value = left . value NEW_LINE DEDENT elif ( right . max_set_bits > left . max_set_bits ) : NEW_LINE INDENT result . max_set_bits = right . max_set_bits NEW_LINE result . value = right . value NEW_LINE DEDENT else : NEW_LINE INDENT result . max_set_bits = left . max_set_bits NEW_LINE result . value = max ( right . value , left . value ) NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 18 , 9 , 8 , 15 , 14 , 5 ] NEW_LINE N = len ( a ) NEW_LINE buildSegmentTree ( a , 0 , 0 , N - 1 ) NEW_LINE print ( query ( 0 , 0 , N - 1 , 1 , 4 ) . value ) NEW_LINE print ( query ( 0 , 0 , N - 1 , 0 , 2 ) . value ) NEW_LINE DEDENT