Skip to content

Commit 577b49c

Browse files
authored
Add comparison of segment tree to simple array and prefix sums
1 parent 60ce472 commit 577b49c

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/data_structures/segment_tree.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ To start easy, we consider the simplest form of a Segment Tree.
2424
We want to answer sum queries efficiently.
2525
The formal definition of our task is:
2626
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.
2831

2932
### Structure of the Segment Tree
3033

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy