Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit577b49c

Browse files
authored
Add comparison of segment tree to simple array and prefix sums
1 parent60ce472 commit577b49c

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)

[8]ページ先頭

©2009-2025 Movatter.jp