首页 > 其他 > 详细

Segment Tree Query I & II

时间:2016-07-09 01:56:54      阅读:181      评论:0      收藏:0      [点我收藏+]

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

 1 /**
 2  * Definition of SegmentTreeNode:
 3  * public class SegmentTreeNode {
 4  *     public int start, end, max;
 5  *     public SegmentTreeNode left, right;
 6  *     public SegmentTreeNode(int start, int end, int max) {
 7  *         this.start = start;
 8  *         this.end = end;
 9  *         this.max = max
10  *         this.left = this.right = null;
11  *     }
12  * }
13  */
14 public class Solution {
15     /**
16      *@param root, start, end: The root of segment tree and 
17      *                         an segment / interval
18      *@return: The maximum number in the interval [start, end]
19      */
20     public int query(SegmentTreeNode root, int start, int end) {
21         if (root == null || root.start > end || root.end < start) return Integer.MIN_VALUE;
22         
23         if (root.start == start && root.end == end) return root.max;
24         
25         int mid = (root.start + root.end) / 2;
26         if (start >= mid + 1) {
27             return query(root.right, start, end);
28         } else if (end <= mid) {
29             return query(root.left, start, end);
30         } else {
31             return Math.max(query(root.left, start, mid), query(root.right, mid + 1 , end));
32         }
33         
34     }
35 }

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

 1 /**
 2  * Definition of SegmentTreeNode:
 3  * public class SegmentTreeNode {
 4  *     public int start, end, count;
 5  *     public SegmentTreeNode left, right;
 6  *     public SegmentTreeNode(int start, int end, int count) {
 7  *         this.start = start;
 8  *         this.end = end;
 9  *         this.count = count;
10  *         this.left = this.right = null;
11  *     }
12  * }
13  */
14 public class Solution {
15     /**
16      *@param root, start, end: The root of segment tree and 
17      *                         an segment / interval
18      *@return: The count number in the interval [start, end]
19      */
20     public int query(SegmentTreeNode root, int start, int end) {
21         if (root == null || root.start > end || root.end < start) return 0;
22         
23         // alert, special case
24         if (end > root.end) end = root.end;
25         if (start < root.start)  start = root.start;
26         
27         if (root.start == start && root.end == end) return root.count;
28         
29         int mid = (root.start + root.end) / 2;
30         
31         if (end <= mid) {
32             return query(root.left, start, end);
33         } else if (start >= mid + 1) {
34             return query(root.right, start, end);
35         } else {
36             return query(root.right, mid + 1, end) + query(root.left, start, mid);
37         }
38     }
39 }

 

 

Segment Tree Query I & II

原文:http://www.cnblogs.com/beiyeqingteng/p/5655024.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!