Skip to content

Commit 72a184f

Browse files
committed
spelling + improved motivation for segment trees
1 parent 577b49c commit 72a184f

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/data_structures/segment_tree.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ e_maxx_link: segment_tree
66

77
# Segment Tree
88

9-
A Segment Tree is a data structure that stores information about array intervals as a tree. This allows answering range queries over an array efficiently, while still being flexible enough to allow quick modificiation of the array.
9+
A Segment Tree is a data structure that stores information about array intervals as a tree. This allows answering range queries over an array efficiently, while still being flexible enough to allow quick modification of the array.
1010
This includes finding the sum of consecutive array elements $a[l \dots r]$, or finding the minimum element in a such a range in $O(\log n)$ time.
1111
Between answering such queries, the Segment Tree allows modifying the array by replacing one element, or even changing the elements of a whole subsegment (e.g. assigning all elements $a[l \dots r]$ to any value, or adding a value to all element in the subsegment).
1212

@@ -23,11 +23,12 @@ The standard Segment Tree requires $4n$ vertices for working on an array of size
2323
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:
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$).
26+
Given an array $a[0 \dots n-1]$, 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$).
2727
The Segment Tree should be able to process **both** queries in $O(\log n)$ time.
2828

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.
29+
This is an improvement over the simpler approaches.
30+
A naive array implementation - just using a simple array - can update elements in $O(1)$, but requires $O(n)$ to compute each sum query.
31+
And precomputed prefix sums can compute sum queries in $O(1)$, but updating an array element requires $O(n)$ changes to the prefix sums.
3132

3233
### Structure of the Segment Tree
3334

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