POJ2019: Two-dimensional St algorithm for solving the problem of the maximum value of static matrices

Source: Internet
Author: User

We really need to extend the ST algorithm to two-dimensional, because the two-dimensional rmq problem is still a lot of

int n,b,k; int mm[505]; int VAL[MAXN][MAXN]; int dpmin[maxn][maxn][8[8]; int dpmax[maxn][maxn][8[8];

Here n is the square of the length of the square, here is the topic, and then mm is preprocessed, convenient to calculate the index

Dpmin and Dpmax are preprocessing arrays.

Then take a look at the start pretreatment:

voidINITRMQ (intNintm) {     for(intI=1; i<=n;i++)     for(intj=1; j<=m;j++) dpmin[i][j][0][0]=dpmax[i][j][0][0]=Val[i][j];  for(intIi=0; ii<=mm[n];ii++)     for(intjj=0; jj<=mm[m];jj++)    if(ii+JJ) for(intI=1; i+ (1&LT;&LT;II)-1<=n;i++)     for(intj=1; j+ (1&LT;&LT;JJ)-1<=m;j++)    {        if(ii) {DPMIN[I][J][II][JJ]= Min (dpmin[i][j][ii-1][jj],dpmin[i+ (1<< (ii-1))][j][ii-1][JJ]); DPMAX[I][J][II][JJ]= Max (dpmax[i][j][ii-1][jj],dpmax[i+ (1<< (ii-1))][j][ii-1][JJ]); }        Else{DPMIN[I][J][II][JJ]= Min (dpmin[i][j][ii][jj-1],dpmin[i][j+ (1<< (jj-1))][ii][jj-1]); DPMAX[I][J][II][JJ]= Max (dpmax[i][j][ii][jj-1],dpmax[i][j+ (1<< (jj-1))][ii][jj-1]); }    }}

We look at the pre-processing time is still relatively clear, of course, do not forget the main function of the MM initialization good

    mm[0]=-1;      for (int i=1; i<=; i++)        mm[i]= ((i& (-1)) = =0) mm[i-1]+1: mm[i-1];

Then it is the function that asks for the maximum and minimum value, here, must be careful to write, it is easy to write wrong:

intRMQ1 (intX1,intY1,intX2,inty2)//Max{    intk1=mm[x2-x1+1]; intk2=mm[y2-y1+1]; X2=x2-(1&LT;&LT;K1) +1; Y2=y2-(1&LT;&LT;K2) +1; returnMax ( Max (Dpmax[x1][y1][k1][k2],dpmax[x1][y2][k1][k2]), Max (DPMAX[X2][Y1][K1][K2],DPMAX[X2][Y2][K1][K2]));}intRMQ2 (intX1,intY1,intX2,inty2) {    intk1=mm[x2-x1+1]; intk2=mm[y2-y1+1]; X2=x2-(1&LT;&LT;K1) +1; Y2=y2-(1&LT;&LT;K2) +1; returnmin (min (dpmin[x1][y1][k1][k2],dpmin[x1][y2][k1][k2]), Min (Dpmin[x2][y1][k1][k2],dpmin[x2][y2][k1][k2]));}

That's a really long one.

Finally, the complete realization of the topic is given:

1#include <cstdio>2#include <algorithm>3 using namespacestd;4 Const intmaxn=255;5 intn,b,k;6 intmm[505];7 intVAL[MAXN][MAXN];8 intdpmin[maxn][maxn][8][8];9 intdpmax[maxn][maxn][8][8];Ten voidINITRMQ (intNintm) One { A      for(intI=1; i<=n;i++) -      for(intj=1; j<=m;j++) -dpmin[i][j][0][0]=dpmax[i][j][0][0]=Val[i][j]; the      for(intIi=0; ii<=mm[n];ii++) -      for(intjj=0; jj<=mm[m];jj++) -     if(ii+JJ) -      for(intI=1; i+ (1&LT;&LT;II)-1<=n;i++) +      for(intj=1; j+ (1&LT;&LT;JJ)-1<=m;j++) -     { +         if(ii) A         { atDPMIN[I][J][II][JJ] = min (dpmin[i][j][ii-1][jj],dpmin[i+ (1<< (ii-1))][j][ii-1][JJ]); -DPMAX[I][J][II][JJ] = max (dpmax[i][j][ii-1][jj],dpmax[i+ (1<< (ii-1))][j][ii-1][JJ]); -         } -         Else -         { -DPMIN[I][J][II][JJ] = min (dpmin[i][j][ii][jj-1],dpmin[i][j+ (1<< (jj-1))][ii][jj-1]); inDPMAX[I][J][II][JJ] = max (dpmax[i][j][ii][jj-1],dpmax[i][j+ (1<< (jj-1))][ii][jj-1]); -         } to     } + } - intRMQ1 (intX1,intY1,intX2,inty2)//Max the { *     intk1=mm[x2-x1+1]; $     intk2=mm[y2-y1+1];Panax Notoginsengx2=x2-(1&LT;&LT;K1) +1; -y2=y2-(1&LT;&LT;K2) +1; the     returnMax ( Max (DPMAX[X1][Y1][K1][K2],DPMAX[X1][Y2][K1][K2), Max (DPMAX[X2][Y1][K1][K2],DPMAX[X2][Y2][K1][K2])); + } A intRMQ2 (intX1,intY1,intX2,inty2) the { +     intk1=mm[x2-x1+1]; -     intk2=mm[y2-y1+1]; $x2=x2-(1&LT;&LT;K1) +1; $y2=y2-(1&LT;&LT;K2) +1; -     returnmin (min (dpmin[x1][y1][k1][k2],dpmin[x1][y2][k1][k2]), Min (Dpmin[x2][y1][k1][k2],dpmin[x2][y2][k1][k2])); - } the intMain () - {Wuyimm[0]=-1; the      for(intI=1; i<= -; i++) -Mm[i]= ((i& (i-1))==0)? mm[i-1]+1: mm[i-1]; Wu      while(SCANF ("%d%d%d", &n,&b,&k) = =3) -     { About          for(intI=1; i<=n;i++) $          for(intj=1; j<=n;j++) -scanf"%d",&val[i][j]); - INITRMQ (n,n); -         intx, y; A          while(k--) +         { thescanf"%d%d",&x,&y); -printf"%d\n", RMQ1 (x,y,x+b-1, y+b-1)-RMQ2 (x,y,x+b-1, y+b-1)); $         } the     } the     return 0; the}

POJ2019: Two-dimensional St algorithm for solving the problem of the maximum value of static matrices

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.