Leetcode--2 Range Sum query-mutable (tree-like Array Implementation)

Source: Internet
Author: User

Problem:

Given an integer array nums, find the sum of the elements between indices I and J (iJ), Inclusive.

the update (i, val) function modifies nums by updating the element on index i to Val.

Example:

Given nums = [1, 3, 5]sumrange (0, 2), 9Update (1, 2) sumrange (0, 2), 8

Analysis:

Beginning to think of a simple array to access and update and sum it, the actual writing found that it is not feasible to find a new data structure on the Internet or a new method-tree-like array (Binary Index tree, or Fenwick tree).

A tree array is often used to modify the value of a point, and to ask for an interval. Ordinary arrays Modify a point value time complexity is O (1), the time complexity of the query is O (n), and for the tree array the time complexity of the modification and query is O (logn).

reference--http://blog.csdn.net/int64ago/article/details/7429868

  

The tree array is a clever use of two points, first introduced two "tools"--lowbit (k) and "plus tail" Operation.

Lowbit (k) is the K of the binary high all clear 0, leaving only the lowest 1, such as lowbit (0101) = 0001, implemented: lowbit (k) =k& (-k).

Plus tail operation, put a number k plus its own lowbit (k), k=k+lowbit (k), such as the tail (0011) =0011+lowbit (0011) = 0100.

The "operation" of a array is actually the operation of the C array, that is, we are actually using the C array, then there is a relationship between C and a, first we need to find this relationship, which will use the first tool lowbit (k). C[k]=a[k] to the left Lowbit (k) and, For example c[0110]=a[0110] to the left to find Lowbit (0110) and, that is, to the left two and, then c[0110]=a[0110]+a[0101]. By this method, it is possible to determine which elements of a CK are determined by, for example, A.

The above is how to find the corresponding a in c, then by a to find the corresponding C will be used to the tail operation, because if we want to change a value in a, the value of C must also correspond to the change, so to find all C corresponding to A. As a[0011] corresponding c, plus tail (0011) =0011+lowbit (0011) = 0100, plus tail (0100) =0100+lowbit (0100) = 1000, so a[0011] corresponds to 3 c,c[0011],c[0100],c[ 1000].

From the diagram on the right we can see the corresponding relationship more intuitively, such as C4 directly contains the c2,c3 (a3), C4 (a4) (directly including the black square), and C2 directly contains a1,a2, then C4 contains a1~a4, and then c6, including C6 (a6), c5 (a5).

A and C correspondence is this, in order to correspond more convenient, usually used when the declaration of the array will give a space, from subscript to 1 to start with, subscript 0 of the unused, the following method is also starting from 1.

// adds an operation to a value in the A array void Add (int k,int  num)  {      while (k<=n)      {          tree[k]+ =num;          K+=k&-k;      }  
 //   void  change (int  k,int   num) { int  n=num-tree[k]; //     while   (k<=n) //  Then this while can be changed to add (n,num);            +=num;      K  +=k&-k; }  } 
 int  read (int  k) //  1~k and  { int  sum=0      ;  while   (k>0) {sum  +=tree[k];  K -=k&-k; //  The interval and the time will be reversed with the tail operation, that is, c corresponding to a all find out   " return   su  M }  
int sumik (int i,int k)//i~k interval and   {      sum (i);    Sum (k+1);     return sum (k+1)-sum (i); // borrow the preceding sum, but be aware that it should actually be K (i-1), since 1 starts with 1} on both sides.  

Solution:

 public classNumarray {int[] btree; int[] arr;  publicNumarray (int[] Nums) {btree=New int[nums.length+1]; Arr=nums;  for(inti=0; i<nums.length; i++) {add (i+1, nums[i]); }            }        voidAddintIintValue) {                 for(;i<btree.length;i+=i& (-I)) {btree[i]+=value; }    }        voidUpdateintIintVal) {add (i+1, val-arr[i]); arr[i]=val; }     public intSumrange (intIintJ) {returnSumhelp (j+1)-Sumhelp (i); }     public intSumhelp (intI) {        intSum=0;  for(;i>=1;i-=i& (-I)) {sum+=btree[i]; }        //while ((boolean) I)// {        //sum+=btree[i]; //i-=i& (-i); // }        returnsum; }}//Your Numarray object would be instantiated and called as Such://Numarray numarray = new Numarray (nums);//numarray.sumrange (0, 1);//numarray.update (1, ten);//numarray.sumrange (1, 2);

  

Leetcode--2 Range Sum query-mutable (tree-like Array Implementation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.