202. Segment Tree Query
最后更新
二刷
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的更多相关文章
- 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), ...
- 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 ...
- 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 ...
- 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 ...
- BestCoder#16 A-Revenge of Segment Tree
Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...
- HDU5086——Revenge of Segment Tree(BestCoder Round #16)
Revenge of Segment Tree Problem DescriptionIn computer science, a segment tree is a tree data struct ...
随机推荐
- C-基础:数组名与取地址符&
指出下面代码的输出,并解释为什么.(不错,对地址掌握的深入挖潜) main() { ]={,,,,}; ); printf(),*(ptr-)); } 输出:2,5 *(a+1)就是a[1], ...
- 工作流activi链接地址
http://topmanopensource.iteye.com/blog/1313865
- ffmpeg解析TS流
介绍: MPEG的系统层编码为不同的应用场景设计了两种格式: TS(Transport Stream) 和PS(Program Stream), 它们两者之间不具有层级关系, 在逻辑上,它们两者都 ...
- 高可用技术之keepalived原理简单了解
Keepalived 工作原理 keepalived是以VRRP协议为实现基础的,VRRP全称Virtual Router Redundancy Protocol,即虚拟路由冗余协议. 虚拟路由冗余协 ...
- python基础知识09-继承,多继承和魔术方法
1.继承 class Father: def init(self,age,sex): self.age = age self.sex = sex class Son(Father): 类名后面写括号, ...
- POJ 3468 A Simple Problem with Integers (线段树多点更新模板)
题意: 给定一个区间, 每个区间有一个初值, 然后给出Q个操作, C a b c是给[a,b]中每个数加上c, Q a b 是查询[a,b]的和 代码: #include <cstdio> ...
- Java学习笔记(1)-(GridBagLayout)网格袋布局
学习JAVA-布局管理的时候,在书上看到了这么一段话:GridBagLayout的功能非常强大,使用是也比较复杂,考虑到一般的读者很少会使用到这种管理,这里不做介绍.然书本就跳过了,为什么功能强大却很 ...
- luogu1856 [USACO5.5]矩形周长Picture
看到一坨矩形就要想到扫描线.(poj atantis) 我们把横边竖边分开计算,因为横边竖边其实没有区别,以下论述全为考虑竖边的. 怎样统计一个竖边对答案的贡献呢?答:把这个竖边加入线段树,当前的总覆 ...
- InnoDB透明页压缩与稀疏文件
此文已由作者王慎为授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. MySQL 5.7中包括了很多让人耳目一新的新特性,其中就包括了InnoDB Transparent Pag ...
- 大数据学习——mapreduce汇总手机号上行流量下行流量总流量
时间戳 手机号 MAC地址 ip 域名 上行流量包个数 下行 上行流量 下行流量 http状态码 1363157995052 13826544101 5C-0E-8B-C7-F1-E0:CMCC 12 ...