1#include <stdio.h>2#include <string.h>3 Const intN = -;4 intA[n];5 intdp[n][ -];6InlineintMinConst int&a,Const int&b)7 {8 returnA < b?a:b;9 }Ten One /* A Dp[i][j] Indicates the minimum value in the interval beginning with I, with a length of 2^j - very clearly dp[i][0] = a[i]; - and the transfer equation is dp[i][j] = min (dp[i][j-1], dp[i+ (1<< (j-1)][j-1]); Divide the interval into 2 2^ (j-1) Inter-cell the */ - voidRmq_init (intN) - { - inti,j; + for(i=1; i<=n; ++i) dp[i][0] =A[i]; - for(j=1; (1<<J) <=n; ++j) + for(i=0; i+ (1<<J)-1<=n; ++i) ADp[i][j] = min (dp[i][j-1],dp[i+ (1<< (J-1))][j-1]);//divide the interval into 2 2^ (j-1) of the inter-cell, the idea of DP at } - - //make 2^k <= r-l+1, then start with L, end of R with a length of 2^k, covering the interval [l,r] - //2^k <= r-l+1, the length of the 2^k is more than half of the interval [l,r], so starting with L, the length 2^k with R ends can cover the interval [l,r] - intRMQ (intLintR) - { in intK =0; - while(1<< (k +1) <= r-l+1) k++; to returnMin (Dp[l][k], dp[r-(1<<K) +1][k]); + } - intMain () the { * intN, I,l,r; $scanf"%d",&n);Panax Notoginseng for(i=1; i<=n; ++i) -scanf"%d",&a[i]); the rmq_init (n); + while(SCANF ("%d%d", &l,&r)! =EOF) A { theprintf"%d\n", RMQ (l,r)); + } - return 0; $}
St Algorithm of RMQ