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.

Have you met this question in a real interview? Yes
Example
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 Note
It is much easier to understand this problem if you finished Segment Tree Buildand Segment Tree Query first.

注意23行

 /**
* 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) {
// write your code here
if (root == null || root.end < start || root.start > end) return 0;
if (root.start>=start && root.end<=end) return root.count;
int mid = (root.start + root.end)/2;
if (end <= mid) return query(root.left, start, end);
else if (start > mid) return query(root.right, start, end);
else return query(root.left, start, mid) + query(root.right, mid+1, end);
}
}

Lintcode: Segment Tree Query II的更多相关文章

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

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

  3. [LintCode] Segment Tree Build II 建立线段树之二

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

  4. 247. Segment Tree Query II

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

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

  6. Lintcode: Segment Tree Modify

    For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...

  7. [LintCode] Segment Tree Build 建立线段树

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  8. Lintcode: Segment Tree Build

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  9. 202. Segment Tree Query

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

随机推荐

  1. http UserAgent

    string uAgent = Request.ServerVariables["HTTP_USER_AGENT"].ToLower();  //获取客户端浏览器的请求 判断 是什 ...

  2. P85练习3

    public class P85Excise { public static void main(String[] args) { // TODO 自动生成的方法存根 int i =1; float ...

  3. [dpdk] 读开发指南(1)

    该文档是随着对于文档的阅读进度,不断增加的阅读笔记.主要内容以大纲为主,以及记录帮助记忆的内容. 在之后的实际应用中,也不随着不断的深入理解,逐渐丰富各大纲下面的内容. 1. 前期准备:设置两个环境变 ...

  4. (IOS)Swift2.0 Radio 程序分析

    本文主要分享下楼主在学习Swift编程过程中,对GitHub上的一个开源项目Swift Radio的研究心得. 项目地址:https://github.com/swiftcodex/Swift-Rad ...

  5. (转) java 简单工厂模式(实现一个计算器)

    package com.simpleFactory; /** * 运算类 * @author Administrator * */ public class Operation { private d ...

  6. Linux学习之CentOS(十)--虚拟机下的CentOS如何上网

    原地址:http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/05/3001148.html 这篇随笔应该说跟CentOS的学习关系不是很大, ...

  7. mysql基本sql语句大全(基础用语篇)

    1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建 备份 ...

  8. json-lib 之jsonConfig具体应用

    一,setCycleDetectionStrategy 防止自包含 public static void testCycleObject() {         CycleObject object ...

  9. A+Bproblem

    package A+Bproblem; /* * A+B Problem 时间限制:3000 ms  |  内存限制:65535 KB 难度:0 描述 此题为练手用题,请大家计算一下a+b的值 输入 ...

  10. [LeetCode] Scramble String(树的问题最易用递归)

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...