cheran-senthil / PyRival

⚡ Competitive Programming Library
https://pyrival.readthedocs.io/
Apache License 2.0
1.17k stars 312 forks source link

LazySegmentTree Issue #73

Closed akshitm169 closed 2 years ago

akshitm169 commented 2 years ago

Hi, I was trying LazySegmentTree on D. Addition and Sum but getting different output.

My Code

from sys import stdin, stdout
input = stdin.readline

class LazySegmentTree:
    def __init__(self, data, default=0, func= lambda a,b: a+b):
        """initialize the lazy segment tree with data"""
        self._default = default
        self._func = func

        self._len = len(data)
        self._size = _size = 1 << (self._len - 1).bit_length()
        self._lazy = [0] * (2 * _size)

        self.data = [default] * (2 * _size)
        self.data[_size:_size + self._len] = data
        for i in reversed(range(_size)):
            self.data[i] = func(self.data[i + i], self.data[i + i + 1])

    def __len__(self):
        return self._len

    def _push(self, idx):
        """push query on idx to its children"""
        # Let the children know of the queries
        q, self._lazy[idx] = self._lazy[idx], 0

        self._lazy[2 * idx] += q
        self._lazy[2 * idx + 1] += q
        self.data[2 * idx] += q
        self.data[2 * idx + 1] += q

    def _update(self, idx):
        """updates the node idx to know of all queries applied to it via its ancestors"""
        for i in reversed(range(1, idx.bit_length())):
            self._push(idx >> i)

    def _build(self, idx):
        """make the changes to idx be known to its ancestors"""
        idx >>= 1
        while idx:
            self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) + self._lazy[idx]
            idx >>= 1

    def add(self, start, stop, value):
        """lazily add value to [start, stop)"""
        start = start_copy = start + self._size
        stop = stop_copy = stop + self._size
        while start < stop:
            if start & 1:
                self._lazy[start] += value
                self.data[start] += value
                start += 1
            if stop & 1:
                stop -= 1
                self._lazy[stop] += value
                self.data[stop] += value
            start >>= 1
            stop >>= 1

        # Tell all nodes above of the updated area of the updates
        self._build(start_copy)
        self._build(stop_copy - 1)

    def query(self, start, stop, default=0):
        """func of data[start, stop)"""
        start += self._size
        stop += self._size

        # Apply all the lazily stored queries
        self._update(start)
        self._update(stop - 1)

        res = default
        while start < stop:
            if start & 1:
                res = self._func(res, self.data[start])
                start += 1
            if stop & 1:
                stop -= 1
                res = self._func(res, self.data[stop])
            start >>= 1
            stop >>= 1
        return res

    def __repr__(self):
        return "LazySegmentTree({0})".format(self.data)

n,m=map(int,input().split())
arr=[0 for x in range(n)]
ST=LazySegmentTree(arr)
for _ in range(m):
    ops=[int(x) for x in input().split()]
    if ops[0]==1:
        ST.add(ops[1],ops[2],ops[3])
    else:
        print(ST.query(ops[1],ops[2]))

Input:

5 6
1 0 3 3
2 1 2
1 1 4 4
2 1 3
2 1 4
2 3 5

Expected Output:

3
14
18
4

Output of above code:

3
14
14
4
bjorn-martinsson commented 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.

akshitm169 commented 2 years ago

Thank you very much!!