【题目描述】

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范围的结果,然后加起来。

【参考答案】

www.jiuzhang.com/solutions/segment-tree-query-ii/

Lintcode247 Segment Tree Query II solution 题解的更多相关文章

  1. Lintcode: Segment Tree Query II

    For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...

  2. 247. Segment Tree Query II

    最后更新 二刷 09-Jna-2017 利用线段树进行区间查找,重点还是如何判断每一层的覆盖区间,和覆盖去见与当前NODE值域的关系. public class Solution { public i ...

  3. 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), ...

  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. [LintCode] Segment Tree Build II 建立线段树之二

    The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...

  6. 202. Segment Tree Query

    最后更新 二刷 09-Jan-17 正儿八经线段树的应用了. 查找区间内的值. 对于某一个Node,有这样的可能: 1)需要查找区间和当前NODE没有覆盖部分,那么直接回去就行了. 2)有覆盖的部分, ...

  7. 439. Segment Tree Build II

    最后更新 08-Jan-2017 开始介绍线段树的主要作用了,可以快速在区间查找极值,我猜是这样的..... 一个NODE的最大值取决于它左边和右边最大值里大 按个,所以,所以什么?对了,我们该用po ...

  8. Lintcode221 Add Two Numbers II solution 题解

    [题目描述] You have two numbers represented by a linked list, where each node contains a single digit. T ...

  9. 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 ...

随机推荐

  1. Centos6增加新用户并赋予权限

    第一步:创建用户并设置密码 useradd testuser // 增加用户名为'testuser'的用户 passwd testpasswd //设定密码为'testpasswd' 第二步:用户授权 ...

  2. 动态规划算法的java实现

    一:动态规划 1)动态规划的向前处理法 java中没有指针,所以邻接表的存储需要转化一中形式,用数组存储邻接表 用三个数组u,v,w存储边,u数组代表起点,v数组代表终点,w代表权值;例如:1--&g ...

  3. Android Stdio 如何自定义生成APK的名称

    Android Stdio自动默认生成的app的名称都是app-release或者app-debug,生成完后还要手动更改apk的名称,很是麻烦. 自定义生成APK的名称的方法:在\app\build ...

  4. UVA 1626 Brackets sequence 区间DP

    题意:给定一个括号序列,将它变成匹配的括号序列,可能多种答案任意输出一组即可.注意:输入可能是空串. 思路:D[i][j]表示区间[i, j]至少需要匹配的括号数,转移方程D[i][j] = min( ...

  5. docker mysql 主从复制

    当然首先 docker pull mysql mkdir /usr/local/mysqlData/master/cnf mkdir /usr/local/mysqlData/master/data ...

  6. android WebP解析开源库-支持高清无损

    在我们的项目中需要支持WebP高清无损图片,推荐一个我们已经使用的解析开源库给大家:https://github.com/keshuangjie/WebpExample/tree/master/lib ...

  7. win7 vs2012+wdk8.0 搭建wdf驱动开发环境

    开发环境搭建:系统:win7 x64工具:vs2012 + WDK8.0插件:wdfcoinstaller.msi(1)先安装vs2012,再安装wdk8.0,这样在打开vs2012时可以创建wind ...

  8. 【mongodb系统学习之十】mongodb查询(三)

    6).特殊类型的查询: A).查询键为null的文档:查询时,如果按常规的key:null的方式做条 件,不仅会匹配到值为null的,还会匹配到不存在这个键的:如果只是要值为null的文档,就必须使用 ...

  9. Caused by: java.lang.ClassNotFoundException: org.jbpm.pvm.internal.processengine.SpringHelper

    1.错误描述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...

  10. java.lang.NullPointerException: No FileItemFactory has been set.

    1.错误描述 java.lang.NullPointerException: No FileItemFactory has been set. at org.apache.commons.fileup ...