*[Lintcode]Interval Sum区间最小数
2016-10-11 17:41
344 查看
Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers
Notice
We suggest you finish problem Segment
Tree Build, Segment Tree Query andSegment
Tree Modify first.
Example
For array
and queries
使用区间树解决。注意interval各有一部分处于root左侧和右侧的问题。
/**
* Definition of Interval:
* public classs Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
*/
public class Solution {
/**
*@param A, queries: Given an integer array and an query list
*@return: The result list
*/
public ArrayList<Long> intervalSum(int[] A,
ArrayList<Interval> queries) {
// write your code here
SegmentTreeNode node = build(A, 0, A.length - 1);
ArrayList<Long> res = new ArrayList<Long>();
for(Interval i : queries) {
res.add(find(node, i));
}
return res;
}
private Long find(SegmentTreeNode root, Interval i) {
if(root == null) return 0L;
if(i.start > root.end || i.end < root.start) return 0L;
if(i.start <= root.start && i.end >= root.end) {
return root.sum;
}
else {
//should caculate separately to deal with separate interval problem
Long left = find(root.left, i);
Long right = find(root.right, i);
return left + right;
}
}
private SegmentTreeNode build(int[] A, int start, int end) {
if(start > end) return null;
SegmentTreeNode root = new SegmentTreeNode(start, end);
if(start == end) {root.sum = (long)A[start]; return root;}
root.left = build(A, start, (start + end) / 2);
root.right = build(A, (start + end) / 2 + 1, end);
root.sum = root.left.sum + root.right.sum;
return root;
}
class SegmentTreeNode {
SegmentTreeNode left;
SegmentTreeNode right;
int start, end;
Long sum;
public SegmentTreeNode(int start, int end) {
this.start = start;
this.end = end;
}
}
}
[start, end]. For each query, calculate the sum number between index start and end in the given array, return the result list.
Notice
We suggest you finish problem Segment
Tree Build, Segment Tree Query andSegment
Tree Modify first.
Example
For array
[1,2,7,8,5],
and queries
[(0,4),(1,2),(2,4)], return
[23,9,20]
使用区间树解决。注意interval各有一部分处于root左侧和右侧的问题。
/**
* Definition of Interval:
* public classs Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
*/
public class Solution {
/**
*@param A, queries: Given an integer array and an query list
*@return: The result list
*/
public ArrayList<Long> intervalSum(int[] A,
ArrayList<Interval> queries) {
// write your code here
SegmentTreeNode node = build(A, 0, A.length - 1);
ArrayList<Long> res = new ArrayList<Long>();
for(Interval i : queries) {
res.add(find(node, i));
}
return res;
}
private Long find(SegmentTreeNode root, Interval i) {
if(root == null) return 0L;
if(i.start > root.end || i.end < root.start) return 0L;
if(i.start <= root.start && i.end >= root.end) {
return root.sum;
}
else {
//should caculate separately to deal with separate interval problem
Long left = find(root.left, i);
Long right = find(root.right, i);
return left + right;
}
}
private SegmentTreeNode build(int[] A, int start, int end) {
if(start > end) return null;
SegmentTreeNode root = new SegmentTreeNode(start, end);
if(start == end) {root.sum = (long)A[start]; return root;}
root.left = build(A, start, (start + end) / 2);
root.right = build(A, (start + end) / 2 + 1, end);
root.sum = root.left.sum + root.right.sum;
return root;
}
class SegmentTreeNode {
SegmentTreeNode left;
SegmentTreeNode right;
int start, end;
Long sum;
public SegmentTreeNode(int start, int end) {
this.start = start;
this.end = end;
}
}
}
相关文章推荐
- *[Lintcode]Interval Minimum Number 区间最小数
- [Lintcode]Interval Sum II区间求和 II
- LintCode:区间最小数
- lintcode-medium-Interval Sum
- python_lintcode_177. 把排序数组转换为高度最小的二叉搜索树_156. 合并区间
- lintcode(205)区间最小数
- Lintcode---区间最小数
- 区间最小数-LintCode
- Lintcode: Interval Sum II
- lintcode-区间最小数-205
- LintCode_207 interval-sum-ii
- lintcode-205-区间最小数
- HDU 5749 Colmerauer(单调栈或单调队列求解固定区间最大或最小值)
- 【HDU 5381】 The sum of gcd (子区间的xx和,离线)
- poj3264 Balanced Lineup(求区间的最大值与最小值之差)
- CodeForces 703D Mishka and Interesting sum (树状数组 区间偶数异或)
- 洛谷 P1440 求m区间内的最小值
- [LintCode] 二叉树的路径之和 Binary Tree Path Sum
- LintCode 376: Binary Tree Path Sum
- Qwt源码解读之区间类——QwtInterval