Code RoomSegment tree with lazy propagation
HardPrep Room Coding #867

Segment tree with lazy propagation

CodingDatabases & SQLAlgorithms & data structuresSenior–Staff~40 min

Implement a segment tree with lazy propagation over an array of length n initialized to all zeros. Process a list of operations: ["add", l, r, v] adds v to every element in the inclusive range [l, r], and ["sum", l, r] returns the sum of that inclusive range. Indices are 0-based and 0 <= l <= r < n. Return the list of answers to the "sum" operations, in order.

Implement
seg_lazy_range_add_sum(n: int, ops: list[list]) → list[int]
Examples
in[5,[["add",0,4,2],["sum",0,4],["add",1,3,3],["sum",1,3],["sum",0,4]]]out[10,15,19]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 40 min
InputExpectedGot
[5,[["add",0,4,2],["sum",0,4],["add",1,3,3],["sum",1,3],["sum",0,4]]][10,15,19]not run yetsample