POJ_2019_Cornfields

2018-06-17 21:46:55来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

Cornfields
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 7444   Accepted: 3609

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. 

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. 

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

Source

USACO 2003 March Green
 
  • 二维区间最值RMQ
  • 用二维ST表即可

 

 

 1 #include <iostream>
 2 #include <string>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <cmath>
 8 #include <vector>
 9 #include <queue>
10 #include <stack>
11 #include <set>
12 #include <map>
13 using namespace std;
14 typedef long long           LL ;
15 typedef unsigned long long ULL ;
16 const int    maxn = 1e5 + 10   ;
17 const int    inf  = 0x3f3f3f3f ;
18 const int    npos = -1         ;
19 const int    mod  = 1e9 + 7    ;
20 const int    mxx  = 100 + 5    ;
21 const double eps  = 1e-6       ;
22 const double PI   = acos(-1.0) ;
23 
24 int max4(int a, int b, int c, int d){
25     return max(max(a,b),max(c,d));
26 }
27 int min4(int a, int b, int c, int d){
28     return min(min(a,b),min(c,d));
29 }
30 int mx[255][255][8][8], mi[255][255][8][8], fac[8];
31 int RMQmx(int x1, int y1, int x2, int y2, int m){
32     int k=(int)(log((double)m)/log(2.0));
33     return max4(mx[x1][y1][k][k],mx[x1][y2-fac[k]+1][k][k],mx[x2-fac[k]+1][y1][k][k],mx[x2-fac[k]+1][y2-fac[k]+1][k][k]);
34 }
35 int RMQmi(int x1, int y1, int x2, int y2, int m){
36     int k=(int)(log((double)m)/log(2.0));
37     return min4(mi[x1][y1][k][k],mi[x1][y2-fac[k]+1][k][k],mi[x2-fac[k]+1][y1][k][k],mi[x2-fac[k]+1][y2-fac[k]+1][k][k]);
38 }
39 int n, m, q, t, u, v;
40 int main(){
41     // freopen("in.txt","r",stdin);
42     // freopen("out.txt","w",stdout);
43     for(int i=0;i<8;i++)
44         fac[i]=(1<<i);
45     while(~scanf("%d %d %d",&n,&m,&q)){
46         for(int i=1;i<=n;i++)
47             for(int j=1;j<=n;j++){
48                 scanf("%d",&t);
49                 mx[i][j][0][0]=t;
50                 mi[i][j][0][0]=t;
51             }
52         int k=(int)(log((double)n)/log(2.0));
53         // [x][y][1<<e][1<<f]
54         for(int e=1;e<=k;e++)
55             for(int f=1;f<=k;f++)
56                 for(int i=1;i+fac[e]-1<=n;i++)
57                     for(int j=1;j+fac[f]-1<=n;j++){
58                         mx[i][j][e][f]=max4(mx[i][j][e-1][f-1],mx[i+fac[e-1]][j][e-1][f-1],mx[i][j+fac[f-1]][e-1][f-1],mx[i+fac[e-1]][j+fac[f-1]][e-1][f-1]);
59                         mi[i][j][e][f]=min4(mi[i][j][e-1][f-1],mi[i+fac[e-1]][j][e-1][f-1],mi[i][j+fac[f-1]][e-1][f-1],mi[i+fac[e-1]][j+fac[f-1]][e-1][f-1]);
60                     }
61         while(q--){
62             scanf("%d %d",&u,&v);
63             printf("%d\n",RMQmx(u,v,u+m-1,v+m-1,m)-RMQmi(u,v,u+m-1,v+m-1,m));
64         }
65     }
66     return 0;
67 }

 

 

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:POJ_3264_Balanced Lineup

下一篇:【luogu 3811】【模板】乘法逆元