Poj 3264 balanced lineup (ST algorithm entry)

Source: Internet
Author: User

Reprinted please indicate the source, thank youHttp://blog.csdn.net/acm_cxlove/article/details/7854526
By --- cxlove

Question: maximum and minimum values of the query Interval

Http://poj.org/problem? Id = 3264

In the past, only the line segment tree approach, the establishment of nlgn, and the query of lgn were used.

Rmq, as a common question, must be learned and can be used as a tool.

StAlgorithmIs another efficient algorithm for solving the maximum value of a range. The processing of nlgn can achieve O (1) queries, and the constant of the Line Segment tree is also very large.

It is a dynamic planning method.
Take the minimum value as an example. A is the search array.
Record the minimum value in the interval [I, I + 2 ^ J-1] (lasting 2 ^ J) using a two-dimensional array f (I, j. F [I, 0] = A [I];
So for any group (I, j), F (I, j) = min {f (I, J-1), F (I + 2 ^ (J-1 ), j-1)} to use dynamic planning calculations.
This algorithm is clever not in the establishment of dynamic planning, but in its query: its query efficiency is O (1 ).
Suppose we require the minimum value of A in the range [M, N] and find a number k to make 2 ^ k <n-m + 1.
In this way, we can divide this interval into two parts: [M, m + 2 ^ k-1] and [N-2 ^ k + 1, N]. we found that these two intervals have been initialized.
The preceding range is F (M, K), and the following interval is F (n-2 ^ k + 1, K ).
In this way, you can know the minimum value of the entire interval by looking at the minimum values of the two intervals!

 # include 
  
    # include 
   
     # include 
    
      # include 
     
       # include 
      
        # define n100005 using namespace STD; int N, Q, A [n]; int MX [N] [18], Mn [N] [18]; void rmq_init () {int M = floor (log (double) N)/log (2.0); For (INT I = 1; I <= N; I ++) MX [I] [0] = Mn [I] [0] = A [I]; for (INT I = 1; I <= m; I ++) for (Int J = N; j --) {MX [J] [I] = Mx [J] [I-1]; mn [J] [I] = Mn [J] [I-1]; If (J + (1 <(I-1) <= N) {MX [J] [I] = max (MX [J] [I], MX [J + (1 <(I-1)] [I-1]); mn [J] [I] = min (Mn [J] [I], Mn [J + (1 <(I-1)] [I-1]) ;}} int rmq_query (int l, int R) {int M = floor (log (double) (R-l + 1)/log (2.0 )); int max = max (MX [l] [m], MX [R-(1 
       
      
     
    
   
  

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.