Cornfields poj2019 二维RMQ

Cornfields

Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u

Submit Status

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

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <math.h>
 5 #include <algorithm>
 6 using namespace std;
 7 unsigned char dpi[300][300][9],dpa[300][300][9];
 8 int main()
 9 {
10     int n,b,k;
11     int i,j,ii,jj,x,y;
12     unsigned char maxa,mina;
13     scanf("%d%d%d",&n,&b,&k);
14     memset(dpi,0,sizeof(dpi));
15     memset(dpa,0,sizeof(dpa));
16     for(i=0; i<n; i++)
17         for(j=0; j<n; j++)
18             scanf("%d",&x),dpa[i][j][0]=x,dpi[i][j][0]=dpa[i][j][0];
19     for(ii=1; ii<9; ii++)
20     {
21         for(i=0; i+(1<<ii) - 1 <n; i++)
22         {
23             for(j=0; j+(1<<ii)-1<n; j++)
24             {
25                dpi[i][j][ii]=min(dpi[i][j][ii-1],dpi[i+(1<<(ii-1))][j][ii-1]);
26                dpi[i][j][ii]=min(dpi[i][j][ii],dpi[i][j+(1<<(ii-1))][ii-1]);
27                dpi[i][j][ii]=min(dpi[i][j][ii],dpi[i+(1<<(ii-1))][j+(1<<(ii-1))][ii-1]);
28                dpa[i][j][ii]=max(dpa[i][j][ii-1],dpa[i+(1<<(ii-1))][j][ii-1]);
29                dpa[i][j][ii]=max(dpa[i][j][ii],dpa[i][j+(1<<(ii-1))][ii-1]);
30                dpa[i][j][ii]=max(dpa[i][j][ii],dpa[i+(1<<(ii-1))][j+(1<<(ii-1))][ii-1]);
31             }
32         }
33     }
34     int kk=log(1.0*b)/log(2.0);
35     for(i=0; i<k; i++)
36     {
37         scanf("%d%d",&x,&y);
38         x--,y--;
39         maxa=dpa[x][y][kk];
40         maxa=max(maxa,dpa[x][y+b-(1<<kk)][kk]);
41         maxa=max(maxa,dpa[x+b-(1<<kk)][y][kk]);
42         maxa=max(maxa,dpa[x+b-(1<<kk)][y+b-(1<<kk)][kk]);
43
44         mina=dpi[x][y][kk];
45         mina=min(mina,dpi[x][y+b-(1<<kk)][kk]);
46         mina=min(mina,dpi[x+b-(1<<kk)][y][kk]);
47         mina=min(mina,dpi[x+b-(1<<kk)][y+b-(1<<kk)][kk]);
48         printf("%d\n",maxa-mina);
49     }
50 }

Cornfields poj2019 二维RMQ

时间: 2024-08-06 07:58:14

Cornfields poj2019 二维RMQ的相关文章

POJ 2019 Cornfields 二维RMQ

题目来源:POJ 2019 Cornfields 题意:求正方形二维区间最大最小值的差 思路:直接二维ST搞 试模版而已 #include <cstdio> #include <algorithm> #include <cmath> using namespace std; const int maxn = 255; int dp[maxn][maxn][8][8]; int dp2[maxn][maxn][8][8]; int a[maxn][maxn]; int n

HDU 2888:Check Corners(二维RMQ)

http://acm.hdu.edu.cn/showproblem.php?pid=2888 题意:给出一个n*m的矩阵,还有q个询问,对于每个询问有一对(x1,y1)和(x2,y2),求这个子矩阵中的最大值,和判断四个角有没有等于这个最大值的. 思路:二维RMQ模板题.注意内存卡的挺紧的. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <cmath> 5

【bzoj1047】[HAOI2007]理想的正方形 二维RMQ

题目描述 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 输入 第一行为3个整数,分别表示a,b,n的值第二行至第a+1行每行为b个非负整数,表示矩阵中相应位置上的数.每行相邻两数之间用一空格分隔.100%的数据2<=a,b<=1000,n<=a,n<=b,n<=1000 输出 仅一个整数,为a*b矩阵中所有“n*n正方形区域中的最大整数和最小整数的差值”的最小值. 样例输入 5 4 2 1 2 5 6 0 1

[hdu2888]二维RMQ

题意:求矩形内最大值.二维RMQ. 1 #pragma comment(linker, "/STACK:10240000,10240000") 2 3 #include <iostream> 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <cstring> 8 #include <map> 9 #include

HDU2888 Check Corners(二维RMQ)

有一个矩阵,每次查询一个子矩阵,判断这个子矩阵的最大值是不是在这个子矩阵的四个角上 裸的二维RMQ 1 #pragma comment(linker, "/STACK:1677721600") 2 #include <map> 3 #include <set> 4 #include <stack> 5 #include <queue> 6 #include <cmath> 7 #include <ctime> 8

P2216 [HAOI2007]理想的正方形(二维RMQ)

题目描述 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 输入输出格式 输入格式: 第一行为3个整数,分别表示a,b,n的值 第二行至第a+1行每行为b个非负整数,表示矩阵中相应位置上的数.每行相邻两数之间用一空格分隔. 输出格式: 仅一个整数,为a*b矩阵中所有“n*n正方形区域中的最大整数和最小整数的差值”的最小值. 输入输出样例 输入样例#1: 5 4 2 1 2 5 6 0 17 16 0 16 17 2 1 2 10 2

POJ--2019--Cornfields【二维RMQ】

链接:http://poj.org/problem?id=2019 题意:给你一个n*n的矩阵,q次询问,每次询问给出左上角的坐标,询问以这个点为左上角的b*b的子矩阵中最大值和最小值的差. 思路:二维RMQ的基本应用,网上找的模板 这道题是USACO的,数据很水,暴力也能过. #include<cstring> #include<string> #include<fstream> #include<iostream> #include<iomanip

HDU 2888:Check Corners 二维RMQ

Check Corners 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2888 题意: 求矩形区间内的最大值 题解: 二维RMQ,和一维的区别不大,按一维的做法求出每一行上的RMQ,再处理行与行之间的关系就好了. 代码 #include<stdio.h> const int N=301; const int M=301; int dp[N][M][9][9]; int mmax(int x,int y) { return x>y?x:

hdu1823 Luck and Love 二维RMQ(二维线段树)

题意:给定你二维范围,找这个范围里面的最大值 解题思路:二维线段树有两种实现方式,一种是  树套树  ,另一种则是将二维平面分成4块的  完全四叉树 我的代码: // File Name: 1823.cpp // Author: darkdream // Created Time: 2014年07月10日 星期四 09时38分05秒 #include<vector> #include<list> #include<map> #include<set> #in