1/31
Loading...
🌳Range Sums, Fast
Summing a range [l..r] each time is O(n). If we precompute partial sums in a tree, both queries and updates run in O(log n).
🔒
Loading...
Summing a range [l..r] each time is O(n). If we precompute partial sums in a tree, both queries and updates run in O(log n).
A segment tree is a binary tree that splits an array into "segments" and stores each segment's sum in a node. The root covers the whole range, each leaf a single element.