Range update in Binary Indexed Tree

371 Views Asked by At

How can I use Binary Indexed Tree for range update such that each element A[k] in a range say [i..j] is updated to A[k]*c where c is some constant.

And I need to do point queries after such update operations.

I tried with the function below but it wasn't working, here n is size of array,c is the constant I want to multiply each element of range with.

def updateM(x, c, n):
while x <= n:
    BIT[x] *= c
    x += (x & -x)

and these are my calls to update the range:

updateM(i, c, n)
updateM(j+1, -c, n)

Any kind of help would be appreciated. :)

2

There are 2 best solutions below

0
On BEST ANSWER

The multiplicative inverse of c is not -c but 1/c. Also, I don't understand, what you're trying to accomplish by x += (x & -x).

0
On

It's simple. You just have to run a loop.

But this method is over kill.

It would be best to use segment tree and update the appropriate range, not just a single value.