Number of primes in a subarray ( with updates ) | Python3 program to find number of prime numbers in a subarray and performing updates ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; A utility function to get the middle index from corner indexes . ; A recursive function to get the number of primes in a given range of array indexes . The following are parameters for this function . st -- > Pointer to segment tree index -- > Index of current node in the segment tree . Initially 0 is passed as root is always at index 0 ss & se -- > Starting and ending indexes of the segment represented by current node , i . e . , st [ index ] qs & qe -- > Starting and ending indexes of query range ; If segment of this node is a part of given range , then return the number of primes in the segment ; If segment of this node is outside the given range ; If a part of this segment overlaps with the given range ; A recursive function to update the nodes which have the given index in their range . The following are parameters st , si , ss and se are same as getSumUtil ( ) i -- > index of the element to be updated . This index is in input array . diff -- > Value to be added to all nodes which have i in range ; Base Case : If the input index lies outside the range of this segment ; If the input index is in range of this node , then update the value of the node and its children ; The function to update a value in input array and segment tree . It uses updateValueUtil ( ) to update the value in segment tree ; Check for erroneous input index ; Update the value in array ; Case 1 : Old and new values both are primes ; Case 2 : Old and new values both non primes ; Case 3 : Old value was prime , new value is non prime ; Case 4 : Old value was non prime , new_val is prime ; Update the values of nodes in segment tree ; Return number of primes in range from index qs ( query start ) to qe ( query end ) . It mainly uses queryPrimesUtil ( ) ; A recursive function that constructs Segment Tree for array [ ss . . se ] . si is index of current node in segment tree st ; If there is one element in array , check if it is prime then store 1 in the segment tree else store 0 and return ; if arr [ ss ] is prime ; If there are more than one elements , then recur for left and right subtrees and store the sum of the two values in this node ; Function to construct segment tree from given array . This function allocates memory for segment tree and calls constructSTUtil ( ) to fill the allocated memory ; Height of segment tree ; Maximum size of segment tree ; Fill the allocated memory st ; Return the constructed segment tree ; Driver code ; Preprocess all primes till MAX . Create a boolean array " isPrime [ 0 . . MAX ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Build segment tree from given array ; Query 1 : Query ( start = 0 , end = 4 ) ; Query 2 : Update ( i = 3 , x = 6 ) , i . e Update a [ i ] to x ; Query 3 : Query ( start = 0 , end = 4 )
from math import ceil , floor , log NEW_LINE MAX = 1000 NEW_LINE def sieveOfEratosthenes ( isPrime ) : NEW_LINE INDENT isPrime [ 1 ] = False NEW_LINE for p in range ( 2 , MAX + 1 ) : NEW_LINE INDENT if p * p > MAX : NEW_LINE INDENT break NEW_LINE DEDENT if ( isPrime [ p ] == True ) : NEW_LINE INDENT for i in range ( 2 * p , MAX + 1 , p ) : NEW_LINE INDENT isPrime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def getMid ( s , e ) : NEW_LINE INDENT return s + ( e - s ) // 2 NEW_LINE DEDENT def queryPrimesUtil ( st , ss , se , qs , qe , index ) : NEW_LINE INDENT if ( qs <= ss and qe >= se ) : NEW_LINE INDENT return st [ index ] NEW_LINE DEDENT if ( se < qs or ss > qe ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT mid = getMid ( ss , se ) NEW_LINE return queryPrimesUtil ( st , ss , mid , qs , qe , 2 * index + 1 ) + queryPrimesUtil ( st , mid + 1 , se , qs , qe , 2 * index + 2 ) NEW_LINE DEDENT def updateValueUtil ( st , ss , se , i , diff , si ) : NEW_LINE INDENT if ( i < ss or i > se ) : NEW_LINE INDENT return NEW_LINE DEDENT st [ si ] = st [ si ] + diff NEW_LINE if ( se != ss ) : NEW_LINE INDENT mid = getMid ( ss , se ) NEW_LINE updateValueUtil ( st , ss , mid , i , diff , 2 * si + 1 ) NEW_LINE updateValueUtil ( st , mid + 1 , se , i , diff , 2 * si + 2 ) NEW_LINE DEDENT DEDENT def updateValue ( arr , st , n , i , new_val , isPrime ) : NEW_LINE INDENT if ( i < 0 or i > n - 1 ) : NEW_LINE INDENT printf ( " Invalid ▁ Input " ) NEW_LINE return NEW_LINE DEDENT diff , oldValue = 0 , 0 NEW_LINE oldValue = arr [ i ] NEW_LINE arr [ i ] = new_val NEW_LINE if ( isPrime [ oldValue ] and isPrime [ new_val ] ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( ( not isPrime [ oldValue ] ) and ( not isPrime [ new_val ] ) ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( isPrime [ oldValue ] and not isPrime [ new_val ] ) : NEW_LINE INDENT diff = - 1 NEW_LINE DEDENT if ( not isPrime [ oldValue ] and isPrime [ new_val ] ) : NEW_LINE INDENT diff = 1 NEW_LINE DEDENT updateValueUtil ( st , 0 , n - 1 , i , diff , 0 ) NEW_LINE DEDENT def queryPrimes ( st , n , qs , qe ) : NEW_LINE INDENT primesInRange = queryPrimesUtil ( st , 0 , n - 1 , qs , qe , 0 ) NEW_LINE print ( " Number ▁ of ▁ Primes ▁ in ▁ subarray ▁ from ▁ " , qs , " ▁ to ▁ " , qe , " ▁ = ▁ " , primesInRange ) NEW_LINE DEDENT def constructSTUtil ( arr , ss , se , st , si , isPrime ) : NEW_LINE INDENT if ( ss == se ) : NEW_LINE INDENT if ( isPrime [ arr [ ss ] ] ) : NEW_LINE INDENT st [ si ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT st [ si ] = 0 NEW_LINE DEDENT return st [ si ] NEW_LINE DEDENT mid = getMid ( ss , se ) NEW_LINE st [ si ] = constructSTUtil ( arr , ss , mid , st , si * 2 + 1 , isPrime ) + constructSTUtil ( arr , mid + 1 , se , st , si * 2 + 2 , isPrime ) NEW_LINE return st [ si ] NEW_LINE DEDENT def constructST ( arr , n , isPrime ) : NEW_LINE INDENT x = ceil ( log ( n , 2 ) ) NEW_LINE max_size = 2 * pow ( 2 , x ) - 1 NEW_LINE st = [ 0 ] * ( max_size ) NEW_LINE constructSTUtil ( arr , 0 , n - 1 , st , 0 , isPrime ) NEW_LINE return st NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 5 , 7 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE isPrime = [ True ] * ( MAX + 1 ) NEW_LINE sieveOfEratosthenes ( isPrime ) NEW_LINE st = constructST ( arr , n , isPrime ) NEW_LINE start = 0 NEW_LINE end = 4 NEW_LINE queryPrimes ( st , n , start , end ) NEW_LINE i = 3 NEW_LINE x = 6 NEW_LINE updateValue ( arr , st , n , i , x , isPrime ) NEW_LINE start = 0 NEW_LINE end = 4 NEW_LINE queryPrimes ( st , n , start , end ) NEW_LINE DEDENT