Python/data_structures/persistent_segment_tree.py

95 lines
3.1 KiB
Python
Raw Normal View History

2024-10-19 16:43:40 +05:30
from __future__ import annotations
2024-10-19 15:58:29 +05:30
class Node:
def __init__(self, value: int = 0) -> None:
2024-10-19 16:27:52 +05:30
self.value: int = value
2024-10-19 16:43:40 +05:30
self.left: Node | None = None
self.right: Node | None = None
2024-10-19 15:58:29 +05:30
class PersistentSegmentTree:
2024-10-19 16:43:40 +05:30
def __init__(self, arr: list[int]) -> None:
2024-10-19 16:27:52 +05:30
self.n: int = len(arr)
2024-10-19 16:43:40 +05:30
self.roots: list[Node] = []
2024-10-19 16:16:21 +05:30
self.roots.append(self._build(arr, 0, self.n - 1))
2024-10-19 16:43:40 +05:30
def _build(self, arr: list[int], start: int, end: int) -> Node:
2024-10-19 16:19:23 +05:30
"""
Builds a segment tree from the provided array.
"""
2024-10-19 15:58:29 +05:30
if start == end:
return Node(arr[start])
mid = (start + end) // 2
node = Node()
node.left = self._build(arr, start, mid)
node.right = self._build(arr, mid + 1, end)
node.value = node.left.value + node.right.value
return node
def update(self, version: int, index: int, value: int) -> int:
2024-10-19 16:22:59 +05:30
"""
Updates the value at the given index and returns the new version.
"""
2024-10-19 15:58:29 +05:30
new_root = self._update(self.roots[version], 0, self.n - 1, index, value)
self.roots.append(new_root)
2024-10-19 16:16:21 +05:30
return len(self.roots) - 1
2024-10-19 15:58:29 +05:30
def _update(
self, node: Node | None, start: int, end: int, index: int, value: int
) -> Node:
2024-10-19 16:57:21 +05:30
"""
Updates the node for the specified index and value and returns the new node.
"""
2024-10-19 17:04:04 +05:30
if node is None: # Handle the None case
node = Node() # Create a new node if None
2024-10-19 15:58:29 +05:30
if start == end:
2024-10-19 16:16:21 +05:30
return Node(value)
2024-10-19 15:58:29 +05:30
mid = (start + end) // 2
new_node = Node()
2024-10-19 16:16:21 +05:30
2024-10-19 15:58:29 +05:30
if index <= mid:
new_node.left = self._update(node.left, start, mid, index, value)
2024-10-19 16:57:21 +05:30
new_node.right = node.right # Ensure right node is the same as the original
2024-10-19 15:58:29 +05:30
else:
2024-10-19 16:57:21 +05:30
new_node.left = node.left # Ensure left node is the same as the original
2024-10-19 15:58:29 +05:30
new_node.right = self._update(node.right, mid + 1, end, index, value)
2024-10-19 16:16:21 +05:30
new_node.value = new_node.left.value + (
new_node.right.value if new_node.right else 0
)
2024-10-19 16:16:21 +05:30
2024-10-19 15:58:29 +05:30
return new_node
def query(self, version: int, left: int, right: int) -> int:
2024-10-19 16:22:59 +05:30
"""
Queries the sum in the given range for the specified version.
"""
2024-10-19 15:58:29 +05:30
return self._query(self.roots[version], 0, self.n - 1, left, right)
def _query(
self, node: Node | None, start: int, end: int, left: int, right: int
) -> int:
2024-10-19 16:57:21 +05:30
"""
Queries the sum of values in the range [left, right] for the given node.
"""
if node is None or left > end or right < start:
2024-10-19 15:58:29 +05:30
return 0
if left <= start and right >= end:
return node.value
mid = (start + end) // 2
return self._query(node.left, start, mid, left, right) + self._query(
node.right, mid + 1, end, left, right
)
2024-10-19 16:16:21 +05:30
2024-10-19 16:19:23 +05:30
# Running the doctests
2024-10-19 16:16:21 +05:30
if __name__ == "__main__":
import doctest
2024-10-19 16:16:21 +05:30
print("Running doctests...")
result = doctest.testmod()
print(f"Ran {result.attempted} tests, {result.failed} failed.")