Minimum value of "self-occupied" segment tree interval

Source: Internet
Author: User
Reference

http://www.cnblogs.com/TenosDoIt/p/3453089.html Overview

is a balanced binary tree, where each node holds a segment (an array interval), which is mainly used to efficiently solve the dynamic query of continuous interval, and basically maintains the complexity of each operation O (Logn). Example

Problem Description: Finds the minimum value within a range from the array arr[0...n-1], where the array size is fixed, but the values of the elements in the array can be updated at any time.

The simplest solution: traversing the array interval to find the minimum value, time complexity O (n), additional space complexity O (1) stores the temporary min variable, but when the data is large and the query operation is frequent, it may time out.

Faster solution: Using a two-dimensional array to save the pre-calculated interval [I, j] The minimum value, the preprocessing time is O (n2) O (n^2), the query time-consuming O (1), but requires additional O (n2) O (n^2) space, when the volume of data is large, the space consumption is large, Also, updating the minimum value in a two-dimensional array is cumbersome when you change one of the values in the data.

The method of using line-segment tree: Preprocessing time O (n), query, update operation O (LOGN), need additional space O (n). According to this problem, construct the following two-fork tree: The leaf node is the original array in arr, the element of the non-leaf node represents the minimum value of all of its children's nodes.


For example, the array [2, 5, 1, 4, 9, 3] can construct the following two-tree (the black line is a leaf node, the blue lines are non-leaf nodes, the minimum values for the corresponding array interval) such as the root node represents the array interval arr[0 ... 5] The minimum value inside is 1:

For a balanced binary tree with n leaf nodes, there are n-1 non-leaf nodes, so there is a total of 2n-1 nodes, so the space complexity required to store segments is O (n).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.