Jill-Jênn Vie

Researcher at Inria

% Segment trees % JJV; CP Algorithms % 29 septembre 2023 — aspectratio: 169 —

Structure

For a given array $a$

Segment tree

Each node:

Building the tree has complexity $O(n)$

The height is $O(\log n)$, the complexity of queries is $4 \log n = O(\log n)$

Sum queries

Requested $[\ell, r]$

If current node is $[t\ell, tr]$, three cases:

Update queries

Update element at position $i$

If current node is $[t\ell, tr]$:

Segment trees defined by arrays

Just like heaps

Variants

Should wonder: what attributes at each node, how to merge children info upwards

Union of rectangles

More variants: lazy

Attribute is “how much is added to this segment”.
Then we will compute the actual value of a cell only if requested, in $O(\log n)$.

Or:

One can also lazy build the segment tree (grow node only if needed)

Variant: persistent

What’s in the notebook

https://cp-algorithms.com/data_structures/segment_tree.html