[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2024-10-19 11:34:26 +00:00
parent e968d1261b
commit 4e95c904f0

View File

@ -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.")