POJ_2019_Cornfields
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 7444 | Accepted: 3609 |
Description
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
* 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
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
- 二维区间最值RMQ
- 用二维ST表即可
- #include <iostream>
- #include <string>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <climits>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <stack>
- #include <set>
- #include <map>
- using namespace std;
- typedef long long LL ;
- typedef unsigned long long ULL ;
- const int maxn = 1e5 + ;
- const int inf = 0x3f3f3f3f ;
- const int npos = - ;
- const int mod = 1e9 + ;
- const int mxx = + ;
- const double eps = 1e- ;
- const double PI = acos(-1.0) ;
- int max4(int a, int b, int c, int d){
- return max(max(a,b),max(c,d));
- }
- int min4(int a, int b, int c, int d){
- return min(min(a,b),min(c,d));
- }
- int mx[][][][], mi[][][][], fac[];
- int RMQmx(int x1, int y1, int x2, int y2, int m){
- int k=(int)(log((double)m)/log(2.0));
- return max4(mx[x1][y1][k][k],mx[x1][y2-fac[k]+][k][k],mx[x2-fac[k]+][y1][k][k],mx[x2-fac[k]+][y2-fac[k]+][k][k]);
- }
- int RMQmi(int x1, int y1, int x2, int y2, int m){
- int k=(int)(log((double)m)/log(2.0));
- return min4(mi[x1][y1][k][k],mi[x1][y2-fac[k]+][k][k],mi[x2-fac[k]+][y1][k][k],mi[x2-fac[k]+][y2-fac[k]+][k][k]);
- }
- int n, m, q, t, u, v;
- int main(){
- // freopen("in.txt","r",stdin);
- // freopen("out.txt","w",stdout);
- for(int i=;i<;i++)
- fac[i]=(<<i);
- while(~scanf("%d %d %d",&n,&m,&q)){
- for(int i=;i<=n;i++)
- for(int j=;j<=n;j++){
- scanf("%d",&t);
- mx[i][j][][]=t;
- mi[i][j][][]=t;
- }
- int k=(int)(log((double)n)/log(2.0));
- // [x][y][1<<e][1<<f]
- for(int e=;e<=k;e++)
- for(int f=;f<=k;f++)
- for(int i=;i+fac[e]-<=n;i++)
- for(int j=;j+fac[f]-<=n;j++){
- mx[i][j][e][f]=max4(mx[i][j][e-][f-],mx[i+fac[e-]][j][e-][f-],mx[i][j+fac[f-]][e-][f-],mx[i+fac[e-]][j+fac[f-]][e-][f-]);
- mi[i][j][e][f]=min4(mi[i][j][e-][f-],mi[i+fac[e-]][j][e-][f-],mi[i][j+fac[f-]][e-][f-],mi[i+fac[e-]][j+fac[f-]][e-][f-]);
- }
- while(q--){
- scanf("%d %d",&u,&v);
- printf("%d\n",RMQmx(u,v,u+m-,v+m-,m)-RMQmi(u,v,u+m-,v+m-,m));
- }
- }
- return ;
- }
POJ_2019_Cornfields的更多相关文章
随机推荐
- 使用Ajax long polling实现简单的聊天程序
关于web实时通信,通常使用长轮询或这长连接方式进行实现. 为了能够实际体会长轮询,通过Ajax长轮询实现了一个简单的聊天程序,在此作为笔记. 长轮询 传统的轮询方式是,客户端定时(一般使用setIn ...
- weblogic创建域生产模式,输入用户名闪退
weblogic创建域,生产模式,报错 <2017-12-29 下午04时53分59秒 CST> <Info> <Security> <BEA-090065& ...
- Data Guard启动实时日志应用
1. REDO数据实时应用 启动实时应用的优势在于,REDO数据不需要等待归档完成,接收到即可被应用,这样执行角色切换时,操作能够执行得更快,因为日志是被即时应用的. 要启动实时应用也简单,前提是St ...
- Libjingle库 综述
国内现在很多语音聊天工具都是基于TURN方式实现的,包括YY.AK等等,这种方式对于服务器的性能要求很高,而且在用户量增大的时候,服务器压力也会越来越大,用户的语音质量也会受到很大影响.而基于P2P方 ...
- springboot 集成elasticsearch
In this article, we will discuss about “How to create a Spring Boot + Spring Data + Elasticsearch Ex ...
- intellij中使用git插件将项目上传到码云
参考帖子: git上传本地Intellij idea 项目到码云的git仓库中(评论中有彩蛋,一定要看) IntelliJ-IDEA和Git.GitHub.Gitlab的使用 使用idea上传项目到码 ...
- PHP 简易导出excel 类解决Excel 打开乱码
<?php class exportCsv{ //列名 protected $_column = array(); protected $_reg = array(); public $ret ...
- PHP 函数引用传值
<?php /* * @1 $arr = array_fill(1,100,'bbb'); echo memory_get_usage()."<br>"; fun ...
- linux-find【递归搜索文件名】
1 命令格式: $ find <指定目录> <指定条件> <指定动作> 最常见示例:查找文件名(忽略大小写) $find . -iname sqlquery.j ...
- 《转》python学习(10)-集合
转自 http://www.cnblogs.com/BeginMan/p/3160565.html 一.目录 1.集合概述 2.关于集合的操作符.关系符号 3.集合的一系列操作(添加.更新.访问.删除 ...