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 | aba \cdot b | 11 | | GCD | gcd(a,b)\gcd(a, b) | 00 | | XOR | aba \oplus b | 00 | | AND | aba \land b | all 1s | | OR | aba \lor b | 00 | The requirement: (ab)c=a(bc)(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.