最后更新

二刷

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. day24-2 单例模式

    目录 单例模式 类内部定义静态方法实现单例模式 装饰器实现单例模式 元类实现单例模式 单例模式 单例模式:基于某种方法实例化多次得到实例是同一个 当实例化多次得到的对象中存放的属性都一样的情况,应该将 ...

  2. Python3 OpenCV3 图像处理基础

    开发环境搭建 本人使用的是Ubuntu 16.04LTS. 1.安装Python3 ## 其实 Ubuntu 16.04 系统自带了 Python 3.5.2,因此不需要再安装了?但是需要安装一些开发 ...

  3. $.noconflict() 有什么用处

    jQuery默认使用"$"操作符,prototype等其他框架也是是使用"$",于是,如果jQuery在其他库之后引入,那么jQuery将获得"$&q ...

  4. eject - 弹出可移动介质

    SYNOPSIS(总览) eject -h.breject [-vnrsfq] [<name>] eject [-vn] -d.breject [-vn] -a on|off|1|0 [& ...

  5. DROP TABLE - 删除一个表

    SYNOPSIS DROP TABLE name [, ...] [ CASCADE | RESTRICT ] DESCRIPTION 描述 DROP TABLE 从数据库中删除表或视图. 只有其所有 ...

  6. 进程池_Pool

    当需要创建子进程数量不多的时候,可以直接利用multiprocessing中的Process动态生成多个进程 但是如果是成百甚至上千个任务,手动地创建它的工作量很大,此时就可以利用到multiproc ...

  7. 多线程test

    import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; import ...

  8. 如何用纯 CSS 创作一种有削铁如泥感觉的菜单导航特效

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/XqYroe 可交互视频教 ...

  9. 3.3.4 使用 awk 重新编排字段

    awk 本身所提供的功能完备,已经是一个很好用的程序语言了.以后会好好地介绍该语言的精髓.虽然 awk 能做的事很多,但它主要的设计是要在 Shell脚本中发挥所长:做一些简单的文本处理,例如取出字段 ...

  10. 大数据学习——hive的sql练习

    1新建一个数据库 create database db3; 2创建一个外部表 --外部表建表语句示例: create external table student_ext(Sno int,Sname ...