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 rootstart and end, find the maximum number in the interval [start, end] by the given root of segment tree.

Example

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 [startend] by the given root of value SegmentTree.

Example

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的更多相关文章

  1. Lintcode: Segment Tree Query II

    For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...

  2. 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 ...

  3. Segment Tree Build I & II

    Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...

  4. 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 ...

  5. 247. Segment Tree Query II

    最后更新 二刷 09-Jna-2017 利用线段树进行区间查找,重点还是如何判断每一层的覆盖区间,和覆盖去见与当前NODE值域的关系. public class Solution { public i ...

  6. 202. Segment Tree Query

    最后更新 二刷 09-Jan-17 正儿八经线段树的应用了. 查找区间内的值. 对于某一个Node,有这样的可能: 1)需要查找区间和当前NODE没有覆盖部分,那么直接回去就行了. 2)有覆盖的部分, ...

  7. Lintcode: Segment Tree Modify

    For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...

  8. 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 ...

  9. [LintCode] Segment Tree Build II 建立线段树之二

    The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...

随机推荐

  1. Callable、Future和FutureTask

    创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口.这2种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果. 如果需要获取执行结果,就必须通过共享变量或者使用线 ...

  2. 【转】Oracle集合操作函数:union、intersect、minus

    集合操作符专门用于合并多条select 语句的结果,包括:UNION, UNION ALL, INTERSECT, MINUS.当使用集合操作符时,必须确保不同查询的列个数和数据类型匹配. 集合操作符 ...

  3. Java基础-基本数据类型转换案例

    java基本数据类型八中 byte = Byte short = Short char = Character int = Integer long = Long float = Float doub ...

  4. 【POJ 1061】青蛙的约会

    题 Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要 ...

  5. 21.Android之SQLite数据库学习

    Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQLite 只需要带一个动 ...

  6. 洛谷U5653 宋荣子的小饼干

    题目描述 楼下机房的LYL有n个妹子,分别编号为a1,a2……an,每个妹子都拥有一定数量的小饼干.有一天,saruka没有吃晚饭,饿的不要不要的,这时,他忽然想起了LYL的妹子们有小饼干可以吃.于是 ...

  7. CODEVS1995 || TYVJ1863 黑魔法师之门

    P1863 [Poetize I]黑魔法师之门 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 经过了16个工作日的紧张忙碌,未来的人类终于收集到了足够的能源 ...

  8. Mysql安全配置

    相关学习资料 http://drops.wooyun.org/tips/2245 http://www.cnblogs.com/siqi/archive/2012/11/21/2780966.html ...

  9. 我所了解的javaScript细节

    变量转换 var myVar = "3.14159", str = ""+ myVar,// to string int = ~~myVar, // to in ...

  10. boost解析json(2)

    "{ "A":1, "B":{ "C":2, "D":3 }, "E":[ {" ...