Reputation: 67
We have an array arr[0 . . . n-1]
. We should be able to
arr[i] = x
where 0 <= i <= n-1.This can be solved using segment tree efficiently .
But how to solve opposite to this i.e.
array arr[i] = x
where 0 <= i <= n-1.How to solve above question efficiently like segment tree?
Upvotes: 0
Views: 1288
Reputation: 11294
Assuming you can calculate sum(L,R) easily with segment tree.
First, calculate the total sum of whole array, called it total
.
For a change in the arr
at position ith
Update the segment tree as usual.
Update total = total - oldValue + newValue
.
For each query , print total - sum(L,R)
Note: we can use binary indexed tree (a.k.a Fenwick tree) for this problem too, which IMO, is more suitable.
Upvotes: 1