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 ...
随机推荐
- java在目录中过滤文件
package com.zh.test; import java.io.File; import java.io.FilenameFilter; public class Test2 { /** * ...
- 如何正确地使用Entity Framework Database First
毕设依旧在不紧不慢地以每天解决一个问题的进度进行中.今天遇到的问题就是在建立数据模型时遇到的.因为项目是基于数据库构建的,所以理所应当地采用DB First来构造实体类和DbContext类.于是想也 ...
- 【BZOJ 1001】狼抓兔子 对偶图+SPFA
这道题是求图的最小割,也就是用最大流.但因为边太多,最大流算法会T,因此不能用最大流算法. 因为这是个平面图,所以求平面图的最小割可以使用特殊的技巧就是求对偶图然后求对偶图的最短路.把每个面看成一个点 ...
- Eclipse-将svn上的项目转化成相应的项目
这里假设svn上的项目为maven项目 首先从svn检出项目 其中项目名称code可自己定义更改新的名称 从svn检出的项目结构 然后将项目转化成相关的项目 转换加载中 加载/下载 maven相关内容 ...
- 统计"1"个数问题
问题: 给定一个十进制整数N,求出从1到N的所有整数中出现”1”的个数. 例如:N=2时 1,2出现了1个 “1” . N=12时 1,2,3,4,5,6,7,8,9,10,11,12.出现了5个“1 ...
- BZOJ-3229 石子合并 GarsiaWachs算法
经典DP?稳T 3229: [Sdoi2008]石子合并 Time Limit: 3 Sec Memory Limit: 128 MB Submit: 426 Solved: 202 [Submit] ...
- BZOJ-1012[JSOI2008]最大数maxnumber 线段树区间最值
这道题相对简单下面是题目: 1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MB Submit: 6542 Solve ...
- 【poj2226】 Muddy Fields
http://poj.org/problem?id=2226 (题目链接) 题意 给出一个只包含‘.’和‘*’的矩阵,用任意长度的宽为1的木板覆盖所有的‘*’而不覆盖‘.’,木板必须跟矩形的长或宽平行 ...
- java中获取本地文件的编码
import java.util.*; public class ScannerDemo { public static void main(String[] args) { System.out.p ...
- Spring学习8-用MyEclipse搭建SSH框架 Struts Spring Hibernate
1.new一个web project. 2.右键项目,为项目添加Struts支持. 点击Finish.src目录下多了struts.xml配置文件. 3.使用MyEclipse DataBase Ex ...