def main(): # Operation: O(logN) arr = [3, 1, 5, 3, 13, 7, 2, 7, 2] tree = SegmentTree(arr) # Time: O(N), Space: O(N) # Sum print ("Sum from 1 to 3", arr[1:4], tree.query(1, 3, 'sum')) print ("Sum from 4 to 7", arr[4:8], tree.query(4, 7, 'sum')) # Min print ("Min from 1 to 3", arr[1:4], tree.query(1, 3, 'min')) print ("Min from 4 to 7", arr[4:8], tree.query(4, 7, 'min')) # Max print ("Max from 1 to 3", arr[1:4], tree.query(1, 3, 'max')) print ("Max from 4 to 7", arr[4:8], tree.query(4, 7, 'max'))
def test_stress(self): n = 1000 epochs = 100 arr = np.zeros(n) tree = SegmentTree(0, n) tree.update(0, n, 0) for i in range(epochs): l, r = np.random.randint(0, n, 2) l, r = sorted([l, r]) r += 1 val = np.random.random() tree.update(l, r, val) arr[l: r] = val for i in range(n): self.assertEqual(arr[i], tree.query(i))
class DyeWall: """ 染色墙 """ def __init__(self, data): self.data = data self._segment_tree = SegmentTree( self.data, merger=lambda x, y: len(set.union(x, y))) self._segment_tree.lazy_push_down = self._lazy_push_down def dye(self, i, j, color): self._dye(self._segment_tree.root, i, j, color) def _dye(self, node, l, r, color): if node.bound_l == l and node.bound_r == r: node.value = {color} node.lazy_tag += 1 return node mid = node.bound_l + (node.bound_r - node.bound_l) // 2 if r <= mid: node.left = self._dye(node.left, l, r, {color}) elif l >= mid + 1: node.right = self._dye(node.right, l, r, {color}) else: node.left = self._dye(node.left, l, mid, {color}) node.right = self._dye(node.right, mid + 1, r, {color}) node.value = self._merger(node.left.value, node.right.value) return node @staticmethod def _lazy_push_down(node): if node.bound_l == node.bound_r: return node.left.value = {node.color} node.left.lazy_tag += node.lazy_tag node.right.value = {node.color} node.right.lazy_tag += node.lazy_tag node.lazy_tag = 0 def query_color_numbers(self, i, j): return len(self._segment_tree.query(i, j))
from segment_tree import SegmentTree arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] t = SegmentTree(arr) a = t.query(2, 9, "max") print("The maximum value of this range is : ", a) a = t.query(2, 9, "min") print("The minimum value of this range is : ", a) a = t.query(2, 7, "sum") print("The sum of this range is : ", a) t.update(2, 25) print("The updated array is : ", arr)
#autor: Manjarrez Hernandez Raul #carrera: Ingenieria en Sistemas Computacionales from segment_tree import SegmentTree """una matriz con algunos elementos aquí estamos ajustando nuestra matriz en el árbol de segmentos donde t es tomado como objeto del árbol de segmentos""" arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] #t se utilizará para realizar operaciones en ese segmento t = SegmentTree(arr) #aquí estamos encontrando valor de número máximo en un rango a = t.query(0, 10, "max") print("El Valor Maximo del Rango es: ", a) #aquí estamos encontrando el valor de número mínimo en un rango a = t.query(4, 10, "min") print("El Valor Minimo del Rango es: ", a) #aquí estamos encontrando el valor de suma de un rango a = t.query(0, 3, "sum") print("La suma del Rango es: ", a) #aquí estamos actualizando el valor de un índice particular t.update(2, 57) #reemplazará el valor de índice '2' con 25 print("El Arreglo Actualizado es : ", arr)