mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-18 16:27:02 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
9e07fc9cd4
commit
bcd9f5dcdd
|
@ -1,11 +1,13 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
class Node:
|
class Node:
|
||||||
def __init__(self, value: int = 0) -> None:
|
def __init__(self, value: int = 0) -> None:
|
||||||
self.value: int = value
|
self.value: int = value
|
||||||
self.left: Node | None = None
|
self.left: Node | None = None
|
||||||
self.right: Node | None = None
|
self.right: Node | None = None
|
||||||
|
|
||||||
|
|
||||||
class PersistentSegmentTree:
|
class PersistentSegmentTree:
|
||||||
def __init__(self, arr: list[int]) -> None:
|
def __init__(self, arr: list[int]) -> None:
|
||||||
self.n: int = len(arr)
|
self.n: int = len(arr)
|
||||||
|
@ -81,7 +83,9 @@ class PersistentSegmentTree:
|
||||||
new_node.left = node.left # Ensure left node is the same as the original
|
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.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
|
return new_node
|
||||||
|
|
||||||
|
@ -120,12 +124,15 @@ class PersistentSegmentTree:
|
||||||
if left <= start and right >= end:
|
if left <= start and right >= end:
|
||||||
return node.value
|
return node.value
|
||||||
mid = (start + end) // 2
|
mid = (start + end) // 2
|
||||||
return (self._query(node.left, start, mid, left, right) +
|
return self._query(node.left, start, mid, left, right) + self._query(
|
||||||
self._query(node.right, mid + 1, end, left, right))
|
node.right, mid + 1, end, left, right
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# Running the doctests
|
# Running the doctests
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import doctest
|
import doctest
|
||||||
|
|
||||||
print("Running doctests...")
|
print("Running doctests...")
|
||||||
result = doctest.testmod()
|
result = doctest.testmod()
|
||||||
print(f"Ran {result.attempted} tests, {result.failed} failed.")
|
print(f"Ran {result.attempted} tests, {result.failed} failed.")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user