Given an integer array in the construct method, implement two methods query(start, end) and modify(index, value):

For query(start, end), return the sum from index start to index end in the given array.
For modify(index, value), modify the number in the given index to value
Have you met this question in a real interview? Yes
Example
Given array A = [1,2,7,8,5]. query(0, 2), return 10.
modify(0, 4), change A[0] from 1 to 4.
query(0, 1), return 6.
modify(2, 1), change A[2] from 7 to 1.
query(2, 4), return 14.
Note
We suggest you finish problem Segment Tree Build, Segment Tree Query and Segment Tree Modify first. Challenge
O(logN) time for query and modify.

Segment Tree:

 public class Solution {
/* you may need to use some attributes here */
class SegmentTreeNode {
long sum;
int start;
int end;
SegmentTreeNode left;
SegmentTreeNode right;
SegmentTreeNode(int start, int end) {
this.sum = 0;
this.start = start;
this.end = end;
this.left = null;
this.right = null;
}
} SegmentTreeNode root; /**
* @param A: An integer array
*/
public Solution(int[] A) {
// write your code here
if (A == null || A.length==0) return;
root = build(A, 0, A.length-1); } public SegmentTreeNode build(int[] A, int start, int end) {
SegmentTreeNode cur = new SegmentTreeNode(start, end);
if (start == end) cur.sum = A[start];
else {
int mid = (start + end)/2;
cur.left = build(A, start, mid);
cur.right = build(A, mid+1, end);
cur.sum = cur.left.sum + cur.right.sum;
}
return cur;
} /**
* @param start, end: Indices
* @return: The sum from start to end
*/
public long query(int start, int end) {
// write your code here
return queryTree(root, start, end);
} public long queryTree(SegmentTreeNode cur, int start, int end) {
if (cur.start==start && cur.end==end) return cur.sum;
int mid = (cur.start + cur.end)/2;
if (end <= mid) return queryTree(cur.left, start, end);
else if (start > mid) return queryTree(cur.right, start, end);
else return queryTree(cur.left, start, mid) + queryTree(cur.right, mid+1, end);
} /**
* @param index, value: modify A[index] to value.
*/
public void modify(int index, int value) {
// write your code here
modifyTree(root, index, value);
} public void modifyTree(SegmentTreeNode cur, int index, int val) {
if (cur.start == cur.end) {
cur.sum = val;
return;
}
int mid = (cur.start + cur.end)/2;
if (index <= mid) modifyTree(cur.left, index, val);
else modifyTree(cur.right, index, val);
cur.sum = cur.left.sum + cur.right.sum;
}
}

Lintcode: Interval Sum II的更多相关文章

  1. Lintcode: k Sum II

    Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where the ...

  2. Lintcode: Interval Sum

    Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...

  3. LintCode: Combination Sum II

    C++ DFS class Solution { public: void help(vector<int> &a, int now, int sum, int target, v ...

  4. LintCode "Subarray Sum II"

    Sliding window doesn't work. So it is a typical partial_sum base solution. As below. However if you ...

  5. Interval Sum I && II

    Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...

  6. [LintCode] Subarray Sum & Subarray Sum II

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

  7. Continuous Subarray Sum II(LintCode)

    Continuous Subarray Sum II   Given an circular integer array (the next element of the last element i ...

  8. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  9. Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

随机推荐

  1. 类库引用EF

    安装EntityFramework 添加引用system.data.entity;

  2. 【php学习】数组操作

    1.json字符串和数组之间的转换 json_decode($jsonStr)      json字符串解码为php变量,若第二个参数默认为false,返回一个对象:若第二个参数设置true,则返回一 ...

  3. mongodb 常用命令

    转自http://www.blogjava.net/waterjava/archive/2011/03/23/346819.html 命令行 --help 显示命令行参数 --nodb 不连接数据库方 ...

  4. MySQL- 锁(3)

    InnoDB在不同隔离级别下的一致性读及锁的差异 前面讲过,锁和多版本数据是InnoDB实现一致性读和ISO/ANSI SQL92隔离级别的手段,因此,在不同的隔离级别下,InnoDB处理SQL时采用 ...

  5. is not configured for rpc

    exec sp_serveroption @server='myserver', @optname='rpc', @optvalue='true' exec sp_serveroption @serv ...

  6. 设计模式:适配器模式(Adapter)

    定  义:将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 有些国家用110V电压,而我们国家用的是220V,但是我们的电器,比 ...

  7. Android OpenGL ES 开发教程 从入门到精通

    感谢,摘自:http://blog.csdn.net/mapdigit/article/details/7526556 Android OpenGL ES 简明开发教程 Android OpenGL ...

  8. .SQL Server中 image类型数据的比较

    原文:.SQL Server中 image类型数据的比较 在SQL Server中如果你对text.ntext或者image数据类型的数据进行比较.将会提示:不能比较或排序 text.ntext 和 ...

  9. magento 切换数据库,使用不同数据库

    1. 在app/etc/local.xml 中,添加新的数据库选项 <?xml version="1.0"?> <config> <global> ...

  10. linux 多个ssh key 登录远程主机

    登录A主机使用分配的sshkey需要指定特定的key,ssh -i  ~/.ssh/xxx_id_rsa_privatekey    username@host , 指定key后登录提示: Agent ...