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:

  1. The array is only modifiable by the update function.
  2. You may assume the number of calls to update and sumRange function is distributed evenly
public class NumArray {
private SegmentTreeNode root = null;
private int size = 0; public NumArray(int[] nums) {
root = buildInSegmentTree(nums, 0, nums.length - 1);
size = nums.length;
} void update(int i, int val) {
if(i<0 || i>=size) return;
updateInSegmentTree(root, i, val);
} public int sumRange(int i, int j) {
if(i>j || i<0 || j>=size) return -1;
return querySum(root, i, j);
} class SegmentTreeNode{
int lc = 0, rc = 0, sum = 0;
SegmentTreeNode left = null, right = null;
SegmentTreeNode(int l, int r, int val) {
lc = l; rc = r; sum = val;
}
} public SegmentTreeNode buildInSegmentTree(int []nums, int l, int r) {
if(l > r) return null; if(l == r) {
SegmentTreeNode leaf = new SegmentTreeNode(l, r, nums[l]);
return leaf;
} SegmentTreeNode root = new SegmentTreeNode(l, r, 0);
int mid = (l + r) >> 1;
root.left = buildInSegmentTree(nums, l, mid);
root.right = buildInSegmentTree(nums, mid+1, r);
root.sum = root.left.sum + root.right.sum; return root;
} public void updateInSegmentTree(SegmentTreeNode root, int i, int val) {
if(root.lc == root.rc && root.lc == i) {
root.sum = val;
return;
} int mid = (root.lc + root.rc) >> 1;
if(i >= root.lc && i <= mid) updateInSegmentTree(root.left, i, val);
else updateInSegmentTree(root.right, i, val);
root.sum = root.left.sum + root.right.sum;
} public int querySum(SegmentTreeNode root, int i, int j) {
if(root.lc == i && root.rc == j) return root.sum; int mid = (root.lc + root.rc) >> 1;
if(i <= mid && j <= mid) return querySum(root.left, i, j);
else if(i > mid && j > mid) return querySum(root.right, i, j);
else return querySum(root.left, i, mid) + querySum(root.right, mid+1, j);
}
} // 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);

下面附上java版segmentTree模板代码(共有两个文件:一个是SegmentTreeNode.java,另一个是SegmentTree.java。)

package cc150;

public class SegmentTreeNode {
public int lc, rc, sum, add;
SegmentTreeNode left, right; public SegmentTreeNode() {
this.lc = 0; this.rc = 0; this.sum = 0; this.add = 0;
this.left = null; this.right = null;
} public SegmentTreeNode(int l, int r, int val) {
this.lc = l; this.rc = r; this.sum = val; this.add = 0;
this.left = null; this.right = null;
} public static void main(String[] args) {
// TODO Auto-generated method stub } }

SegmentTreeNode.java

package cc150;

public class SegmentTree {
public SegmentTreeNode root = null;
int lower_bound, upper_bound; public SegmentTree() {
this.root = null;
this.lower_bound = 0; this.upper_bound = 0;
} public SegmentTree(int l, int r, int []nums) {
//@ SegmentTreeNode(left_idx, right_idx, sum).
this.root = new SegmentTreeNode(l, r, 0);
this.lower_bound = l; this.upper_bound = r;
buildSegmentTree(l, r, nums, root);
} public void buildSegmentTree(int l, int r, int []nums, SegmentTreeNode s) {
SegmentTreeNode sroot = s;
if(l > r) return; if(l == r) {
sroot.sum = nums[l];
return;
} int mid = (l + r) / 2;
sroot.left = new SegmentTreeNode(l, mid, 0);
buildSegmentTree(l, mid, nums, sroot.left); sroot.right = new SegmentTreeNode(mid+1, r, 0);
buildSegmentTree(mid+1, r, nums, sroot.right); sroot.sum = sroot.left.sum + sroot.right.sum; } public void updateByPoint(SegmentTreeNode sroot, int idx, int val) {
if(idx == sroot.lc && sroot.lc == sroot.rc) {
sroot.sum = val;
return;
} int mid = (sroot.lc + sroot.rc) / 2;
if(idx <= mid) updateByPoint(sroot.left, idx, val);
else updateByPoint(sroot.right, idx, val); sroot.sum = sroot.left.sum + sroot.right.sum;
} public void updateBySegment(SegmentTreeNode sroot, int l, int r, int val) {
if(l == sroot.lc && r == sroot.rc) {
sroot.add += val;
sroot.sum += val * (r - l + 1);
return;
} if(sroot.lc == sroot.rc) return;
int len = sroot.rc - sroot.lc + 1;
if(sroot.add > 0) {
sroot.left.add += sroot.add;
sroot.right.add += sroot.add;
sroot.left.sum += sroot.add * (len - (len/2));
sroot.right.sum += sroot.add * (len/2);
sroot.add = 0;
} int mid = sroot.lc + (sroot.rc - sroot.lc)/2;
if(r <= mid) updateBySegment(sroot.left, l, r, val);
else if(l > mid) updateBySegment(sroot.right, l, r, val);
else {
updateBySegment(sroot.left, l, mid, val);
updateBySegment(sroot.right, mid+1, r, val);
} sroot.sum = sroot.left.sum + sroot.right.sum;
} static int querySum(SegmentTreeNode sroot, int i, int j) {
if(i > j) {
System.out.println("Invalid Query!");
return -1;
}
if(i<sroot.lc || j>sroot.rc) return querySum(sroot, sroot.lc, sroot.rc); if(sroot.lc == i && sroot.rc == j) return sroot.sum;/*
int len = sroot.rc - sroot.lc + 1;
if(sroot.add > 0) {
sroot.left.add += sroot.add;
sroot.right.add += sroot.add;
sroot.left.sum += sroot.add * (len - len/2);
sroot.right.sum += sroot.add * (len/2);
sroot.add = 0;
}
*/
int mid = (sroot.lc + sroot.rc) / 2; if(j <= mid) return querySum(sroot.left, i, j);
else if(i > mid) return querySum(sroot.right, i, j);
else return querySum(sroot.left, i, mid) + querySum(sroot.right, mid+1, j);
} public static void main(String[] args) {
// TODO Auto-generated method stub
int []nums = new int[10];
for(int i=0;i<nums.length;++i) nums[i] = i; SegmentTree st = new SegmentTree(0, nums.length-1, nums);
int tmp = querySum(st.root, 0, 9);
System.out.println(tmp); st.updateByPoint(st.root, 5, 7);
System.out.println(querySum(st.root, 0, 9)); st.updateBySegment(st.root, 3, 4, 2);
System.out.println(querySum(st.root, 2, 7));
} }

SegmentTree.java

leetcode@ [307] Range Sum Query - Mutable / 线段树模板的更多相关文章

  1. [LeetCode] 307. 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] 307. Range Sum Query - Mutable 解题思路

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

