Data Structures19 sections · 729 units
Open in Course

Segment Tree Generalization

Associative operations

Segment trees work for any associative operation with an identity element: | Operation | Merge | Identity | |-----------|-------|----------| | Sum | a+ba + b | 00 | | Min | min⁡(a,b)\min(a, b) | +∞+\infty | | Max | max⁡(a,b)\max(a, b) | −∞-\infty | | Product | a⋅ba \cdot b | 11 | | GCD | gcd⁡(a,b)\gcd(a, b) | 00 | | XOR | a⊕ba \oplus b | 00 | | AND | a∧ba \land b | all 1s | | OR | a∨ba \lor b | 00 | The requirement: (a∘b)∘c=a∘(b∘c)(a \circ b) \circ c = a \circ (b \circ c) (associativity).

For non-associative operations (like median), segment trees don't directly work. You'd need to store more information at each node.