Topic Link: Click to enter
In fact, this dynamic query interval maximum minimum value of the problem, the solution is a lot, like a line tree and a tree array can be done. The efficiency of the ST algorithm is the same as the above two, but the coding is much simpler.
The ST algorithm is an algorithm that uses recursive thought to calculate, so that DP (I,J) represents the minimum value from a section of an element that has a length of 2^j from I, then DP (I,J) =min (DP (I,J-1), DP (i+2^ (J-1), j-1)). This is the recursive relationship of interval minimum value, in fact, the same is the same as the interval maximum.
The code is as follows:
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace STD;Const intmaxn=50000+ -;intdp1[maxn][ -];intdp2[maxn][ -];intA[MAXN],MM[MAXN];voidRmq_init (intN///RMQ Initialization{mm[0]=-1; for(intI=1; i<=n;i++) {mm[i]= (i& (-1))==0)? mm[i-1]+1: mm[i-1]; dp1[i][0]=dp2[i][0]=a[i]; } for(intj=1; j<=mm[n];j++) for(intI=1; i+ (1<<J)-1<=n;i++) {dp1[i][j]=min (dp1[i][j-1],dp1[i+ (1<< (J-1))][j-1]); Dp2[i][j]=max (dp2[i][j-1],dp2[i+ (1<< (J-1))][j-1]); }}intRmq_max (intLintR/// query maximum value{intk=mm[r-l+1];returnMax (dp2[l][k],dp2[r-(1<<K) +1][k]);}intRmq_min (intLintR/// query minimum value{intk=mm[r-l+1];returnMin (dp1[l][k],dp1[r-(1<<K) +1][k]);}intMain () {//freopen ("In.txt", "R", stdin); intN,m; while(scanf("%d%d", &n,&m)!=eof) { for(intI=1; i<=n;i++)scanf("%d", &a[i]); Rmq_init (n); while(m--) {intb;scanf("%d%d", &a,&b);printf("%d\n", Rmq_max (A, B)-rmq_min (A, b)); } }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
poj--3264balanced lineup+st algorithm to find the maximum interval minimum value