Complete the sentence to describe the main purpose of a segment tree.
A segment tree is a data structure used to efficiently perform [1] on array intervals.A segment tree is designed to answer range queries efficiently, such as finding sums or minimums over intervals.
Complete the sentence about the time complexity of segment tree queries.
The time complexity to answer a range query using a segment tree is O([1]).Segment trees answer range queries in O(log n) time, which is much faster than scanning the array.
Complete the sentence about the time complexity of segment tree construction.
Building a segment tree for an array of size n takes O([1]) time.
Constructing a segment tree requires processing O(n) nodes, each in constant time, leading to O(n) time.
Fill both blanks to complete the segment tree update operation description.
To update an element at index [1] in the array, we update the leaf node and then update all [2] nodes on the path to the root.
The index to update is usually called i. After updating the leaf node, all internal nodes on the path to the root must be updated to reflect the change.
Fill both blanks to complete the dictionary comprehension for segment tree leaves initialization.
leaves = {index: value for index, value in enumerate(array) if index [1] n//2} # Only first half leaves
internal = [2] # Placeholder for internal nodesThe leaves dictionary starts with a { to create a dictionary comprehension. The condition uses < to select the first half of the array. The internal nodes are initialized as an empty dictionary {}.