247 - 线段树查询 II
2017-09-08 14:13
232 查看
2017.9.8
注意处理一下,当给定的区间超出原区间范围时的情况。
也就是这里的这种情况。剩下的就没有什么问题了。
if(start < sNode.start){
start = sNode.start;
}
if(end > sNode.end){
end = sNode.end;
}
/**
* Definition of SegmentTreeNode:
* public class SegmentTreeNode {
* public int start, end, count;
* public SegmentTreeNode left, right;
* public SegmentTreeNode(int start, int end, int count) {
* this.start = start;
* this.end = end;
* this.count = count;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/*
* @param root: The root of segment tree.
* @param start: start value.
* @param end: end value.
* @return: The count number in the interval [start, end]
*/
public int query(SegmentTreeNode root, int start, int end) {
// write your code here
if(root == null || root.count == 0){
return 0;
}
int count = 0;
SegmentTreeNode sNode = root;
int mid = (sNode.end + sNode.start)/2;
if(start < sNode.start){
start = sNode.start;
}
if(end > sNode.end){
end = sNode.end;
}
if(start == sNode.start && end == sNode.end){
return count + sNode.count;
}
if(end <= mid){
return count + query(sNode.left,start,end);
}
if(start > mid){
return count + query(sNode.right,start,end);
}
else{
return count + query(sNode.left,start,mid) + query(sNode.right,mid + 1, end);
}
}
/*
public int query(SegmentTreeNode root, int start, int end) {
int count = 0;
count = query(root,start,end,count);
return count;
}
*/
}
注意处理一下,当给定的区间超出原区间范围时的情况。
也就是这里的这种情况。剩下的就没有什么问题了。
if(start < sNode.start){
start = sNode.start;
}
if(end > sNode.end){
end = sNode.end;
}
/**
* Definition of SegmentTreeNode:
* public class SegmentTreeNode {
* public int start, end, count;
* public SegmentTreeNode left, right;
* public SegmentTreeNode(int start, int end, int count) {
* this.start = start;
* this.end = end;
* this.count = count;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/*
* @param root: The root of segment tree.
* @param start: start value.
* @param end: end value.
* @return: The count number in the interval [start, end]
*/
public int query(SegmentTreeNode root, int start, int end) {
// write your code here
if(root == null || root.count == 0){
return 0;
}
int count = 0;
SegmentTreeNode sNode = root;
int mid = (sNode.end + sNode.start)/2;
if(start < sNode.start){
start = sNode.start;
}
if(end > sNode.end){
end = sNode.end;
}
if(start == sNode.start && end == sNode.end){
return count + sNode.count;
}
if(end <= mid){
return count + query(sNode.left,start,end);
}
if(start > mid){
return count + query(sNode.right,start,end);
}
else{
return count + query(sNode.left,start,mid) + query(sNode.right,mid + 1, end);
}
}
/*
public int query(SegmentTreeNode root, int start, int end) {
int count = 0;
count = query(root,start,end,count);
return count;
}
*/
}
相关文章推荐
- lintocde-247-线段树的查询 II
- lintcode(247)线段树查询 II
- lintcode-线段树查询II-247
- <LeetCode> 题247:线段树的查询(2)
- 线段树查询 II-LintCode
- SPOJ GSS2 - Can you answer these queries II(线段树 区间修改+区间查询)(后缀和)
- 线段树查询 II
- fzu 2171 防守阵地 II(线段树,成段更新,查询一段区间和)
- LintCode:线段树的查询II
- 【线段树II:区间修改+点查询】hdu 1556 Color the ball
- FOJ 2171 防守阵地 II 区间求和区间查询 线段树
- 【线段树II:区间修改+点查询】hdu 4031 Attack
- 解题报告:线段树查询 II
- 线段树查询 II
- 线段树查询 II
- POJ 4047 Garden (线段树 - 区间增减、区间查询) -- 解题报告
- Points in Segments (II)(线段树)
- HDOJ 3308 LCIS(线段树区间合并与查询)
- HDU 4267 线段树 离散点区间更新, 自叶子节点至根单点查询
- <LeetCode> 题202:线段树的查询