Binary Indexed Tree : Range Updates and Point Queries | Updates such that getElement ( ) gets an increased value when queried from l to r . ; Get the element indexed at i ; To get ith element sum of all the elements from 0 to i need to be computed ; Driver Code ; Find the element at Index 4 ; Find the element at Index 3
def update ( arr , l , r , val ) : NEW_LINE INDENT arr [ l ] += val NEW_LINE if r + 1 < len ( arr ) : NEW_LINE INDENT arr [ r + 1 ] -= val NEW_LINE DEDENT DEDENT def getElement ( arr , i ) : NEW_LINE INDENT res = 0 NEW_LINE for j in range ( i + 1 ) : NEW_LINE INDENT res += arr [ j ] NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 0 , 0 , 0 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE l = 2 NEW_LINE r = 4 NEW_LINE val = 2 NEW_LINE update ( arr , l , r , val ) NEW_LINE index = 4 NEW_LINE print ( " Element ▁ at ▁ index " , index , " is " , getElement ( arr , index ) ) NEW_LINE l = 0 NEW_LINE r = 3 NEW_LINE val = 4 NEW_LINE update ( arr , l , r , val ) NEW_LINE index = 3 NEW_LINE print ( " Element ▁ at ▁ index " , index , " is " , getElement ( arr , index ) ) NEW_LINE DEDENT