ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

poj2019 二维RMQ裸题

2019-07-15 17:54:48  阅读:202  来源: 互联网

标签:RMQ query int 裸题 poj2019 integer MAXN line include


Cornfields
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions:8623   Accepted: 4100

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



C++代码
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <set>
 6 #include <vector>
 7 #include <map>
 8 #include <queue>
 9 #include <set>
10 #include <math.h>
11 #include <algorithm>
12 using namespace std;
13 #define MAXN 250 + 5
14 int dp[MAXN][MAXN][20];
15 int dp1[MAXN][MAXN][20];
16 int a[MAXN][MAXN];
17 int n,m;
18 void st(){
19     for(int i=1;i<=n;i++)
20     for(int k=0;(1<<k)<=m;k++){
21     for(int j=1;j+(1<<k)-1<=m;j++){
22         if(k==0){
23             dp[i][j][k]=dp1[i][j][k]=a[i][j];
24         }
25         else {
26             dp[i][j][k]=max(dp[i][j][k-1],dp[i][j+(1<<(k-1))][k-1]);
27             dp1[i][j][k]=min(dp1[i][j][k-1],dp1[i][j+(1<<(k-1))][k-1]);
28         }
29     }
30     }
31 }
32 int rmq2dmax(int x,int y,int x1,int y1){
33     int k=log2(y1-y+1);
34     int mm=max(dp[x][y][k],dp[x][y1-(1<<k)+1][k]);
35     for(int i=x+1;i<=x1;i++)
36         mm=max(mm,max(dp[i][y][k],dp[i][y1-(1<<k)+1][k]));
37     return mm;
38 }
39 int rmq2dmin(int x,int y,int x1,int y1){
40     int k=log2(y1-y+1);
41     int mm=min(dp1[x][y][k],dp1[x][y1-(1<<k)+1][k]);
42     for(int i=x+1;i<=x1;i++)
43         mm=min(mm,min(dp1[i][y][k],dp1[i][y1-(1<<k)+1][k]));
44     return mm;
45 }
46 
47 
48 int main(int argc, char const *argv[])
49 {
50     int b,k;
51     scanf("%d%d%d",&n,&b,&k);
52     m = n;
53     for(int i = 1;i <= n; i++){
54         for(int j = 1;j <= n ; j++){
55             scanf("%d",&a[i][j]);
56         }
57     }
58     st();
59     while(k--){
60         int p,q;
61         scanf("%d%d",&p,&q);
62         cout << rmq2dmax(p,q,p + b - 1,q + b - 1) - rmq2dmin(p,q,p + b - 1,q + b - 1)<< endl;
63     }
64     return 0;
65 }
二维RMQ

 

标签:RMQ,query,int,裸题,poj2019,integer,MAXN,line,include
来源: https://www.cnblogs.com/DWVictor/p/11190343.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有