首页 > 代码库 > 二维RMQ

二维RMQ

求二维ST表

    for (int k=0;k<=10;k++) for (int l=0;l<=10;l++)
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++){
        int ty=min(j+(1<<(l-1)),m+1),tx=min(n+1,i+(1<<(k-1)));
          if (k==0&&l==0) continue;else
          if (k==0) rmq[i][j][k][l]=max(rmq[i][ty][k][l-1],rmq[i][j][k][l-1]);else
          if (l==0) rmq[i][j][k][l]=max(rmq[tx][j][k-1][l],rmq[i][j][k-1][l]);else
                    rmq[i][j][k][l]=max(max(rmq[i][j][k-1][l-1],rmq[tx][j][k-1][l-1]),
                                        max(rmq[i][ty][k-1][l-1],rmq[tx][ty][k-1][l-1]));
      }

求RMQ

  int getrmq(int x1,int y1,int x2,int y2){
      int ranx=log(x2-x1+1)/log(2)+0.001,rany=log(y2-y1+1)/log(2)+0.001;
      int tx=x2+1-(1<<ranx),ty=y2+1-(1<<rany);
      return(max(max(rmq[x1][y1][ranx][rany],rmq[tx][y1][ranx][rany]),max(rmq[x1][ty][ranx][rany],rmq[tx][ty][ranx][rany])));
  }

 

二维RMQ