1

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. :)

Mooncrater
  • 4,146
  • 4
  • 33
  • 62

2 Answers2

1

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).

Andy Clifton
  • 4,926
  • 3
  • 35
  • 47
Kai
  • 112
  • 1
  • 1
0

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.

danny
  • 5,140
  • 1
  • 19
  • 31