Closed akshitm169 closed 2 years ago
That lazy segment tree code is written to be used with max, not addition.
To make it be able to handle addition you need to make some modifications. Change
self.data[2 * idx] += q
self.data[2 * idx + 1] += q
...
self.data[start] += value
...
self.data[stop] += value
to
self.data[2 * idx] += q * interval_length[2 * idx]
self.data[2 * idx + 1] += q * interval_length[2 * idx + 1]
...
self.data[start] += value * interval_length[start]
...
self.data[stop] += value * interval_length[stop]
where interval_length[i]
is the length of the interval corresponding to segment tree node i
.
Thank you very much!!
Hi, I was trying LazySegmentTree on D. Addition and Sum but getting different output.
My Code
Input:
Expected Output:
Output of above code: