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-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -653,11 +653,11 @@ These values can be computed in parallel to the merging step when we build the t
653
653
654
654
How does this speed up the queries?
655
655
656
-
Remember, in the normal solution we did a binary search in ever node.
656
+
Remember, in the normal solution we did a binary search in every node.
657
657
But with this modification, we can avoid all except one.
658
658
659
-
To answer a query, we simply to a binary search in the root node.
660
-
This gives as the smallest element $y \ge x$ in the complete array, but it also gives us two positions.
659
+
To answer a query, we simply do a binary search in the root node.
660
+
This gives us the smallest element $y \ge x$ in the complete array, but it also gives us two positions.
661
661
The index of the smallest element greater or equal $x$ in the left subtree, and the index of the smallest element $y$ in the right subtree. Notice that $\ge y$ is the same as $\ge x$, since our array doesn't contain any elements between $x$ and $y$.
662
662
In the normal Merge Sort Tree solution we would compute these indices via binary search, but with the help of the precomputed values we can just look them up in $O(1)$.
663
663
And we can repeat that until we visited all nodes that cover our query interval.
@@ -755,7 +755,7 @@ But before we do this, we must first sort out the root vertex first.
755
755
The subtlety here is that the right half of the array should still be assigned to the value of the first query, and at the moment there is no information for the right half stored.
756
756
757
757
The way to solve this is to push the information of the root to its children, i.e. if the root of the tree was assigned with any number, then we assign the left and the right child vertices with this number and remove the mark of the root.
758
-
After that, we can assign the left child with the new value, without loosing any necessary information.
758
+
After that, we can assign the left child with the new value, without losing any necessary information.
759
759
760
760
Summarizing we get:
761
761
for any queries (a modification or reading query) during the descent along the tree we should always push information from the current vertex into both of its children.
0 commit comments