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. IE10、IE11 User-Agent 导致的 ASP.Net 网站无法写入Cookie 问题

    你是否遇到过当使用一个涉及到Cookie操作的网站或者管理系统时,IE 6.7.8.9下都跑的好好的,唯独到了IE10.11这些高版本浏览器就不行了?好吧,这个问题码农连续2天内遇到了2次.那么,我们 ...

  2. 【Beta】Daily Scrum Meeting第六次

    1.任务进度 学号 已完成 接下去要做 502 无 发布任务到服务器 509 给所有api添加注释 将各个api改为面向对象 517 无 删除任务的控件及逻辑 530 下拉刷新控件 添加及修改职工信息 ...

  3. svm使用的一般步骤

    LIBSVM 使用的一般步骤是:1)准备数据集,转化为 LIBSVM支持的数据格式 :[label] [index1]:[value1] [index2]:[value2] ...即 [l类别标号] ...

  4. unslider.js 实现移动web轮播

    unslider.js可以实现轮播,但是在移动端还需要另两个插件. jquery.event.move和jQuery.event.swipe : 下面就是简单的实例: <!doctype htm ...

  5. HDU 1002 A + B Problem II

    A + B Problem II   Time Limit: 1000MS      Memory Limit: 65536K Total Submissions: 16104    Accepted ...

  6. Effective C++ -----条款44:将与参数无关的代码抽离templates

    Templates生成多个classes和多个函数,所以任何template代码都不该与某个造成膨胀的template参数产生相依关系. 因非类型模板参数(non-type template para ...

  7. CentOS挂载NTFS移动硬盘

    CentOS操作系统默认无法挂在NTFS格式的移动硬盘,解决方案之一为使用ntfs-3g挂在: 1. 在其官网上下载安装包: http://www.tuxera.com/community/open- ...

  8. SharePoint 2010 文档管理之文档推送

    前言:文档推送功能,不是一个复杂的功能,我们这里简单的应用了Ribbon定制.Js使用.对象模型推送(Server端),下面,我们来简单介绍下文档推送功能吧. 一. 功能设计: 文档推送功能,主要就是 ...

  9. 当多个工程互相引用时,若有serverlet工程,提示java.lang.NoClassDefFoundError错误

    serverlet工程和其他的工程引用有所不同,直接在buildpath中添加引用的工程会报NoClassDefFoundError错误错误, 需要在properties-depoyment asse ...

  10. CyclicBarrier

    用于多线程计算数据,最后合并计算结果的应用场景 CyclicBarrier 的字面意思是可循环使用(Cyclic)的屏障(Barrier) 它要做的事情是,让一组线程到达一个屏障(也可以叫同步点)时被 ...