Efficient Range Queries with a Segment Tree
Segment trees are powerful data structures used to efficiently answer range queries on an array. They allow for quick calculations (like sum, min, max, etc.) over specific segments of the array, and also support updates to individual elements. This challenge asks you to implement a segment tree in JavaScript to perform range sum queries.
Problem Description
You are tasked with implementing a segment tree that supports range sum queries. The segment tree should be built from an input array and then allow you to efficiently calculate the sum of elements within a given range. Your implementation should include the following functionalities:
build(arr): Constructs the segment tree from the input arrayarr. The tree should be represented as an array.query(arr, left, right): Calculates the sum of elements in the arrayarrwithin the range[left, right](inclusive).update(arr, index, value): Updates the element atindexin the original arrayarrtovalueand propagates the change through the segment tree.
The segment tree should be a complete binary tree represented as an array. The root of the tree is at index 0. The left child of node i is at index 2*i + 1, and the right child is at index 2*i + 2. Leaf nodes correspond to the elements of the original array.
Examples
Example 1:
Input: arr = [1, 3, 5, 7, 9, 11]
build(arr) // Returns a segment tree array (implementation detail, not directly visible)
query(arr, 1, 3)
Output: 15
Explanation: The sum of elements from index 1 to 3 (inclusive) is 3 + 5 + 7 = 15.
query(arr, 0, 5)
Output: 36
Explanation: The sum of all elements is 1 + 3 + 5 + 7 + 9 + 11 = 36.
Example 2:
Input: arr = [2, 4, 6, 8, 10]
build(arr)
query(arr, 0, 2)
Output: 12
Explanation: The sum of elements from index 0 to 2 (inclusive) is 2 + 4 + 6 = 12.
update(arr, 2, 12)
query(arr, 0, 2)
Output: 18
Explanation: After updating arr[2] to 12, the sum from index 0 to 2 is 2 + 4 + 12 = 18.
Example 3: (Edge Case)
Input: arr = [5]
build(arr)
query(arr, 0, 0)
Output: 5
Explanation: Single element array, query on the single element returns the element itself.
update(arr, 0, 10)
query(arr, 0, 0)
Output: 10
Explanation: Updating the single element changes the value correctly.
Constraints
- The input array
arrwill contain non-negative integers. - The length of the input array
arrwill be between 1 and 100,000 (inclusive). leftandrightin thequeryfunction will be valid indices within the bounds of the array (0 <=left<=right<arr.length).indexin theupdatefunction will be a valid index within the bounds of the array (0 <=index<arr.length).- The time complexity of
buildshould be O(n), where n is the length of the array. - The time complexity of
queryandupdateshould be O(log n).
Notes
- The
buildfunction should return the segment tree array. - Consider how to efficiently calculate the sum of a range during the
queryfunction using the precomputed sums stored in the segment tree. - The
updatefunction needs to update the segment tree after modifying an element in the original array. Think about which nodes in the tree need to be updated. - You can assume that the input array will not be modified directly outside of the
updatefunction. - Focus on the core logic of the segment tree; you don't need to handle error cases like invalid input types.