0

I'd like to add a function to the lazy propagation implementation in the link below that sets a range to 0. There is currently an update_tree function in there which increments a range, but I don't know how to modify it so that it would set a range to zero lazily in O(log(N)) time.

http://se7so.blogspot.com.au/2012/12/segment-trees-and-lazy-propagation.html

I'm thinking of using a "lazy clear" flag on each of the nodes, but how would I know to clear first then lazy add or lazy add then clear (which would be just clear)?

William Rookwood
  • 297
  • 3
  • 15

1 Answers1

1

Use a queue instead of a flag on each node which'll contain the operations to process, as long as you use a FIFO data structure you can be sure they are done in the proper order. Unless I am missing the point on the question.

Justin
  • 4,196
  • 4
  • 24
  • 48
  • So are you suggesting instead of the lazy[MAX] array I replace it with a queue lazy[MAX] so a queue is mapped to each node? And then when I propagate one node, I pop the ops from the queue and push them onto the child node queues? The queue will never have more than 2 items correct? – William Rookwood Feb 16 '13 at 23:04
  • Yes but the queue will never have more than 2 items is an implementation detail. I've implemented lazy segment tree where all modifications (add, clear) get simply pushed onto each affected node's queue. Once a query is performed, you process everything in each node's (which is involved in the query) queue. You essentially make modifications faster at the cost of making queries slower. – Justin Feb 16 '13 at 23:08
  • @WilliamRookwood See my comment above. – Justin Feb 17 '13 at 02:19
  • yes I'm trying to implement it, except queues don't work for some reason it makes my program crash if I try to have an array of 3 million deques. So I'm implementing it except just with 2 "flag slots" on each node, the 1st representing the first operation, the second representing the second operation. It's getting really messy though, a lot of "if" statements to handle the different cases of the slots. – William Rookwood Feb 17 '13 at 02:58