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
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 ppp holds the sum of [ij][i \ldots j][ij] range, its left and right children hold the sum for range [ii+j2][i \ldots \frac{i + j}{2}][i2i+j] and [i+j2+1,j][\frac{i + j}{2} + 1, j][2i+j+1,j] respectively.
Therefore to find the sum of node ppp, 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,,n1]a[0, 1, \ldots, n-1]a[0,1,,n1]. 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 iii 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]a[i]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][L, R][L,R] using segment tree in the following way:
Algorithm hold loop invariant:
lrl \le rlr and sum of [Ll][L \ldots l][Ll] and [rR][r \ldots R][rR] has been calculated, where lll and rrr are the left and right boundary of calculated sum. Initially we set lll with left leaf LLL and rrr with right leaf RRR. Range [l,r][l, r][l,r] shrinks on each iteration till range borders meets after approximately logn\log nlogn iterations of the algorithm
  • Loop till lrl \le
    • Check if ll is right child of its parent PP
      • lll is right child of PPP. Then PP contains sum of range of ll and another child which is outside the range [l,r][l, r] and we don't need parent PP sum. Add ll to sumsum without its parent PP and set ll to point to the right of PP on the upper level.
      • lll is not right child of PP. Then parent PP contains sum of range which lies in [l,r][l, r]. Add PP to sumsum and set ll to point to the parent of PP
    • Check if rr is left child of its parent PP
      • rr is left child of PP. Then PP contains sum of range of rr and another child which is outside the range [l,r][l, r] and we don't need parent PP sum. Add rr to sumsum without its parent PP and set rr to point to the left of PP on the upper level.
      • rr is not left child of PP. Then parent PP contains sum of range which lies in [l,r][l, r][l,r]. Add PP to sumsum and set rr to point to the parent of PP


Ref: https://leetcode.com/problems/range-sum-query-mutable/solution/#

Comments

Popular posts from this blog

zhitongguigu

Array Part2

Two pointers (Part 1)