最后更新

二刷

09-Jan-17

正儿八经线段树的应用了。 查找区间内的值。

对于某一个Node,有这样的可能:

1)需要查找区间和当前NODE没有覆盖部分,那么直接回去就行了。

2)有覆盖的部分,覆盖部分作为新的查找区间,往左右子找。

Time: O(NlgN)

Space: O(lgN) for memory stack

public class Solution {
public int query(SegmentTreeNode root, int start, int end) {
if (root == null) return Integer.MIN_VALUE;
if (end < root.start || start > root.end) return Integer.MIN_VALUE; int newStart = Math.max(root.start, start);
int newEnd = Math.min(root.end, end); if (newStart == root.start && newEnd == root.end) return root.max; return Math.max(query(root.left, newStart, newEnd),
query(root.right, newStart, newEnd));
}
}

202. Segment Tree Query的更多相关文章

  1. 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), ...

  2. Lintcode: Segment Tree Query II

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

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

  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. Lintcode: Segment Tree Modify

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

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

  8. BestCoder#16 A-Revenge of Segment Tree

    Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...

  9. HDU5086——Revenge of Segment Tree(BestCoder Round #16)

    Revenge of Segment Tree Problem DescriptionIn computer science, a segment tree is a tree data struct ...

随机推荐

  1. Format a Hard Drive in Csharp

    Article Author(s): Audric Thevenet All Rights Reserved. Here's how to format hard drives, floppies, ...

  2. linux查看内存和释放内存

    linux: 查看内存:free -m 释放内存:echo 1 > /proc/sys/vm/drop_caches

  3. vue的使用配置

    我的编辑器是webstorm,虽然占内容占资源, 但是用起来很方便, 刚开始接触的时候就是用这个软件,很喜欢. vue的教程 1.http://www.jianshu.com/p/5ba253651c ...

  4. Vue通信、传值的多种方式,详解

    Vue通信.传值的多种方式,详解 转自:https://blog.csdn.net/qq_35430000/article/details/79291287 一.通过路由带参数进行传值 ①两个组件 A ...

  5. 洛谷 P2858 奶牛零食

    https://www.luogu.org/problemnew/show/P2858 毫无疑问区间dp. ![区间dp入门] 我们定义dp[i][j]表示从i到j的最大收益,显然我们需要利用比较小的 ...

  6. ps指令详解

    ps aux #显示出系统上的全部进程ps -ef #显示出系统上的全部进程,且显示出PPID一栏ps -ljF #仅显示与本终端上开启的进程 选项:-t 终端名称1 终端名称2 #指定关联的多个终端 ...

  7. 快速部署jumpserver堡垒机

    jumpserver版本:Version 1.4.1-2 (社区版) 主机IP地址:10.0.0.105 准备环境1.安装依赖yum -y install wget sqlite-devel xz g ...

  8. 【JDBC-MVC模式】开发实例

    JDBC - 开发实例-MVC模式  1. 在web.xml中配置连接数据库的信息 web.xml: <context-param> <param-name>server< ...

  9. js 对象细节

    原型和原型链 在对象自身身上找不到指定属性时,就会到这个对象的原型__proto__上找,原型也是指向一个对象,在这个对象上还找不到对应属性,则继续到原型上来找...以上过程形成原型链. 访问对象的原 ...

  10. tensorflow with gpu 环境配置

    1.准备工作 1.1 确保GPU驱动已经安装 lspci | grep -i nvidia 通过此命令可以查看GPU信息,测试机已经安装GPU驱动