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. Error in notifier

    sudo apt-get install libnotify-bin or 在gulpfile.js第一行插入 process.env.DISABLE_NOTIFIER = true; 禁用notif ...

  2. Rice Rock

    先翻译评分要点,然后一点点翻译程序实现过程 如何产生一堆岩石? rock_group = set([])#空集合,全局变量   rock_group.add(a_rock) 要画出来draw hand ...

  3. find principles

    Computer Science An Overview _J. Glenn Brookshear _11th Edition In this chapter we explore the probl ...

  4. Java高级之虚拟机加载机制

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 1.0版本:2016-05-21 SubClass!! 执行结果说明一个问题:子类调用父类变量的时候 ...

  5. 安装Postman

    原文地址:http://blog.csdn.net/ouyang111222/article/details/45743831 ** (一)安装篇 ** Postman是一款功能强大的网页调试与发送网 ...

  6. ios证书

    内容提要: 安装app时提示 “无法下载应用,此时无法安装“XXX””.我遇到过多次是由于ios的app出现证书问题.本篇文章讲解用ios证书制作过程,以及每个步骤的解释. 正文: Xcode签名至少 ...

  7. ArcGIS API for Silverlight 绘制降雨路径动画

    原文:ArcGIS API for Silverlight 绘制降雨路径动画 #region 降雨动画演示 2014-04-16 List<Graphic> graphics = new ...

  8. NAT原理与NAT穿越

    最近在看东西的时候发现很多网络程序中都需要NAT穿越,特意在此总结一下. 先做一个约定: 内网A中有:A1(192.168.0.8).A2(192.168.0.9)两用户 网关X1(一个NAT设备)有 ...

  9. mongo安装、备份与常见命令整理

    http://zlboy888.blog.163.com/blog/static/315357072012919241104/ 1 下载安装包  官方下载地址:http://www.mongodb.o ...

  10. imx6 启动 init进程

    之前不知道imx6内核是怎么启动文件系统的init进程,查了下资料,记录于此,以后再来补充. kernel/init/main.c static noinline int init_post(void ...