Consider this question. In this segment tree solution, we are updating all nodes of the tree in the given range. Is it possible to apply lazy propagation to this problem?
Edit: Consider that in every update operation arr[i] = (1-(1-arr[i])*a)
, where L<=i<=R
and a
is a constant.
I'll assume your query operation is finding the sum in a range
[L, R]
.You certainly want to do this efficiently, probably in
O(log n)
per operation.You need a way to store data in the
lazy
field that allows you to compute the updates when traversing the tree for a query.Let's see if we can write the update in a nicer way:
If we do this twice:
Which is equivalent to (applied to an entire range):
a
a
1
When updating the lazy field, it's clear that you just increase the exponent of
a
.You can do all of these 3 lazily as described in the lazy propagation article you link to.
So your update operation can be split into 3 lazy updates, each done in
O(log n)
time, for a total time ofO(log n)
.