Queries for count of array elements with values in given range with updates | Python3 code for queries for number of elements that lie in range [ l , r ] ( with updates ) ; Function to set arr [ index ] = x ; Function to get count of elements that lie in range [ l , r ] ; Traverse array ; If element lies in the range [ L , R ] ; Increase count ; Function to solve each query ; Driver Code
from typing import Generic , List , TypeVar NEW_LINE T = TypeVar ( ' T ' ) NEW_LINE V = TypeVar ( ' V ' ) NEW_LINE class Pair ( Generic [ V , T ] ) : NEW_LINE INDENT def __init__ ( self , first : V , second : T ) -> None : NEW_LINE INDENT self . first = first NEW_LINE self . second = second NEW_LINE DEDENT DEDENT def setElement ( arr : List [ int ] , n : int , index : int , x : int ) -> None : NEW_LINE INDENT arr [ index ] = x NEW_LINE DEDENT def getCount ( arr : List [ int ] , n : int , l : int , r : int ) -> int : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] >= l and arr [ i ] <= r ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT def SolveQuery ( arr : List [ int ] , n : int , Q : List [ Pair [ int , Pair [ int , int ] ] ] ) : NEW_LINE INDENT x = 0 NEW_LINE for i in range ( len ( Q ) ) : NEW_LINE INDENT if ( Q [ i ] . first == 1 ) : NEW_LINE INDENT x = getCount ( arr , n , Q [ i ] . second . first , Q [ i ] . second . second ) NEW_LINE print ( x , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT setElement ( arr , n , Q [ i ] . second . first , Q [ i ] . second . second ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 3 , 4 , 4 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE Q = [ Pair ( 1 , Pair ( 3 , 5 ) ) , Pair ( 1 , Pair ( 2 , 4 ) ) , Pair ( 1 , Pair ( 1 , 2 ) ) , Pair ( 2 , Pair ( 1 , 7 ) ) , Pair ( 1 , Pair ( 1 , 2 ) ) ] NEW_LINE SolveQuery ( arr , n , Q ) NEW_LINE DEDENT