POJ3264 Balanced Lineup

Source: Internet
Author: User

Exercises

Can be used as a template problem for the ST algorithm in RMQ

Code:

#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<cmath>#include<map>#include<Set>#include<vector>using namespacestd;using namespacestd;#definePB Push_back#defineMP Make_pair#defineSe Second#defineFS First#definell Long Long#defineMS (x, y) memset (x,y,sizeof (×))#defineMC (x, y) memcpy (x,y,sizeof (×))#defineLS o<<1#defineRS O<<1|1#defineSZ (x) ((int) (x). Size ())#definefor (It,c) for (__typeof ((c). Begin ()) it= (c). Begin (); it!= (c). end (); it++)typedef pair<int,int>P;Const Doubleeps=1e-9;Const intmaxn=50100;Const intn=1e9;Const intmod=1e9+7; ll read () {ll x=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//-----------------------------------------------------------------------------intW[MAXN];intN,q,l,r;intmi[maxn][ -],mx[maxn][ -];//Mi[j][i], which starts with J and is the minimum length of the 2^i, MXvoidRmq_init () {//Preprocess to the beginning of each number: The range is 2^0,2^1 ... the extremum in the ...     for(intI=1; i<=n;i++) mi[i][0]=mx[i][0]=W[i]; intM= (int) (Log (n1.0)/log (2.0));//m represents the maximum value of I in the 2^i     for(intI=1; i<=m;i++)     for(intj=1; j<=n;j++) {Mi[j][i]=mi[j][i-1];//Mi[j][i] The minimum value can be from mi[j][i-1] and mi[j+ (1<< (i-1)][i-1], is divided into half        if(J+ (1<< (I-1)) <=n) Mi[j][i]=min (mi[j][i],mi[j+ (1<< (I-1))][i-1]); Mx[j][i]=mx[j][i-1]; if(J+ (1<< (I-1)) <=n) Mx[j][i]=max (mx[j][i],mx[j+ (1<< (I-1))][i-1]); }}//the extremum of [l,r] can be calculated from [L,m],[m,r] two ranges//as long as you meet l+2^k>=r-2^k+1//2^ (k+1) >=r-l+1------>k>=log2 (r-l+1)-1;//1. When log2 (r-l+1) is an integer, K takes log (r-l+1)-1, can also take log (r-l+1), that is, the entire range//2. When log2 (r-l+1) is not an integer, K takes the upward-rounding of log (r-l+1)-1, which is the downward rounding of log (r-l+1)//synthesis 1, 2, go directly to log2 (r-l+1) to the downward rounding, that is (int) (LOG2 (r-l+1))intRmq_max (intLintR) {    intK= (int) (Log (r-l+1)*1.0)/log (2.0)); returnMax (mx[l][k],mx[r-(1&LT;&LT;K) +1][k]);//Add 1 here because, for example [1,8], with a range of 8, the 8-8+1}intRmq_min (intLintR) {    intK= (int) (Log (r-l+1)*1.0/log (2.0)); returnMin (mi[l][k],mi[r-(1&LT;&LT;K) +1][k]);}intMain () {scanf ("%d%d",&n,&q);  for(intI=1; i<=n;i++) scanf ("%d",&W[i]);    Rmq_init ();  for(intI=1; i<=q;i++) {scanf ("%d%d",&l,&R); printf ("%d\n", Rmq_max (L,R)-rmq_min (l,r)); }    return 0;}

POJ3264 Balanced Lineup

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.