Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.

The update(i, val) function modifies nums by updating the element at index i to val.
Example:
Given nums = [1, 3, 5] sumRange(0, 2) -> 9
update(1, 2)
sumRange(0, 2) -> 8
Note:
The array is only modifiable by the update function.
You may assume the number of calls to update and sumRange function is distributed evenly.

Introduction of Segment Tree: http://www.geeksforgeeks.org/segment-tree-set-1-sum-of-given-range/

Time Complexity:
Time Complexity for tree construction is O(n). There are total 2n-1 nodes, and value of every node is calculated only once in tree construction.

Time complexity to query is O(Logn). To query a sum, we process at most four nodes at every level and number of levels is O(Logn).

The time complexity of update is also O(Logn). To update a leaf value, we process one node at every level and number of levels is O(Logn).

 public class NumArray {
SegmentTreeNode root; public NumArray(int[] nums) {
this.root = buildTree(nums, 0, nums.length-1);
} void update(int i, int val) {
update(root, i, val);
} public int sumRange(int i, int j) {
return sumRange(root, i, j);
} public SegmentTreeNode buildTree(int[] nums, int start, int end) {
if (start > end) return null;
else {
SegmentTreeNode cur = new SegmentTreeNode(start, end);
if (start == end) {
cur.sum = nums[start];
}
else {
int mid = start + (end - start)/2;
cur.left = buildTree(nums, start, mid);
cur.right = buildTree(nums, mid+1, end);
cur.sum = cur.left.sum + cur.right.sum;
}
return cur;
}
} public void update(SegmentTreeNode root, int i, int val) {
if (root.start == root.end) { //leaf node
root.sum = val;
return;
}
else {
int mid = root.start + (root.end - root.start)/2;
if (i <= mid) update(root.left, i, val);
else update(root.right, i, val);
root.sum = root.left.sum + root.right.sum;
}
} public int sumRange(SegmentTreeNode root, int i, int j) {
if (i==root.start && j==root.end) return root.sum;
else {
int mid = root.start + (root.end - root.start)/2;
if (j <= mid) return sumRange(root.left, i, j);
else if (i >= mid+1) return sumRange(root.right, i, j);
else
return sumRange(root.left, i, mid) + sumRange(root.right, mid+1, j);
}
} public class SegmentTreeNode{
int sum;
int start;
int end;
SegmentTreeNode left;
SegmentTreeNode right; public SegmentTreeNode(int start, int end) {
this.sum = 0;
this.start = start;
this.end = end;
this.left = null;
this.right = null;
} }
} // Your NumArray object will be instantiated and called as such:
// NumArray numArray = new NumArray(nums);
// numArray.sumRange(0, 1);
// numArray.update(1, 10);
// numArray.sumRange(1, 2);

Leetcode: Range Sum Query - Mutable && Summary: Segment Tree的更多相关文章

  1. [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  2. [LeetCode] Range Sum Query - Mutable 题解

    题目 题目 思路 一看就是单点更新和区间求和,故用线段树做. 一开始没搞清楚,题目给定的i是从0开始还是从1开始,还以为是从1开始,导致后面把下标都改掉了,还有用区间更新的代码去实现单点更新,虽然两者 ...

  3. [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  4. LeetCode Range Sum Query 2D - Mutable

    原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...

  5. [Leetcode Week16]Range Sum Query - Mutable

    Range Sum Query - Mutable 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/range-sum-query-mutable/de ...

  6. 【刷题-LeetCode】307. Range Sum Query - Mutable

    Range Sum Query - Mutable Given an integer array nums, find the sum of the elements between indices ...

  7. [LeetCode] Range Sum Query 2D - Immutable 二维区域和检索 - 不可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  8. [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  9. Leetcode: Range Sum Query 2D - Mutable && Summary: Binary Indexed Tree

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

随机推荐

  1. MTU-TCP/IP协议栈-linux kernel-TCP丢包重传-UDP高性能-AI-

    http://view.inews.qq.com/a/20161025A0766200窄带时代的QQQQ是窄带时代极具代表性的产品,在那个网络传输效率比较低的年代,大家还记得Google的首页吗?Go ...

  2. IOS 入门开发教程

    object-c: http://mobile.51cto.com/iphone-261129.htm Objective-C入门教材 Objective-C入门教材 2011-05-11 15:58 ...

  3. JS时间戳格式化日期时间 由于mysql数据库里面存储时间存的是时间戳,取出来之后,JS要格式化一下显示。

    //时间戳转时间 function RiQi(sj) { var now = new Date(sj*1000); var year=now.getFullYear(); var month=now. ...

  4. 在脚本中操作plist文件

    终端输入: /usr/libexec/PlistBuddy -c "Print CFBundleIdentifier" /Users/achen/Desktop/testBundl ...

  5. mysql 存储过程--- 创建,调用,删除

    DELIMITER //CREATE PROCEDURE p_addscore(nums INT,OUT retrows INT)BEGINDECLARE i INT DEFAULT 0;add_lo ...

  6. SQL SERVER中非聚集索引的覆盖,连接,交叉,过滤

    1.覆盖索引:select和where中包含的结果集中应存在“非聚集索引列”,这样就不用查找基表了,索引表即可搞定:   2.索引交叉:索引的交叉可以理解成建立多个非聚集索引之间的join,如表实体一 ...

  7. Windows搭建Cocos2d-JS环境

    目录 安装Python Cocos2d-JS框架 开发工具(WebStorm / Cocos Code IDE) 用Webstorm查看官方实例 使用Cocos Code IDE创建工程 1.安装Py ...

  8. strcpy之代码的健壮性与可维护性

    strcpy   函数的原型是: char * strcpy(char * strDest,const char * strSrc);    功能:把从strSrc地址开始且含有NULL结束符的字符串 ...

  9. sublime text2 操作及插件

    sublime text2 1. 文件快速导航: 这是sublime上面很好用的功能之一,ctrl+p可以调出窗口,菜单上的解释是gotoanythings ,确实如其所言,调出窗口后,直接输入关键字 ...

  10. JS事件分析

    1.注册事件 1.1 使用HTML元素的事件属性 <div id='myDiv' style="width:100px;height:100px;background-color:re ...