Segment Tree Query I & II
Segment Tree Query I
For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding SegmentTree, each node stores an extra attribute max
to denote the maximum number in the interval of the array (index from start to end).
Design a query
method with three parameters root
, start
and end
, find the maximum number in the interval [start, end] by the given root of segment tree.
For array [1, 4, 2, 3]
, the corresponding Segment Tree is:
[0, 3, max=4]
/ \
[0,1,max=4] [2,3,max=3]
/ \ / \
[0,0,max=1] [1,1,max=4] [2,2,max=2], [3,3,max=3]
query(root, 1, 1), return 4
query(root, 1, 2), return 4
query(root, 2, 3), return 3
query(root, 0, 2), return 4
/**
* Definition of SegmentTreeNode:
* public class SegmentTreeNode {
* public int start, end, max;
* public SegmentTreeNode left, right;
* public SegmentTreeNode(int start, int end, int max) {
* this.start = start;
* this.end = end;
* this.max = max
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
*@param root, start, end: The root of segment tree and
* an segment / interval
*@return: The maximum number in the interval [start, end]
*/
public int query(SegmentTreeNode root, int start, int end) {
if (root == null || root.start > end || root.end < start) return Integer.MIN_VALUE; if (root.start == start && root.end == end) return root.max; int mid = (root.start + root.end) / ;
if (start >= mid + ) {
return query(root.right, start, end);
} else if (end <= mid) {
return query(root.left, start, end);
} else {
return Math.max(query(root.left, start, mid), query(root.right, mid + , end));
} }
}
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.
For array [0, 2, 3]
, 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]
query(1, 1)
, return 0
query(1, 2)
, return 1
query(2, 3)
, return 2
query(0, 2)
, return 2
/**
* 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, start, end: The root of segment tree and
* an segment / interval
*@return: The count number in the interval [start, end]
*/
public int query(SegmentTreeNode root, int start, int end) {
if (root == null || root.start > end || root.end < start) return ; // alert, special case
if (end > root.end) end = root.end;
if (start < root.start) start = root.start; if (root.start == start && root.end == end) return root.count; int mid = (root.start + root.end) / ; if (end <= mid) {
return query(root.left, start, end);
} else if (start >= mid + ) {
return query(root.right, start, end);
} else {
return query(root.right, mid + , end) + query(root.left, start, mid);
}
}
}
Segment Tree Query I & II的更多相关文章
- Lintcode: Segment Tree Query II
For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...
- Lintcode247 Segment Tree Query II solution 题解
[题目描述] For an array, we can build a Segment Tree for it, each node stores an extra attribute count t ...
- Segment Tree Build I & II
Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...
- Lintcode: Segment Tree Query
For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding ...
- 247. Segment Tree Query II
最后更新 二刷 09-Jna-2017 利用线段树进行区间查找,重点还是如何判断每一层的覆盖区间,和覆盖去见与当前NODE值域的关系. public class Solution { public i ...
- 202. Segment Tree Query
最后更新 二刷 09-Jan-17 正儿八经线段树的应用了. 查找区间内的值. 对于某一个Node,有这样的可能: 1)需要查找区间和当前NODE没有覆盖部分,那么直接回去就行了. 2)有覆盖的部分, ...
- Lintcode: Segment Tree Modify
For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...
- 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 ...
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
随机推荐
- 【POJ 2104】 K-th Number 主席树模板题
达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没 ...
- 基于Bootstrap的jQuery开关按钮插件
按钮 下载 使用方法 首先要在页面中引入依赖文件: jquery.Bootstrap.Bootstrap Switch CSS和Bootstrap Switch JS.这里用的是bootstr ...
- POJ2186 Popular Cows
Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= ...
- Redis 集合操作
1.SCARD key 返回集合 key 的基数(集合中元素的数量). 2.SDIFFSTORE destination key [key ...] 这个命令的作用和 类似,但它将结果保存到 des ...
- 2016年4月7日 js的全选和反选
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title> ...
- Python序列的切片操作与技巧
切片操作 对于具有序列结构的数据来说,切片操作的方法是:consequence[start_index: end_index: step]. start_index: 表示是第一个元素对象,正索引位置 ...
- iOS系统navigationBar背景色,文字颜色处理
- (void)setRightBarButtonItem { // Create done Button UIBarButtonItem *doneButton = [[UIBarButtonIte ...
- WIN7下使用VC2010调试uCOS-II 2.91
WIN7下使用VC2010调试uCOS-II 2.91 http://www.amobbs.com/thread-5462878-1-1.html ucos系统学习汇总 http://www.cnbl ...
- GDB中应该知道的几个调试方法 来自陈皓
GDB中应该知道的几个调试方法 2011年2月10日陈皓发表评论阅读评论62,325 人阅读 七.八年前写过一篇<用GDB调试程序>,于是,从那以后,很多朋友在MSN上以及给我发邮件询 ...
- c/c++指针
指针主要分: 指向单一的某个对象/变量, 用于多态或函数指针, 这个不难 - 一级指针 其次是指向数组, 用来操作/遍历数组元素 - 一级/二级指针 指向数组的一级指针很简单了: p指向的是数组的元素 ...