标签:des style blog http color io os ar for
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 5516 | Accepted: 2714 |
Description
Input
Output
Sample Input
5 3 1 5 1 2 6 3 1 3 5 2 7 7 2 4 6 1 9 9 8 6 5 0 6 9 3 9 1 2
Sample Output
5
Source
#include <iostream> #include <cstdio> #include <cmath> using namespace std; #define N 255 int n,b,k; int val[N][N]; int mx[N][N][8][8]; int mi[N][N][8][8]; void ST(int n,int m) { int i,j,r,c; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { mx[i][j][0][0]=mi[i][j][0][0]=val[i][j]; } } int kn=(int)(log(double(n))/log(2.0)); int km=(int)(log(double(m))/log(2.0)); for(i=0;i<=kn;i++) { for(j=0;j<=km;j++) { if(i==0 && j==0) continue; for(r=1;r+(1<<i)-1<=n;r++) { for(c=1;c+(1<<j)-1<=m;c++) { if(i==0) { mx[r][c][i][j]=max(mx[r][c][i][j-1],mx[r][c+(1<<(j-1))][i][j-1]); mi[r][c][i][j]=min(mi[r][c][i][j-1],mi[r][c+(1<<(j-1))][i][j-1]); } else { mx[r][c][i][j]=max(mx[r][c][i-1][j],mx[r+(1<<(i-1))][c][i-1][j]); mi[r][c][i][j]=min(mi[r][c][i-1][j],mi[r+(1<<(i-1))][c][i-1][j]); } } } } } } int RMQ(int r1,int c1,int r2,int c2) { int kr=(int)(log(double(r2-r1+1))/log(2.0)); int kc=(int)(log(double(c2-c1+1))/log(2.0)); int t1=mx[r1][c1][kr][kc]; int t2=mx[r2-(1<<kr)+1][c1][kr][kc]; int t3=mx[r1][c2-(1<<kc)+1][kr][kc]; int t4=mx[r2-(1<<kr)+1][c2-(1<<kc)+1][kr][kc]; int m1=mi[r1][c1][kr][kc]; int m2=mi[r2-(1<<kr)+1][c1][kr][kc]; int m3=mi[r1][c2-(1<<kc)+1][kr][kc]; int m4=mi[r2-(1<<kr)+1][c2-(1<<kc)+1][kr][kc]; return max(max(t1,t2),max(t3,t4))-min(min(m1,m2),min(m3,m4)); } int main() { int i,j; scanf("%d%d%d",&n,&b,&k); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%d",&val[i][j]); } } ST(n,n); while(k--) { int r1,c1,r2,c2; scanf("%d%d",&r1,&c1); r2=r1+b-1; c2=c1+b-1; printf("%d\n",RMQ(r1,c1,r2,c2)); } return 0; }
正方形解法
#include <stdio.h> #include <iostream> #include <math.h> using namespace std; #define inf 0x7fffffff #define N 255 #define max(a,b) a>b?a:b #define min(a,b) a<b?a:b int n,b,k; int val[N][N]; int mx[N][N][8]; int mi[N][N][8]; int getMax(int x,int y,int p) { int res=-inf; res=max(res,mx[x][y][p]); if(x+(1<<p)<=n) res=max(res,mx[x+(1<<p)][y][p]); if(y+(1<<p)<=n) res=max(res,mx[x][y+(1<<p)][p]); if(x+(1<<p)<=n && y+(1<<p)<=n) res=max(res,mx[x+(1<<p)][y+(1<<p)][p]); return res; } int getMin(int x,int y,int p) { int res=inf; res=min(res,mi[x][y][p]); if(x+(1<<p)<=n) res=min(res,mi[x+(1<<p)][y][p]); if(y+(1<<p)<=n) res=min(res,mi[x][y+(1<<p)][p]); if(x+(1<<p)<=n && y+(1<<p)<=n) res=min(res,mi[x+(1<<p)][y+(1<<p)][p]); return res; } void ST() { int i,j,k; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { mx[i][j][0]=mi[i][j][0]=val[i][j]; } } int kn=(int)(log(n*1.0)/log(2.0)); for(k=1;k<=kn;k++) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { mx[i][j][k]=getMax(i,j,k-1); mi[i][j][k]=getMin(i,j,k-1); } } } } int RMQMAX(int x,int y,int b) { int p=(int)(log(b*1.0)/log(2.0)); int res=-inf; res=max(res,mx[x][y][p]); if(x+b-(1<<p)<=n) res=max(res,mx[x+b-(1<<p)][y][p]); if(y+b-(1<<p)<=n) res=max(res,mx[x][y+b-(1<<p)][p]); if(x+b-(1<<p)<=n && y+b-(1<<p)<=n) res=max(res,mx[x+b-(1<<p)][y+b-(1<<p)][p]); return res; } int RMQMIN(int x,int y,int b) { int p=(int)(log(b*1.0)/log(2.0)); int res=inf; res=min(res,mi[x][y][p]); if(x+b-(1<<p)<=n) res=min(res,mi[x+b-(1<<p)][y][p]); if(y+b-(1<<p)<=n) res=min(res,mi[x][y+b-(1<<p)][p]); if(x+b-(1<<p)<=n && y+b-(1<<p)<=n) res=min(res,mi[x+b-(1<<p)][y+b-(1<<p)][p]); return res; } int main() { int i,j; scanf("%d%d%d",&n,&b,&k); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%d",&val[i][j]); } } ST(); while(k--) { int x,y; scanf("%d%d",&x,&y); printf("%d\n",RMQMAX(x,y,b)-RMQMIN(x,y,b)); } return 0; }
标签:des style blog http color io os ar for
原文地址:http://www.cnblogs.com/hate13/p/4045719.html