Title Link: http://poj.org/problem?id=3264
Typical RMQ, this problem was I whip corpse three times is also drunk ... This time with the newly learned St algorithm.
The ST algorithm itself is an interval DP, the nature of the use is that the maximum value of the adjacent two intervals must be the maximum value of the two interval combined, this property determines the overlap of the DP sub-problem. You can use this property to preprocess the table, except that the step is a power of 2.
The same is true when querying, but it is not necessarily accurate to select two intervals, it does not matter, because the two overlapping sections will be automatically calculated in the most value of the interior. This is the time to subtract this part if the interval is counted. (interval and words directly with the prefix and not just OK)
1 /*2 ━━━━━┒ギリギリ♂eye! 3 ┓┏┓┏┓┃キリキリ♂mind! 4 ┛┗┛┗┛┃\0/5 ┓┏┓┏┓┃/6 ┛┗┛┗┛┃ノ)7 ┓┏┓┏┓┃8 ┛┗┛┗┛┃9 ┓┏┓┏┓┃Ten ┛┗┛┗┛┃ One ┓┏┓┏┓┃ A ┛┗┛┗┛┃ - ┓┏┓┏┓┃ - ┃┃┃┃┃┃ the ┻┻┻┻┻┻ - */ -#include <algorithm> -#include <iostream> +#include <iomanip> -#include <cstring> +#include <climits> A#include <complex> at#include <fstream> -#include <cassert> -#include <cstdio> -#include <bitset> -#include <vector> -#include <deque> in#include <queue> -#include <stack> to#include <ctime> +#include <Set> -#include <map> the#include <cmath> * using namespacestd; $ #defineFr FirstPanax Notoginseng #defineSC Second - #defineCL Clear the #defineBUG puts ("Here!!!") + #defineW (a) while (a--) A #definePB (a) push_back (a) the #defineRint (a) scanf ("%d", & (A)) + #defineRll (a) scanf ("%lld", &a) - #defineRs (a) scanf ("%s", a) $ #defineCIN (a) CIN >> a $ #defineFRead () freopen ("in", "R", stdin) - #defineFWrite () freopen ("Out", "w", stdout) - #defineRep (i, Len) for (int i = 0; i < (len); i++) the #defineFor (I, A, Len) for (int i = (a); I < (len); i++) - #defineCls (a) memset ((a), 0, sizeof (a))Wuyi #defineCLR (A, X) memset ((a), (x), sizeof (a)) the #defineFull (a) memset ((a), 0x7f, sizeof (a)) - #defineLRT RT << 1 Wu #defineRRT RT << 1 | 1 - #definePi 3.14159265359 About #defineRT return $ #defineLowbit (x) x & (-X) - #defineOnenum (x) __builtin_popcount (x) -typedefLong LongLL; -typedefLong DoubleLD; Atypedef unsignedLong LongUint; +typedef pair<int,int>PII; thetypedef PAIR<LL, Ll>PLL; -typedef pair<string, ll>psi; $typedef map<string, ll>MSI; thetypedef vector<ll>VI; thetypedef vector<ll>VL; thetypedef vector<vl>VVL; thetypedef vector<BOOL>vb; - in Const intMAXN =50050; the intN, Q; the intH[MAXN]; About intdp[maxn][ -][2]; the the voidSt () { the for(inti =1; I <= N; i++) dp[i][0][0] = dp[i][0][1] =H[i]; + for(intj =1; (1<< j) <= N; J + +) { - for(inti =1; i + (1<< j)-1<= N; i++) { thedp[i][j][0] = min (dp[i][j-1][0], dp[i+ (1<< (J-1))][j-1][0]);Bayidp[i][j][1] = max (dp[i][j-1][1], dp[i+ (1<< (J-1))][j-1][1]); the } the } - } - the intQueryintLintr) { the intj =0; the while((1<< (j +1)) <= R-l +1) J + +; the returnMax (dp[l][j][1], dp[r-(1<<J) +1][j][1])-min (dp[l][j][0], dp[r-(1<<J) +1][j][0]); - } the the intMain () { the //FRead ();94 while(~SCANF ("%d%d",&n,&q)) { thefor (I,1, n+1) Rint (H[i]); the St (); the intL, R;98 W (q) { Aboutscanf"%d%d",&l,&R); -printf"%d\n", Query (l,r));101 }102 }103Rt0;104}
[POJ3264] Balanced lineup (RMQ, St algorithm)