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 ...
随机推荐
- 本地Git搭建并与Github连接
本地Git搭建并与Github连接 git 小结 1.ubuntu下安装git环境 ubuntu 16.04已经自带git ,可以通过下列命令进行安装与检测是否成功安装 sudo apt-get in ...
- web基础知识通信概述URI与http
1.url是什么,有什么作用: 说白了就是我们常说的网址:正规来说就是统一资源定位符是对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址. 互联网上的每个文件都有一个 ...
- ZOJ - 3261 逆向并查集
思路:很巧妙的解法.如果按照常规一边读入,一边合并并查集,删边实在没办法做. 首先读入所有的操作,把所有不会被删除的边加入并查集,然后从最后一个操作开始逆向操作,当遇到删边操作,就直接把这条边加入并查 ...
- Dockerfile 编译安装mysql5.7 千万不要执行.只是记录一下编译安装罢了
开启所有核心make 20G 内存都不够玩,跑3-4个核心吧,还好. 最后的出来的镜像3G多,百思不得其解,看了官方的Dockerfile,也没什么特别,就是 apt 或者 yum.好吧,不知做了什 ...
- 有状态InheritableThreadLocal 配合 JDK8 ,异步方法调用
我们可以把一个类的作用域注解为 @Scope(scopeName = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode. ...
- Java版连连看
连连看大家应该都玩过,不多说直接上一个做好的界面截图吧,所有的功能都在上面的,要做的就只是如何去实现它们了. 差不多就是这个样子.先说一下大致的思路吧.首先编写基本的界面:把什么按钮啊,表格啊什么的都 ...
- 【前端】用百度BAE和express部署自己的node后台
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/node_bae.html 百度有一个应用引擎,价格非常便宜,Java的tomcat每天4毛钱,node每天2毛钱, ...
- mongodb去除重复的数据(二)
前天因为工作需要,开始着手对数据库中两千多万的数据中其中一个字段重复的数据进行去重. 原本使用一些测试的数据测试后,前天写的那个方法是可行的,但是当面对这个两千万的真实数据时,我却发现这方法有些不顶用 ...
- CSS注释
CSS注释 1./*注释内容*/ /*-moz-background-origin:border; -webkit-background-origin:border; -moz-background- ...
- Android开发中用到的第三方框架汇总
最近上网搜索了一些框架资料,整理了以下常用框架,希望在项目中有所帮助. 1.网络请求框架 android-async-http 该网络框架的介绍文章地址:http://www.cnblogs.com/ ...