  3. LeetCode - 307. Range Sum Query - Mutable

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

  4. leetcode 307. Range Sum Query - Mutable(树状数组)

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

  5. Leetcode 2——Range Sum Query - Mutable(树状数组实现)

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

  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 Week16]Range Sum Query - Mutable

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

  8. 【leetcode】307. Range Sum Query - Mutable

    题目如下: 解题思路:就三个字-线段树.这个题目是线段树用法最经典的场景. 代码如下: class NumArray(object): def __init__(self, nums): " ...

  9. 307. Range Sum Query - Mutable

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

随机推荐

  1. 【BZOJ 1185】 凸包+旋转卡壳

    Description [分析] 打计算几何真的可以哭出来... 跟那个求线段最远点差不多,这题弄三个东西转一转,一个表示左端最远点,一个表示右端最远点,一个表示上面最远点. 左右两边的最远点用点积判 ...

  2. utf-8转换为ansi和修改文件名的批处理(可解决source insight中文注释乱码问题)

    source insight中文乱码有两个原因,一个是source insight的设置不正确.另外一个原因是源文件是utf-8格式的. 最近在工作中用source insight 查看jsp文件.j ...

  3. nchar 和 nvarchar

    字符数据类型(nchar 长度固定,nvarchar 长度可变)和 Unicode 数据使用 UNICODE UCS-2 字符集. nchar [ ( n ) ] n 个字符的固定长度的 Unicod ...

  4. WCF的行为与异常-------配置文件说明

    ServiceBehavior and OperationBehavior(这些都是应用在实现类上) http://msdn.microsoft.com/zh-cn/library/system.se ...

  5. libevent入门教程

    首先给出官方文档吧: http://libevent.org ,首页有个Programming with Libevent,里面是一节一节的介绍libevent,但是感觉信息量太大了,而且还是英文的- ...

  6. Call to undefined function pg_

    网上普遍的解决方案: 1.修改php.ini文件, 添加php_pgsql.dll扩展 2.如果是wamp这样类似的软件,可以直接通过图形化操作 这样操作后,大部分RD都是没有问的...但是为什么还提 ...

  7. jquery获取元素索引值index()方法

    jquery的index()方法 搜索匹配的元素,并返回相应元素的索引值,从0开始计数. 如果不给 .index() 方法传递参数,那么返回值就是这个jQuery对象集合中第一个元素相对于其同辈元素的 ...

  8. C语言中指针数组和数组指针的区别

    指针数组:首先它是一个数组,数组的元素都是指针,数组占多少个字节由数组本身决定.它是“储存指针的数组”的简称. 数组指针:首先它是一个指针,它指向一个数组.在32 位系统下永远是占4 个字节,至于它指 ...

  9. oh my zsh命令

    打开某个文件夹地址,输入 cdf   命令,会自动进入这个文件夹命令行 open ./      打开当前命令行所在目录的文件夹

  10. C++ volatile的作用

    volatile的作用     2006-10-23 13:44:21 大 中 小 关键在于两个地方:     1. 编译器的优化 (请高手帮我看看下面的理解) 在本次线程内, 当读取一个变量时,为提 ...