Skip to content

Commit fa72825

Browse files
authored
Add motivation for data structure
1 parent b7df8c8 commit fa72825

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

src/data_structures/segment_tree.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@ 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-
The Segment Tree should be able to process both queries in $O(\log n)$ time.
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.
2828

2929
### Structure of the Segment Tree
3030

31-
So, what is a Segment Tree?
32-
31+
We can take a divide-and-conquer approach when it comes to array segments.
3332
We compute and store the sum of the elements of the whole array, i.e. the sum of the segment $a[0 \dots n-1]$.
3433
We then split the array into two halves $a[0 \dots n/2]$ and $a[n/2+1 \dots n-1]$ and compute the sum of each halve and store them.
3534
Each of these two halves in turn also split in half, their sums are computed and stored.

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