247. Segment Tree Query II
For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote the number of elements in the the array which value is between interval start and end. (The array may not fully filled by elements)
Design a query method with three parameters root, start and end, find the number of elements in the in array's interval [start, end] by the given root of value SegmentTree.
Example
Example 1:
Input:"[0,3,count=3][0,1,count=1][2,3,count=2][0,0,count=1][1,1,count=0][2,2,count=1][3,3,count=1]",[[1, 1], [1, 2], [2, 3], [0, 2]]
Output:[0,1,2,2]
Explanation:
The corresponding value Segment Tree is:
[0, 3, count=3]
/ \
[0,1,count=1] [2,3,count=2]
/ \ / \
[0,0,count=1] [1,1,count=0] [2,2,count=1], [3,3,count=1]
Input : query(1,1), Output: 0
Input : query(1,2), Output: 1
Input : query(2,3), Output: 2
Input : query(0,2), Output: 2
Example 2:
Input:"[0,3,count=3][0,1,count=1][2,3,count=2][0,0,count=1][1,1,count=0][2,2,count=0][3,3,count=1]",[[1, 1], [1, 2], [2, 3], [0, 2]]
Output:[0,0,1,1]
Explanation:
The corresponding value Segment Tree is:
[0, 3, count=2]
/ \
[0,1,count=1] [2,3,count=1]
/ \ / \
[0,0,count=1] [1,1,count=0] [2,2,count=0], [3,3,count=1]
Input : query(1,1), Output: 0
Input : query(1,2), Output: 0
Input : query(2,3), Output: 1
Input : query(0,2), Output: 1
Input test data (one parameter per line)How to understand a testcase?
解法1:
/**
* Definition of SegmentTreeNode:
* class SegmentTreeNode {
* public:
* int start, end, count;
* SegmentTreeNode *left, *right;
* SegmentTreeNode(int start, int end, int count) {
* this->start = start;
* this->end = end;
* this->count = count;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/*
* @param root: The root of segment tree.
* @param start: start value.
* @param end: end value.
* @return: The count number in the interval [start, end]
*/
int query(SegmentTreeNode * root, int start, int end) {
if (!root || start > end) return 0;
if (start <= root->start && end >= root->end) {
return root->count;
}
int mid = root->start + (root->end - root->start) / 2;
int leftCount = 0, rightCount = 0;
if (start <= mid) {
leftCount = query(root->left, start, min(mid, end));
}
if (end >= mid + 1) {
rightCount = query(root->right, max(start, mid + 1), end);
}
return leftCount + rightCount;
}
};
本文介绍了一种使用段树进行区间查询的高效算法。通过构建段树,每个节点存储区间内元素的数量,实现了快速查找指定区间内的元素计数。示例展示了如何通过递归查询根节点获取指定区间的元素数量。
60万+

被折叠的 条评论
为什么被折叠?



