Comparison Mode

Side-by-side: use case, requirements, complexity, strengths, weaknesses, example problems, and a clear “choose this when…”.

Use caseRange queries for any associative operation, with point or lazy range updates.Prefix sums with point updates; range sums via subtraction.
RequirementsAssociative combine; roughly 4n storage.An invertible operation (sum, XOR); 1-indexed array of size n + 1.
Time complexityO(n) build, O(log n) query and update.O(n log n) (or O(n)) build, O(log n) query and update.
Space complexityO(4n).O(n).
StrengthsHandles min/max/gcd, range assignment, lazy propagation, and "first index where prefix exceeds x" searches.About ten lines; very fast; tiny memory; easy to extend to 2D.
WeaknessesLonger code, larger constant, more memory.Only invertible operations; no lazy range updates without a second tree; no arbitrary range min.
Example problemsRange sum query mutable, range minimum with updates, counting inversions online.Range sum query mutable, count of smaller numbers after self, number of inversions.
Choose this whenChoose a segment tree when the operation is not invertible (min, max, gcd) or you need lazy range updates.Choose a Fenwick tree for sums/XOR with point updates; it is shorter, faster and enough for most interview range problems.