Leetcode: Range Sum Query - Mutable && Summary: Segment Tree
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的更多相关文章
- [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- [LeetCode] Range Sum Query - Mutable 题解
题目 题目 思路 一看就是单点更新和区间求和,故用线段树做. 一开始没搞清楚,题目给定的i是从0开始还是从1开始,还以为是从1开始,导致后面把下标都改掉了,还有用区间更新的代码去实现单点更新,虽然两者 ...
- [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 ...
- LeetCode Range Sum Query 2D - Mutable
原题链接在这里:https://leetcode.com/problems/range-sum-query-2d-mutable/ 题目: Given a 2D matrix matrix, find ...
- [Leetcode Week16]Range Sum Query - Mutable
Range Sum Query - Mutable 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/range-sum-query-mutable/de ...
- 【刷题-LeetCode】307. Range Sum Query - Mutable
Range Sum Query - Mutable Given an integer array nums, find the sum of the elements between indices ...
- [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 ...
- [LeetCode] Range Sum Query - Immutable 区域和检索 - 不可变
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- 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 ...
随机推荐
- MySQL优化常用
一.mysql的配置都是小写的,使用下划线_或破折号-分割单词,两者是一样的二.在配置文件中可以用1m,1g等单位,但是用set命令,不能使用单位,默认单位是字节三.特殊例子a.query_cache ...
- P1083 借教室
思路:前缀和, c表示对于当前的middle, 前缀和 #include <bits/stdc++.h> using namespace std; const int maxn = 1e6 ...
- http://d3js.org/
http://d3js.org/ http://www.ourd3js.com/wordpress/?p=51 http://www.ourd3js.com/wordpress/?p=104file: ...
- PHP基础语法: echo,var_dump, 常用函数:随机数:拆分字符串:explode()、rand()、日期时间:time()、字符串转化为时间戳:strtotime()可变参数的函数:PHP里数组长度表示方法:count($attr[指数组]);字符串长度:strlen($a)
PHP语言原理:先把代码显示在源代码中,再通过浏览器解析在网页上 a. 1.substr; //用于输出字符串中,需要的某一部分 <?PHP $a="learn php"; ...
- QGridLayout--01
#include "mainwindow.h" #include <QApplication> #include<QLabel> #include<Q ...
- 20145211 《Java程序设计》实验报告一:Java开发环境的熟悉(Windows+IDEA)
实验要求 使用JDK编译.运行简单的Java程序: 使用IDEA 编辑.编译.运行.调试Java程序. 实验内容 命令行下Java程序开发 IDEA下Java程序开发.调试 练习(通过命令行和IDEA ...
- LightOj 1098 - A New Function(求1-n所有数的因子和)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1098 题意:给你一个数n (0 ≤ n ≤ 2 * 109),求n以内所有数的因子和, ...
- recordcount
rs.recordcount 有时不能取到数,这时 要更改游标为客户端游标 .
- Java学习-026-类名或方法名应用之二 -- 统计分析基础
前文讲述了类名或方法的应用之一调试源码,具体请参阅:Java学习-025-类名或方法名应用之一 -- 调试源码 此文主要讲述类名或方法应用之二统计分析,通过在各个方法中插桩(调用桩方法),获取方法的调 ...
- Android Annotations 注解例子
1.AndroidAnnotations官网: http://androidannotations.org/ (也许你需要FQ) 2.eclipse中使用androidannotations的配置方法 ...