POJ 3264 Balanced Lineup ST table
Question: Give a string of numbers and ask for the difference between the maximum and minimum values of the interval.
Train of Thought: For RMQ, we can use O (N ^ 2) preprocessing, and then each time we query O (1), we can use line segment tree and O (N) building, for O (logN) queries, you can use ST table records, O (NlogN) preprocessing, and O (1) queries.
In fact, the ST table preprocessing process is also a DP process. dp [I] [j] indicates the maximum range of 2 ^ j consecutive bits starting from the I bit.
Preprocessing: dp [I] [j] = min (dp [I] [j], dp [I + 2 ^ j] [j]), query: query (l, r) = min (dp [l] [k], dp [R-2 ^ k + 1] [k]), k guarantees 2 ^ k <= r-l + 1 and 2 ^ (k + 1)> = r-l + 1.
Code:
#include
#include
#include #define maxn 50010using namespace std;int stTable_min[maxn][32],stTable_max[maxn][32];int preLog2[maxn],aa[maxn];void st_prepare(int n,int *array){ preLog2[1]=0; for(int i=2; i<=n; i++) { preLog2[i]=preLog2[i-1]; if((1<
=0; i--) { stTable_min[i][0]=array[i]; stTable_max[i][0]=array[i]; for(int j=1; (i+(1<