poj 2019 Cornfields

Posted zjxxcn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 2019 Cornfields相关的知识,希望对你有一定的参考价值。

Cornfields
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 9378   Accepted: 4420

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

二维线段树,也可以一维+RMQ
技术图片
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;  
 7 #define lch (x<<1)  
 8 #define rch (x<<1|1)  
 9 #define mid (l+r)/2  
10 int const N=250+10;  
11 int t[N<<2][N<<2],s[N<<2][N<<2],n,b,k,a[N][N],ansx,ansy;   
12 
13 void buildy(int o,int x,int l,int r,int ll,int rr){
14     if(l==r){
15         if(ll==rr)  {
16             t[o][x]=s[o][x]=a[ll][l];  
17         }else {
18             t[o][x]=min(t[o*2][x],t[o*2+1][x]);  
19             s[o][x]=max(s[o*2][x],s[o*2+1][x]);   
20         }
21         //cout<<o<<" "<<x<<" "<<t[o][x]<<" "<<s[o][x]<<endl;  
22         return ;  
23     }
24     buildy(o,lch,l,mid,ll,rr);  
25     buildy(o,rch,mid+1,r,ll,rr);  
26     t[o][x]=min(t[o][lch],t[o][rch]);   
27     s[o][x]=max(s[o][lch],s[o][rch]);  
28 }
29 void buildx(int x,int l,int r){  
30     if(l!=r){
31         buildx(lch,l,mid);  
32         buildx(rch,mid+1,r);
33     }  
34     buildy(x,1,1,n,l,r); 
35 } 
36 void queryy(int o,int x,int l,int r,int y1,int y2){
37     if(y1<=l && r<=y2){
38         ansx=min(ansx,t[o][x]);  
39         ansy=max(ansy,s[o][x]);  
40         return; 
41     }
42     if(y1<=mid) queryy(o,lch,l,mid,y1,y2);  
43     if(y2>mid)  queryy(o,rch,mid+1,r,y1,y2);  
44 }
45 void queryx(int x,int l,int r,int x1,int x2,int y1,int y2){
46     if(x1<=l && r<=x2) {
47         queryy(x,1,1,n,y1,y2);  
48         return ; 
49     }
50     if(x1<=mid) queryx(lch,l,mid,x1,x2,y1,y2);  
51     if(x2>mid)  queryx(rch,mid+1,r,x1,x2,y1,y2);  
52 }
53 int main(){
54     scanf("%d%d%d",&n,&b,&k);  
55     for(int i=1;i<=n;i++)  
56         for(int j=1;j<=n;j++)  
57             scanf("%d",&a[i][j]);  
58     buildx(1,1,n);  
59     //cout<<"***"<<endl;  
60     while (k--){
61         int x,y;  
62         scanf("%d%d",&x,&y);  
63         ansx=300,ansy=-1;  
64         queryx(1,1,n,x,x+b-1,y,y+b-1); 
65         printf("%d
",ansy-ansx);   
66     }
67     return 0; 
68 }
View Code

 

以上是关于poj 2019 Cornfields的主要内容,如果未能解决你的问题,请参考以下文章

Cornfields(poj2019)

POJ2019 Cornfields 二维ST表

POJ-2019 Cornfields(二维RMQ)

POJ 2019 Cornfields 二维线段树的初始化与最值查询

POJ 3254 Corn Fields(状压DP)

POJ2778DNA Sequence(AC自动机)