Algorithmslazy propagation assignment

Lazy Propagation with Assignment (Range Set Updates in Segment Tree)

TT
Testlaa Team
May 15, 20261 min read

Assignment lazy sets an entire segment to one value—on push, children reset sum and assignment tag; pending adds cleared.

Why this shows up in the real world

Time-series dashboards, game leaderboards, and competitive programming interval problems all need fast answers on changing arrays.

Core idea (explained for students)

Used in painting problems, range set, and some max queries with assign.

Try this in Python

def apply_assign(sum_seg: list[int], lazy: list[int], i: int, l: int, r: int, v: int) -> None:
    sum_seg[i] = v * (r - l + 1)
    lazy[i] = v  # assignment tag; real code uses sentinel for 'no assign'


# Full lazy-assign tree is longer; pair with range_override_logic lesson.
print("assignment lazy: set node sum = v * length")

Common mistakes

  • Letting old add lazy survive after assign.
  • Not setting both children on assign push.

Key takeaways

  • Rule: assign tag dominates add tag when both exist.
  • Query after assign still O(log n).

Tags:

Segment tree & range queriesPythonStudents