POJ 2019 Cornfields (二维RMQ)
Cornfields
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 Sample Output 5 Source |
二维RMQ。
和一维的差不多。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std; int val[][];
int mm[];
int dpmin[][][][];//最小值
int dpmax[][][][];//最大值 void initRMQ(int n,int m)
{
for(int i = ;i <= n;i++)
for(int j = ;j <= m;j++)
dpmin[i][j][][] = dpmax[i][j][][] = val[i][j];
for(int ii = ; ii <= mm[n]; ii++)
for(int jj = ; jj <= mm[m]; jj++)
if(ii+jj)
for(int i = ; i + (<<ii) - <= n; i++)
for(int j = ; j + (<<jj) - <= m; j++)
{
if(ii)
{
dpmin[i][j][ii][jj] = min(dpmin[i][j][ii-][jj],dpmin[i+(<<(ii-))][j][ii-][jj]);
dpmax[i][j][ii][jj] = max(dpmax[i][j][ii-][jj],dpmax[i+(<<(ii-))][j][ii-][jj]);
}
else
{
dpmin[i][j][ii][jj] = min(dpmin[i][j][ii][jj-],dpmin[i][j+(<<(jj-))][ii][jj-]);
dpmax[i][j][ii][jj] = max(dpmax[i][j][ii][jj-],dpmax[i][j+(<<(jj-))][ii][jj-]);
}
}
}
//查询矩形的最大值
int rmq1(int x1,int y1,int x2,int y2)
{
int k1 = mm[x2-x1+];
int k2 = mm[y2-y1+];
x2 = x2 - (<<k1) + ;
y2 = y2 - (<<k2) + ;
return max(max(dpmax[x1][y1][k1][k2],dpmax[x1][y2][k1][k2]),max(dpmax[x2][y1][k1][k2],dpmax[x2][y2][k1][k2]));
}
//查询矩形的最小值
int rmq2(int x1,int y1,int x2,int y2)
{
int k1 = mm[x2-x1+];
int k2 = mm[y2-y1+];
x2 = x2 - (<<k1) + ;
y2 = y2 - (<<k2) + ;
return min(min(dpmin[x1][y1][k1][k2],dpmin[x1][y2][k1][k2]),min(dpmin[x2][y1][k1][k2],dpmin[x2][y2][k1][k2]));
} int main()
{
mm[] = -;
for(int i = ;i <= ;i++)
mm[i] = ((i&(i-))==)?mm[i-]+:mm[i-];
int N,B,K;
while(scanf("%d%d%d",&N,&B,&K)==)
{
for(int i = ;i <= N;i++)
for(int j = ;j <= N;j++)
scanf("%d",&val[i][j]);
initRMQ(N,N);
int x,y;
while(K--)
{
scanf("%d%d",&x,&y);
printf("%d\n",rmq1(x,y,x+B-,y+B-)-rmq2(x,y,x+B-,y+B-));
}
}
return ;
}
POJ 2019 Cornfields (二维RMQ)的更多相关文章
- POJ 2019 Cornfields [二维RMQ]
题目传送门 Cornfields Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7963 Accepted: 3822 ...
- POJ 2019 Cornfields 二维线段树的初始化与最值查询
模板到不行.. 连更新都没有.. .存个模板. 理解留到小结的时候再写. #include <algorithm> #include <iostream> #include & ...
- [poj2019]Cornfields(二维RMQ)
题意:给你一个n*n的矩阵,让你从中圈定一个小矩阵,其大小为b*b,有q个询问,每次询问告诉你小矩阵的左上角,求小矩阵内的最大值和最小值的差. 解题关键:二维st表模板题. 预处理复杂度:$O({n^ ...
- [POJ 2019] Cornfields
Cornfields Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5516 Accepted: 2714 Descri ...
- poj2019 二维RMQ裸题
Cornfields Time Limit: 1000MS Memory Limit: 30000K Total Submissions:8623 Accepted: 4100 Descrip ...
- hdu2888 二维RMQ
Check Corners Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hduacm 2888 ----二维rmq
http://acm.hdu.edu.cn/showproblem.php?pid=2888 模板题 直接用二维rmq 读入数据时比较坑爹 cin 会超时 #include <cstdio& ...
- hdu 2888 二维RMQ模板题
Check Corners Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 2888 Check Corners (模板题)【二维RMQ】
<题目链接> <转载于 >>> > 题目大意: 给出一个N*M的矩阵,并且给出该矩阵上每个点对应的值,再进行Q次询问,每次询问给出代询问子矩阵的左上顶点和右下 ...
随机推荐
- nodejs 使用redis 管理session
一.在开发机安装redis并远程连接 因本人的远程开发机配置原因,使用jumbo安装redis 首先登录开发机,并使用jumbo 安装redis:jumbo install redis 查看redis ...
- 使用node.js做一个自用的天气插件
var request = require('request') var url = 'http://www.baidu.com/home/xman/data/superload' var cooki ...
- iptables 操作
iptables --list 查看列表 iptables删除规则 iptables -nL --line-number Chain INPUT (policy ACCEPT)num target p ...
- 关于JqueryEasyUI插件—Tab,默认选中某个面板 如果不明显指定的话,第一个就是被选中的
如果不明显指定的话,第一个就是被选中的,你可以通过data-options="selected:true"指定默认选中
- mybatis 一级缓存和二级缓存
1.默认是会话期内 一级session缓存 2.二级缓存: 引入二级缓存的jar, 配置 ehcache.xml, mapper.xml引入缓存<cache type="org.myb ...
- HDU-1083
Courses Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- hdu 1065(贪心)
Wooden Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20938 Accepted: 8872 De ...
- AttributeError: 'ForeignKey' object has no attribute 're' 解决办法
使用 field_object.rel.model.objects.filter(**db_condition) 报错 forekey中存在rel,为什么不能调用? 通过以下语句观察 print(fi ...
- Codeforces GYM 101968 A. Tree Game
差点自闭,感谢大佬帮忙找bug 题目:https://codeforces.com/gym/101968/problem/A 找树的重心+思维 找到树的重心,如果重心只有一个,以重心为根节点dfs,求 ...
- leetcode104 Maximum Depth
题意:二叉树最大深度 思路:递归,但是不知道怎么回事直接在return里面计算总是报超时,用俩变量就可以A···奇怪,没想通 代码: int maxDepth(TreeNode* root) { if ...