【题目描述】

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. SpringMVC+FreeMarker实现静态资源文件自动添加版本号(md5)

    近日切换java开发,开始学习springframework.在实现静态资源文件自动计算版本号的实例时,因为不熟悉框架,走了不少弯路,好在最终解决了问题.这里写篇文章记录一下实现,也希望对大家有些用处 ...

  2. linux开放80 端口

    1.使用su登录管理员用户 2.编辑防火墙配置文件 vim /etc/sysconfig/iptables 3.在里面加入后保存 #open port 80 -A INPUT -p TCP -m st ...

  3. asp.net core 一 Centos 环境部署

             .netcore的运行环境,创建asp.net core 项目    CentOS 7 ,dotnet-sdk-2.0.0-2.0.0-1.x86_64 直接在liunx创建项目并运 ...

  4. 初识Vue——计算属性和观察者

    一.计算属性 在模板内使用 1.基础例子 <template> <div class="main"> <div id="reverse_st ...

  5. Git版本控制的基本命令

    安装完了GIT首先要自报家门,否则代码不能提交 git config --global user.name "Your Name" git config --global user ...

  6. ffmpeg入门之 Tutorial02

    02实际是在01的基础上添加了 SDL显示yuv部分,这部分相对独立. if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER))   ...

  7. 【mysql】phpMyadmin上传文件限制

    在使用phpMyadmin导入数据库的时候,因为脚本在上传的时候响应时间过长,导致大于2M的数据偶尔会导入失败.而且大多数默认设置,只能导入2M的数据. 遇到这种情况, 我们可以修改php.ini以及 ...

  8. 基于LVDS/M-LVDS的数据通信

    现在有两种方案:一种基于 M-LVDS (基于总线的多节点通信) ,有其 特定的电气要求:另外一种是基于 LVDS 的点到点通信,具体说明如 下: 基于 M-LVDS 的总线通信: 基于 M-LVDS ...

  9. document.forms[].submit()

    document.forms['exportServlet'].submit(); (1)document.forms:表示获取当前页面的所有表单 (2)document.forms[0]:表示获取当 ...

  10. Oracle连接出错(一)

    1.错误描述 java.sql.SQLException: ORA-0064:error occurred at recursive SQL level 1. ORA-06153:unable to ...