From 4e95c904f0fd5b4bf74bca427241082ac8a74c6b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 19 Oct 2024 11:34:26 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/persistent_segment_tree.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/data_structures/persistent_segment_tree.py b/data_structures/persistent_segment_tree.py index 1cd526a29..54fa38320 100644 --- a/data_structures/persistent_segment_tree.py +++ b/data_structures/persistent_segment_tree.py @@ -1,11 +1,13 @@ from __future__ import annotations + class Node: def __init__(self, value: int = 0) -> None: self.value: int = value self.left: Node | None = None self.right: Node | None = None + class PersistentSegmentTree: def __init__(self, arr: list[int]) -> None: self.n: int = len(arr) @@ -33,7 +35,9 @@ class PersistentSegmentTree: self.roots.append(new_root) return len(self.roots) - 1 - def _update(self, node: Node | None, start: int, end: int, index: int, value: int) -> Node: + def _update( + self, node: Node | None, start: int, end: int, index: int, value: int + ) -> Node: """ Updates the node for the specified index and value and returns the new node. """ @@ -53,7 +57,9 @@ class PersistentSegmentTree: new_node.left = node.left # Ensure left node is the same as the original new_node.right = self._update(node.right, mid + 1, end, index, value) - new_node.value = new_node.left.value + (new_node.right.value if new_node.right else 0) + new_node.value = new_node.left.value + ( + new_node.right.value if new_node.right else 0 + ) return new_node @@ -63,7 +69,9 @@ class PersistentSegmentTree: """ 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: + def _query( + self, node: Node | None, start: int, end: int, left: int, right: int + ) -> int: """ Queries the sum of values in the range [left, right] for the given node. """ @@ -72,12 +80,15 @@ class PersistentSegmentTree: 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)) + return self._query(node.left, start, mid, left, right) + self._query( + node.right, mid + 1, end, left, right + ) + # Running the doctests if __name__ == "__main__": import doctest + print("Running doctests...") result = doctest.testmod() print(f"Ran {result.attempted} tests, {result.failed} failed.")