You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/data_structures/segment_tree.md
+4-1Lines changed: 4 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,10 @@ To start easy, we consider the simplest form of a Segment Tree.
24
24
We want to answer sum queries efficiently.
25
25
The formal definition of our task is:
26
26
We have an array $a[0 \dots n-1]$, and the Segment Tree must be able to find the sum of elements between the indices $l$ and $r$ (i.e. computing the sum $\sum_{i=l}^r a[i]$), and also handle changing values of the elements in the array (i.e. perform assignments of the form $a[i] = x$).
27
-
If we computed each query naively, each query would take $O(n)$ time. The Segment Tree should be able to process both queries in $O(\log n)$ time.
27
+
The Segment Tree should be able to process**both** queries in $O(\log n)$ time.
28
+
29
+
A naive array implementation can update elements in $O(1)$, but requires $O(n)$ to recompute each sum query.
30
+
Precomputed prefix sums can compute sum queries in $O(1)$, but updating an array element requires $O(n)$ changes to the prefix sums.