human
Find sum of product of every number and its frequency in given range | Function to solve queries.
Calculating answer for every query.
The end points of the ith query.
Map for storing frequency.
Incrementing the frequency.
Iterating over map to find answer.
Adding the contribution of ith number.
Print answer.
Driver code"
How can the above be solved in Python?
 Give me a code to find sum of product of every number and its frequency in given range and the other steps required?
assistant
#Here is code to find sum of product of every number and its frequency in given range and the other steps required.
 def answerQueries ( arr , n , queries ) :
	for i in range ( len ( queries ) ) :
		ans = 0
		l = queries [ i ] [ 0 ] - 1
		r = queries [ i ] [ 1 ] - 1
		freq = dict ( )
		for j in range ( l , r + 1 ) :
			freq [ arr [ j ] ] = freq . get ( arr [ j ] , 0 ) + 1
		for i in freq :
			ans += ( i * freq [ i ] )
		print ( ans )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 1 , 2 , 1 ]
	n = len ( arr )
	queries = [ [ 1 , 2 ] , [ 1 , 3 ] ]
	answerQueries ( arr , n , queries )