Comparison Mode

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

Use caseMany range-sum queries on an array that never changes.Range queries interleaved with point (or lazy range) updates; any associative operation.
RequirementsAn invertible operation (sum, XOR); static data.An associative combine function; 4n array or explicit nodes.
Time complexityO(n) build, O(1) query, O(n) per update.O(n) build, O(log n) query and update.
Space complexityO(n).O(n) (about 4n slots in the array form).
StrengthsTrivial to write; constant-time queries; combines with a hash map to count subarrays.Supports min/max/gcd/custom merges and updates; lazy propagation gives range updates.
WeaknessesAny update invalidates every later prefix; only works for invertible operations.More code and constant factor; overkill for static sums.
Example problemsRange sum query immutable, subarray sum equals k, product of array except self.Range sum query mutable, count of smaller numbers after self, sliding-window range min with updates.
Choose this whenChoose prefix sums when the array is static and the query is a sum-like invertible aggregate.Choose a segment tree (or Fenwick tree for sums) when updates and queries interleave, or the operation is min/max/gcd.