Segment Tree Query I

For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding SegmentTree, each node stores an extra attribute max to denote the maximum number in the interval of the array (index from start to end).

Design a query method with three parameters rootstart and end, find the maximum number in the interval [start, end] by the given root of segment tree.

Example

For array [1, 4, 2, 3], the corresponding Segment Tree is:

                  [0, 3, max=4]
                 /             \
          [0,1,max=4]        [2,3,max=3]
          /         \        /         \
   [0,0,max=1] [1,1,max=4] [2,2,max=2], [3,3,max=3]

query(root, 1, 1), return 4

query(root, 1, 2), return 4

query(root, 2, 3), return 3

query(root, 0, 2), return 4

 /**
  * Definition of SegmentTreeNode:
  * public class SegmentTreeNode {
  *     public int start, end, max;
  *     public SegmentTreeNode left, right;
  *     public SegmentTreeNode(int start, int end, int max) {
  *         this.start = start;
  *         this.end = end;
  *         this.max = max
  *         this.left = this.right = null;
  *     }
  * }
  */
 public class Solution {
     /**
      *@param root, start, end: The root of segment tree and
      *                         an segment / interval
      *@return: The maximum number in the interval [start, end]
      */
     public int query(SegmentTreeNode root, int start, int end) {
         if (root == null || root.start > end || root.end < start) return Integer.MIN_VALUE;

         if (root.start == start && root.end == end) return root.max;

         ;
         ) {
             return query(root.right, start, end);
         } else if (end <= mid) {
             return query(root.left, start, end);
         } else {
              , end));
         }

     }
 }

Segment Tree Query II

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 [startend] by the given root of value SegmentTree.

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

 /**
  * 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) {
         ;

         // alert, special case
         if (end > root.end) end = root.end;
         if (start < root.start)  start = root.start;

         if (root.start == start && root.end == end) return root.count;

         ;

         if (end <= mid) {
             return query(root.left, start, end);
         } ) {
             return query(root.right, start, end);
         } else {
             , end) + query(root.left, start, mid);
         }
     }
 }

Segment Tree Query I & II的更多相关文章

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

  3. Segment Tree Build I &amp; II

    Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...

  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 Modify

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

  6. Leetcode: Range Sum Query - Mutable &amp;&amp; Summary: Segment Tree

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

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

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

  8. BestCoder#16 A-Revenge of Segment Tree

    Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data struc ...

  9. HDU5086——Revenge of Segment Tree(BestCoder Round #16)

    Revenge of Segment Tree Problem DescriptionIn computer science, a segment tree is a tree data struct ...

随机推荐

  1. js快速判断IE浏览器(兼容IE10与IE11)

    在很多时候,我们一般采用navigator.userAgent和正则表达来判断IE浏览器版本,下面介绍用IE浏览器中不同特性来判断IE浏览器   1 判断IE浏览器与非IE 浏览器 IE浏览器与非IE ...

  2. Unreleased Resource(未释放资源)-Streams(流)

    java中把不同的输入/输出源(键盘.文件.网络连接等)抽象表现为Stream(流). java程序可以通过使用不同的流来访问不同的输入/输出源.而Stream(流)可以直观的理解为从数据的源(Sou ...

  3. IIS出现Service Unavailable 错误

    IIS访问操作出现以下问题时要如何解决:

  4. 记录自己对EventLoop和性能问题处理的一点心得

    1.EventLoop 这里说的EventLoop不是指某一个具体的库或是框架,而是指一种程序实现结构.这种结构多是基于IO多路转接的API(select.poll.epoll之类)以reactor模 ...

  5. union all 取代 select中的case when 提高查询效率

    首先union all不是适用于所有情况,其次,case when的可读性肯定要强.所以在不是特别在意性能的情况下, 可以考虑用case when. 再者,不是所有情况下的union all都要比ca ...

  6. CI中自定义SQL查询,LIKE模糊查询的处理

    参考: /** * 据用户输入的关键字查询相册信息;照片墙搜索框功能 * @param $keyWord 关键字 * @param $pageNum 页码,第几页 * @param $pageSize ...

  7. 1.BOM学习

    1.bom.html <html> <head> <title>bom演示</title> <script type="text/jav ...

  8. linux_iptables 详解

    iptables工具__过滤包—命令(-A.-I.-D.-R.-L等).参数(-p.-s.-d.--sport.--dport.-i.-o等).动作-j (ACCEPT.DROP.REJECT.RED ...

  9. Uva 11198 - Dancing Digits

    Problem D Dancing Digits 题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid ...

  10. Vim cscope

    /********************************************************************** * Vim cscope * 说明: * 之前使用Vim ...