Time: 844MS
Result: Accepted
Source Code
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[260][260];
int dp_min[260][260][10],dp_max[260][260][10];
int rmq_min(int t,int x,int y){
int c=log10(y-x+1)/log10(2);
return min(dp_min[t][x][c],dp_min[t][y-(1<<c)+1][c]);
}
int rmq_max(int t,int x,int y){
int c=log10(y-x+1)/log10(2);
return max(dp_max[t][x][c],dp_max[t][y-(1<<c)+1][c]);
}
int main(){
int n,m,k,i,j,q,x,y,minn,maxx;
scanf("%d%d%d",&n,&m,&k);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
dp_max[i][j][0]=a[i][j];
dp_min[i][j][0]=a[i][j];
}
for(j=1;(1<<j)<=n;j++)
for(q=1;q<=n;q++){
dp_max[i][q][j]=max(dp_max[i][q][j-1],dp_max[i][q+(1<<(j-1))][j-1]);
dp_min[i][q][j]=min(dp_min[i][q][j-1],dp_min[i][q+(1<<(j-1))][j-1]);
}
}
for(i=1;i<=k;i++){
scanf("%d%d",&x,&y);
minn=0x3f3f3f3f;
maxx=0;
for(j=x;j<x+m;j++){
minn=min(minn,rmq_min(j,y,y+m-1));
maxx=max(maxx,rmq_max(j,y,y+m-1));
}
printf("%d\n",maxx-minn);
}
return 0;
}
POJ 2019 Cornfields
最新推荐文章于 2022-01-18 22:49:56 发布