Lintcode247 Segment Tree Query II solution 题解
【题目描述】
For an array, we can build a Segment Tree 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 Segment Tree.
Notice:It is much easier to understand this problem if you finished Segment Tree Build and Segment Tree Query first.
对于一个数组,我们可以对其建立一棵线段树, 每个结点存储一个额外的值count来代表这个结点所指代的数组区间内的元素个数. (数组中并不一定每个位置上都有元素)
实现一个query的方法,该方法接受三个参数root,start和end, 分别代表线段树的根节点和需要查询的区间,找到数组中在区间[start,end]内的元素个数。
【注】如果你完成了 Segment Tree Build 和 Segment Tree Query两道题,会更容易理解此题。
【题目链接】
www.lintcode.com/en/problem/segment-tree-query-ii/
【题目解析】
题目里的start end指的是数组的值的取值范围,count表示的是在这个取值范围之内有几个数字。
可以采用二分法解题。
如果root.start >= start && root.end <= end, 这就意味着这个root所包含的范围是我们要求解的一个范围的子范围,这个范围内的count值我们是要的,所以直接返回root。count
接下来进行二分。
mid = (start + end)/2;
如果mid 《start, 说明root节点的左半部分是不需要考虑的,因此调用 query(root.right, start, end);
如果 mid 》= end, 说明root节点的右侧的值全部比end要大,也不是我们需要考虑的范围,因此调用 query(root,left, start ,end)
最后,如果mid在start跟end之间,那么就需要分别统计 start~mid mid+1~ end范围的结果,然后加起来。
【参考答案】
Lintcode247 Segment Tree Query II solution 题解的更多相关文章
- Lintcode: Segment Tree Query II
For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...
- 247. Segment Tree Query II
最后更新 二刷 09-Jna-2017 利用线段树进行区间查找,重点还是如何判断每一层的覆盖区间,和覆盖去见与当前NODE值域的关系. public class Solution { public i ...
- 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
For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding ...
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
- 202. Segment Tree Query
最后更新 二刷 09-Jan-17 正儿八经线段树的应用了. 查找区间内的值. 对于某一个Node,有这样的可能: 1)需要查找区间和当前NODE没有覆盖部分,那么直接回去就行了. 2)有覆盖的部分, ...
- 439. Segment Tree Build II
最后更新 08-Jan-2017 开始介绍线段树的主要作用了,可以快速在区间查找极值,我猜是这样的..... 一个NODE的最大值取决于它左边和右边最大值里大 按个,所以,所以什么?对了,我们该用po ...
- Lintcode221 Add Two Numbers II solution 题解
[题目描述] You have two numbers represented by a linked list, where each node contains a single digit. T ...
- Lintcode395 Coins in a Line II solution 题解
[题目描述] There are n coins with different value in a line. Two players take turns to take one or two c ...
随机推荐
- Android Stdio 中的Rendering Problems Android N requires the IDE to be running with Java 1.8 or later Install a supported JDK解决办法
出现如下图所示的错误 解决办法为: 然后在里面输入SDK 下载 下载APILevel为23版本的SDK 换成23版本的SDK 完美解决问题
- 2018年最完整5大网页设计图标解决方案:Font Awesome奥森图Unicode、CSS 和、Font以及国产zfont图标集
网上有很多高质量的图标,基于icon的关键词能在Google上搜索到不少内容,不考虑版权外还要修改大小.颜色等等,现在介绍一些替代方案:Unicode.CSS 和 Font,它具有更高的灵活性. 方案 ...
- javascript类型判断方法
判断javascript中的类型,共有四种常用的方法 var a=6; var b="str"; var c=true; var arr=[]; typeof 用于基本类型的判断 ...
- ffmpeg结构体以及函数介绍(二)
1 avcodec_find_decoder() /** * Find a registered decoder with a matching codec ID. * * @param id Cod ...
- PL/SQL集合 ----- varrays
varrays可以再表,记录,对象定义中使用,类似于C中的数组. 1.定义varrays用作PL/SQL程序构造块. declare type integer_varray ) of integer; ...
- JavaScript实现弹窗报错
JavaScript实现弹窗报错 1.具体错误如下 SCRIPT 5022:cannot call methods on dialog prior to initialization; attempt ...
- (三十)java多线程一
我们通常在电脑中打开的应用称作进程,一个应用就是一个进程,而一个进程里边一般包含多个线程. 系统要为每一个进程分配独立的内存空间,而进程里的多个线程共用这些内存. 我们通常所写的main方法就是一个线 ...
- vue全局配置----小白基础篇
今天学习vue全局配置.希望帮助我们去了解vue的全局配置,快速开发. Vue.config是vue的全局配置对象.包含Vue的所有全局属性: silent:boolean(默认值:false)--- ...
- 命令行工具osql.exe使用
目标: 快速在21个库修改Test表的某条记录,这几个库都分别在不同的服务器上. 通常会想到,到每个库都执行一下语句不就好了吗?这个数据库切换来切换去,挺麻烦了,通过命令行工具osql.exe就可以快 ...
- 二叉树与AVL树
二叉树 什么是二叉树? 父节点至多只有两个子树的树形结构成为二叉树.如下图所示,图1不是二叉树,图2是一棵二叉树. 图1 普通的树 ...