Leetcode 307. Range Sum query-mutable

Source: Internet
Author: User


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.


Given an array of integers, find the and of the elements of the array subscript i to J.

Given an update operation, update (I,VAL) updates the first element to Val.


Example:

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


Note:

    1. The array is a modifiable by the update function.

    2. Assume the number of calls to update and sumrange function is distributed evenly.


    1. The array can only be changed by the Update method.

    2. Assume that update and sumrange are called evenly.


The first attempt is to use the sums element of the array to store [0:i] and, in this case: the time complexity of the Sumrange is O (1), because each update (I,VAL) to update after the subscript I sums, so the time complexity of O (n), Because both update and Sumrange are called evenly, the time complexity of the entire operation is O (n), and the storage space for n elements is required.


In that case, there is no accidental tle.


Consider using a binary tree to store parts and.


Constructs a complete binary tree, the last layer stores the original array elements, and each of the remaining layers stores the sum of the left and right child nodes, using a two-dimensional array tree[][] to store the complete binary tree, Tree[0] store the original array, Tree[i][j] storage tree[i-1][j*2] and Tree[i-1][j*2+1], of which i>0,j>=0;


Sumrange (I,j) method of seeking:

As a result of i<=j, it is only necessary to traverse from tree[0][i] and Tree[0][j] up to the nearest common ancestor node PF at the same time, and the sum of all elements of this tree that was preceded by the PF is recorded by S1 in the search process to s2 the sum of all elements before J, The final result is: S2-s1+tree[0][i];


Update method for S1:

If the currently traversed node I ' is the parent node I ' of the right child, then another S1 plus I ' left the child's value, if the left child does not need to be updated;

Update method for S2:

If the currently traversed node J ' is the parent of the right child of J ', then another S2 plus J ' ' left child's value, if the left child does not need to be updated;


Update (I,VAL) operation:

Press TREE[0][I],TREE[1][I/2],... The order is updated to the root node;


Complexity of:

Sumrange and update most traversal times are logn times, space complexity is O (n);


Class numarray{private:vector<vector<int>> tree;int rows, cols;public:numarray (vector<int> &nums) {int r = 0, c = nums.size ();if  ((cols = c)  == 0) {rows  = 0;return;} Tree.push_back (Nums);while  (1) {int size = tree[r].size ();if  (size == 1) break;vector<int> sums;for  (int i = 0;i < size;i += 2) { if  (i + 1 < size)     sums.push_back (tree[r][i] +  Tree[r][i + 1]); Else    sums.push_back (Tree[r][i]);} Tree.push_back (sums); ++r;} rows = r + 1;} Void update (int i, int val) {int delta = val - tree[0][i];for   (INT&NBSP;J&NBSP;=&NBSP;0;J&NBSP;&LT;&NBSP;ROWS;++J) {tree[j][i] += delta;i = i  / 2;}} Int sumrange (int i, INT&NBSP;J) {if  (i < 0 | |  i >= cols | |  j < 0 | |  j >= cols) return 0;int r, s1, s2, i0, i_, j_;r =  0;s1 = tree[r][i];s2 = tree[r][j];i0 = i;while  (i != j) {I_  = i / 2;j_ = j / 2;if  (i_ * 2 + 1 ==  i)// i is the right child of his parent{s1 += tree[r][ I&NBSP;-&NBSP;1];} if  (J_&NBSP;*&NBSP;2&NBSP;+&NBSP;1&NBSP;==&NBSP;J)// j is the right child  OF&NBSP;HIS&NBSP;PARENT{S2&NBSP;+=&NBSP;TREE[R][J&NBSP;-&NBSP;1];} ++r;i = i_;j = j_;} return s2 - s1 + tree[0][i0];}};


This article from "Ding Zhi-win blog" blog, declined reprint!

Leetcode 307. Range Sum query-mutable

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.