Poj 3264 balanced lineup st Algorithm

Source: Internet
Author: User

The St algorithm is the sparse table algorithm, which refers to the meaning of a sparse table. It is used to divide a table into the power segment of 2 and then calculate the query result using this st table, the pre-processing time O (nlgn) and the query time is O (1 );


Some people may wonder, since the query time is O (1), why is this algorithm not much faster or even faster than the line segment tree?

In fact, the query time is O (log (range), and range is the size of the query interval. To find the maximum number of digits of the range binary, floor (log (range) can be used )) but it also requires lg (rang) time efficiency, because the range is not too large, so this time efficiency can be considered as O (1 ). however, this explains why the query time of this algorithm is not faster than that of the Line Segment tree, because the query time of the Line Segment tree is log (range ).

Few people on the internet pointed out that the query time is O (1) in general, and many people seem to be confused about it.

However, it can also be said that the query time efficiency of the algorithm itself is O (1), but the actual execution is O (logn ). In theory, the time efficiency is O (1), and the actual time efficiency is O (logn ). It is estimated that this is why the query efficiency of this algorithm is O (1) In many blogs or books.


It is a mathematical method.

Specific can refer to topcoder or refer to this Daniel's right: http://blog.csdn.net/v_july_v/article/details/18312089


The advantage of this algorithm is that the amount of code is less than that of the Line Segment tree. After mastering the mathematical principles, this algorithm is well written.

However, because of the existence of a cable segment tree, this algorithm does not seem to be essential.


# Include <cstdio> # include <algorithm> const int max_n = 50001; const int L = 17; // 16; // (INT) log (max_n)/log (2) + 1; int N, Q, a, B; int maxarr [max_n] [L], minarr [max_n] [l]; void initrmq_st () {for (Int J = 1; j <L; j ++) for (INT I = 1; I <= N; I ++) {if (I-1 + (1 <j) <= N) {maxarr [I] [J] = max (maxarr [I] [J-1], maxarr [I + (1 <(J-1)] [J-1]); minarr [I] [J] = min (minarr [I] [J-1], minarr [I + (1 <(J-1)] [J-1]);} else break ;// Can be break} inline int query (INT low, int up) {int range = up-low + 1; int r = 0; while (1 <(R + 1) <= range) r ++; int maxv = max (maxarr [low] [r], maxarr [Up-(1 <r) + 1] [r]); int MINV = min (minarr [low] [r], minarr [Up-(1 <r) + 1] [r]); Return maxv-MINV;} int main () {While (scanf ("% d ", & N, & Q )! = EOF) {for (INT I = 1; I <= N; I ++) {scanf ("% d", & maxarr [I] [0]); minarr [I] [0] = maxarr [I] [0];} initrmq_st (); While (Q --) {scanf ("% d", &, & B); printf ("% d \ n", query (a, B) ;}} return 0 ;}



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.