jigyasu_kalyan's blog

By jigyasu_kalyan, 12 months ago, In English

Hello Codeforces, We all learn push()/push_down() function when studying Lazy Segment Trees. It's the engine that propagates the changes stored on a node to its children. This ensures that before we recurse deeper, the children of the current node are updated with any pending changes, guaranteeing we always work with consistent data. I was just studying Lazy Segment Trees and a fundamental question came to my mind. Is it possible to continue in Lazy trees without this push() function? I tried some variations of problems where we use Lazy Propagation with push() function, and tried to solve and find approaches on paper pen and I was able to find way to solve problems without push() function. I discussed with my peers an faculty if they can prove me wrong in those solutions and can provide some variation where we strictly need push() function with lazy tree. I am listing some variations below and their standard approaches of lazy tree with push() function and my approach of lazy tree without push() function.

Range Update Point Query

The problem is to add val in range [l, r] and query some index pos.

Standard Approach
My Approach

Range Update Range Query

The problem is to add val in range [l, r]and in query-1 and answer the sum in range [l, r] in query-2.

Standard Approach
My Approach

Adding on Segments, Querying on Maximum

The problem is to add val in range [l, r] in query-1 and answer the max_value in range [l, r] in query-2.

Standard Approach
My Approach

Range Assignment, Point Query

The problem is to assign val to all indices in range [l, r] in query-1 and answer some index pos in query-2.

Standard Approach
My Approach

There are many other variations, but covering them all would make this blog post too long. So, I'll end with a question: Is the push() function a necessity in a lazy segment tree, or is it just a way to keep the code clean?

Yo! Tadaa!

  • Vote: I like it
  • +5
  • Vote: I do not like it

»
11 months ago, hide # |
← Rev. 2  
Vote: I like it 0 Vote: I do not like it

bro you accumulated the nodes its just the function of the push itself. also you should push as it'll make it easier to keep track of it

  • »
    »
    11 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    yeah, in my approach nodes in tree don't have correct values. But when we use push. nodes store correct values in tree. So, I just wanted to ask will it make a difference if we keep it dynamic instead of pushing values everytime. Thanks for your clarification :)

»
11 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Bro you are writing codes for contests. If you want codes that are long and uneasy to read you can try so LOL

  • »
    »
    11 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    yeah, i know that the code will get a bit complex. I wanted to ask that only, if this approach fails somewhere or it's just to keep the code clean.

»
3 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

I think one issue is with the set_range updates, while propagating downwards, u need to make sure u maintain the order of the updates, but that can actually be handled by storing time stamps of the queries.

Ye nice, i like this version without the push function....

And yeah, to prove the correctness of this approach, first of all we need to consider only the ranges that are special (the segtree nodes)

lets say some node i was updated, and j is queried.

if j is an ancestor of i, then it stores correct information about i, because we updated the information while bactracking.

if j is a child of i, then it accumulates the update based on time stamp, and has the correct information.

in both cases the information is made sure to be consistent.

SO I RECKON, WE DO NOT NEED AN EXPLICIT PUSH FUNCTION