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
< ? php function update ( & $ arr , $ l , $ r , $ val ) { $ arr [ $ l ] += $ val ; if ( $ r + 1 < sizeof ( $ arr ) ) $ arr [ $ r + 1 ] -= $ val ; } function getElement ( & $ arr , $ i ) { $ res = 0 ; for ( $ j = 0 ; $ j <= $ i ; $ j ++ ) $ res += $ arr [ $ j ] ; return $ res ; } $ arr = array ( 0 , 0 , 0 , 0 , 0 ) ; $ n = sizeof ( $ arr ) ; $ l = 2 ; $ r = 4 ; $ val = 2 ; update ( $ arr , $ l , $ r , $ val ) ; $ index = 4 ; echo ( " Element ▁ at ▁ index ▁ " . $ index . " ▁ is ▁ " . getElement ( $ arr , $ index ) . " STRNEWLINE " ) ; $ l = 0 ; $ r = 3 ; $ val = 4 ; update ( $ arr , $ l , $ r , $ val ) ; $ index = 3 ; echo ( " Element ▁ at ▁ index ▁ " . $ index . " ▁ is ▁ " . getElement ( $ arr , $ index ) ) ; ? >