segment tree
Second Time
=========================================================================
307 Range Sum Query - Mutable
(1) A naive way is to do range sum query whenever needed, i.e: for loop the number between index i and j and sum all of them together, this takes O(N) time for the range sum query operation.
(2) segment tree
Therefore to find the sum of node p, we need to calculate the sum of its right and left child in advance.
We begin from the leaves, initialize them with input array elements a[0,1,…,n−1]. Then we move upward to the higher level to calculate the parents' sum till we get to the root of the segment tree.
Algorithm hold loop invariant:
l≤r and sum of [L…l] and [r…R] has been calculated, where l and r are the left and right boundary of calculated sum. Initially we set l with left leaf L and r with right leaf R. Range [l,r] shrinks on each iteration till range borders meets after approximately logn iterations of the algorithm
Ref: https://leetcode.com/problems/range-sum-query-mutable/solution/#
=========================================================================
307 Range Sum Query - Mutable
(1) A naive way is to do range sum query whenever needed, i.e: for loop the number between index i and j and sum all of them together, this takes O(N) time for the range sum query operation.
(2) segment tree
1. Build segment tree
We will use a very effective bottom-up approach to build segment tree. We already know from the above that if some node p holds the sum of [i…j] range, its left and right children hold the sum for range [i…2i+j] and [2i+j+1,j] respectively.Therefore to find the sum of node p, we need to calculate the sum of its right and left child in advance.
We begin from the leaves, initialize them with input array elements a[0,1,…,n−1]. Then we move upward to the higher level to calculate the parents' sum till we get to the root of the segment tree.
2. Update segment tree
When we update the array at some index i we need to rebuild the segment tree, because there are tree nodes which contain the sum of the modified element. Again we will use a bottom-up approach. We update the leaf node that stores a[i]. From there we will follow the path up to the root updating the value of each parent as a sum of its children values.3. Range Sum Query
We can find range sum query [L,R] using segment tree in the following way:Algorithm hold loop invariant:
l≤r and sum of [L…l] and [r…R] has been calculated, where l and r are the left and right boundary of calculated sum. Initially we set l with left leaf L and r with right leaf R. Range [l,r] shrinks on each iteration till range borders meets after approximately logn iterations of the algorithm
- Loop till
- Check if is right child of its parent
- l is right child of P. Then contains sum of range of and another child which is outside the range and we don't need parent sum. Add to without its parent and set to point to the right of on the upper level.
- l is not right child of . Then parent contains sum of range which lies in . Add to and set to point to the parent of
- Check if is left child of its parent
- is left child of . Then contains sum of range of and another child which is outside the range and we don't need parent sum. Add to without its parent and set to point to the left of on the upper level.
- is not left child of . Then parent contains sum of range which lies in [l,r]. Add to and set to point to the parent of P
- Check if is right child of its parent
Ref: https://leetcode.com/problems/range-sum-query-mutable/solution/#
Comments
Post a Comment