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. Natural Language Processing Computational Linguistics

    http://www.nltk.org/book/ch00.html After this, the pace picks up, and we move on to a series of chap ...

  2. IoC容器装配Bean(xml配置方式)(Bean的生命周期)

    1.Spring管理Bean,实例化Bean对象 三种方式 第一种:使用类构造器实例化(默认无参数) package cn.itcast.spring.initbean; /** * 使用构造方法 实 ...

  3. 获取真实ip的报告

    今天登录九秒社团 http://www.9miao.com/的时候忘记了用户名和密码,尝试了5次都没登录成功,网站弹出提示15分钟后才能再次登录.我纳闷它是怎么判断用户的登录次数,这时候用户还没有登录 ...

  4. ntpdate[16603]: the NTP socket is in use

    ubuntu使用ntpdate更新时间的时候提示错误如下 root@lnmp:/etc/squid3# sudo ntpdate cn.pool.ntp.org 5 Jan 07:22:59 ntpd ...

  5. NSURLSession

    参考文章1, apple文档 一.NSURLSessionConfiguration 介绍:分别配置每一个 session 对象.(NSURLConnection 很难做到) 分类: 1) defau ...

  6. 设计模式:享元模式(Flyweight)

    定   义:运用共享技术有效地支持大量细粒度的对象. 结构图: 内部状态:在享元对象内部并且不会随环境而改变的共享部分. 外部状态:随环境改变而改变的.不可共享的状态. Flyweight类,具体享元 ...

  7. window平台安装MongoDB

    官网:www.mongodb.org 安装-->设置环境变量-->启动 1.下载: 根据系统下载 32 位或 64 位的 .msi 文件,下载后双击该文件,按提示安装即可, 2.设置安装目 ...

  8. Swift-09-可空链式调用(Optional Chaining)

    我对这个的理解就是:我们有可能会用到其他的属性或者方法,当我们在使用其他的时候,可以使用点语法去访问另一个的属性,这样的使用,就形成了链式访问. 可空链式调用是一种可以请求和调用属性.方法及下表的过程 ...

  9. sqlserver中表变量和变量表之间区别

    sqlserver中表变量和变量表之间区别

  10. each的详解

    首先我还是先观察w3c讲解: 先写一段代码,如图: 定义:each() 方法规定为每个匹配元素规定运行的函数. 提示:返回 false 可用于及早停止循环.(我在代码中加了return false,发 ...