Once written a one-dimensional, tonight with a long time according to that one-dimensional change into two-dimensional and then do this problem
St algorithm: Finding the maximum value within a specified interval
One-dimensional: set d[i][j] means [i,i+1,..., i+2^j-1] The maximum value in this interval and any given interval can be represented by two such intervals (overlapping parts) and then only one max for two intervals is required.
D[I][J] can be obtained by recursion
Two-dimensional: set D[i][j][k][l] represents the maximum value in a sub-matrix consisting of [i,i+1,...., i+2^j-1] rows and [k,k+1,...., k+2^l-1] columns, which can then be represented by four parts, that is, this part makes people want to die.
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 Const intMAXN =301;6 intd[maxn][9][maxn][9],A[MAXN][MAXN];7 intn,m;8 voidInit ()9 {Ten intsn =0, SM =0, x =1; One while((x<<1) <n) x=x<<1, sn++; Ax =1; - while((x<<1) <m) x=x<<1, sm++; - for(inti =1; i<=n;++i) for(intj =1; j<=m;++j) thed[i][0][j][0] =A[i][j]; - for(inti =0; i<=sn;i++) for(intj =0; j<=sm;++j) - { - if(i==0&& j==0)Continue; + intLenn = (1<<i)-1, Lenm = (1<<J)-1; - for(intK =1; k+lenn<=n;++k) for(intL =1; l+lenm<=m;++l) + if(i==0&& j!=0) d[k][0][L][J] = max (d[k][0][l][j-1],d[k][0][l+ ((lenm+1) >>1)][j-1]); A Else if(i!=0&& j==0) d[k][i][l][0] = max (d[k][i-1][l][0],d[k+ ((lenn+1) >>1)][i-1][l][0]); at ElseD[K][I][L][J] = max (d[k][i-1][l][j-1],max (d[k][i-1][l+ ((lenm+1) >>1)][j-1],max (d[k+ (lenn+1)/2][i-1][l][j-1],d[k+ (lenn+1)/2][i-1][l+ (lenm+1)/2][j-1]))); - } - } - intQueryintXintYintLintR) - { - intLenn =1, SN =0, Lenm =1, SM =0; in while(lenn*2<l-x+1) lenn=lenn<<1, sn++; - while(lenm*2<r-y+1) lenm=lenm<<1, sm++; to returnMax (D[x][sn][y][sm],max (d[x][sn][r-lenm+1][sm],max (d[l-lenn+1][sn][y][sm],d[l-lenn+1][sn][r-lenm+1][SM] )); + } - intMain () the { *Freopen ("In.txt","R", stdin); $ while(~SCANF ("%d%d",&n,&m))Panax Notoginseng { - for(inti =1; i<=n;++i) for(intj =1; j<=m;++j) thescanf"%d",&a[i][j]); + init (); A intT;SCANF ("%d",&T); the while(t--) + { - intX,y,l,r; $scanf"%d%d%d%d",&x,&y,&l,&R); $ intAns =query (x,y,l,r); -printf"%d", ans); - if(a[x][y]==ans| | a[l][r]==ans| | a[x][r]==ans| | A[l][y]==ans) printf ("yes\n"); the Elseprintf"no\n"); - }Wuyi } the return 0; -}
HDU2888 Two-dimensional St