B - Cornfields
Crawling in process...
Crawling failed
Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u
Appoint description:
Description
FJ has decided to grow his own corn hybrid in order to help the cows make the best possible milk. To that end, he's looking to build the cornfield on the flattest piece of land he can find.
FJ has, at great expense, surveyed his square farm of N x N hectares (1 <= N <= 250). Each hectare has an integer elevation (0 <= elevation <= 250) associated with it.
FJ will present your program with the elevations and a set of K (1 <= K <= 100,000) queries of the form "in this B x B submatrix, what is the maximum and minimum elevation?". The integer B (1 <= B <= N) is the size of one edge of the square cornfield and is a constant for every inquiry. Help FJ find the best place to put his cornfield.
FJ has, at great expense, surveyed his square farm of N x N hectares (1 <= N <= 250). Each hectare has an integer elevation (0 <= elevation <= 250) associated with it.
FJ will present your program with the elevations and a set of K (1 <= K <= 100,000) queries of the form "in this B x B submatrix, what is the maximum and minimum elevation?". The integer B (1 <= B <= N) is the size of one edge of the square cornfield and is a constant for every inquiry. Help FJ find the best place to put his cornfield.
Input
* Line 1: Three space-separated integers: N, B, and K.
* Lines 2..N+1: Each line contains N space-separated integers. Line 2 represents row 1; line 3 represents row 2, etc. The first integer on each line represents column 1; the second integer represents column 2; etc.
* Lines N+2..N+K+1: Each line contains two space-separated integers representing a query. The first integer is the top row of the query; the second integer is the left column of the query. The integers are in the range 1..N-B+1.
* Lines 2..N+1: Each line contains N space-separated integers. Line 2 represents row 1; line 3 represents row 2, etc. The first integer on each line represents column 1; the second integer represents column 2; etc.
* Lines N+2..N+K+1: Each line contains two space-separated integers representing a query. The first integer is the top row of the query; the second integer is the left column of the query. The integers are in the range 1..N-B+1.
Output
* Lines 1..K: A single integer per line representing the difference between the max and the min in each query.
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
#include<iostream> #include<cstring> #include<cstdio> #define ll(x) (1<<x) #define clr(f,z) memset(f,z,sizeof(f)) #define FOR(i,a,b) for(int i=a;i<=b;++i) using namespace std; const int mm=255; int rmqb[mm][mm][9][9],rmqm[mm][mm][9][9]; int f[mm][mm]; int N,B,K,bit[mm]; void initRMQ() { bit[0]=-1; FOR(i,1,mm-1)bit[i]=(i&(i-1))==0?bit[i-1]+1:bit[i-1]; FOR(i,1,N)FOR(j,1,N) rmqb[i][j][0][0]=rmqm[i][j][0][0]=f[i][j]; FOR(r,0,bit[N])FOR(c,0,bit[N]) if(r+c) for(int i=1;i+ll(r)-1<=N;++i) for(int j=1;j+ll(c)-1<=N;++j) { if(r) { rmqm[i][j][r][c]=min(rmqm[i][j][r-1][c],rmqm[i+ll(r-1)][j][r-1][c]); rmqb[i][j][r][c]=max(rmqb[i][j][r-1][c],rmqb[i+ll(r-1)][j][r-1][c]); } else { rmqm[i][j][r][c]=min(rmqm[i][j][r][c-1],rmqm[i][j+ll(c-1)][r][c-1]); rmqb[i][j][r][c]=max(rmqb[i][j][r][c-1],rmqb[i][j+ll(c-1)][r][c-1]); } } } int RMQ(int r1,int c1,int r2,int c2) { int t1,t2; t1=bit[r2-r1+1]; t2=bit[c2-c1+1]; r2-=ll(t1)-1; c2-=ll(t2)-1; int a,b,z; a=min(rmqm[r1][c1][t1][t2],rmqm[r2][c1][t1][t2]); b=min(rmqm[r1][c2][t1][t2],rmqm[r2][c2][t1][t2]); z=min(a,b); a=max(rmqb[r1][c1][t1][t2],rmqb[r2][c1][t1][t2]); b=max(rmqb[r1][c2][t1][t2],rmqb[r2][c2][t1][t2]); return max(a,b)-z; } int main() { while(~scanf("%d%d%d",&N,&B,&K)) { FOR(i,1,N)FOR(j,1,N) scanf("%d",&f[i][j]); initRMQ(); int r,c; while(K--) { scanf("%d%d",&r,&c); printf("%d\n",RMQ(r,c,r+B-1,c+B-1)); } } return 0; }
本文介绍了一种用于帮助农民寻找其农场中最平坦区域以建立玉米田的算法。该算法通过处理农场地形数据并快速响应查询请求来确定指定大小的玉米田内最大与最小海拔高度差,从而辅助决定最佳种植位置。
873

被折叠的 条评论
为什么被折叠?



