【LightOJ 1081】Square Queries(二维RMQ降维)
Little Tommy is playing a game. The game is played on a 2D N x N grid. There is an integer in each cell of the grid. The rows and columns are numbered from 1 to N.
At first the board is shown. When the user presses a key, the screen shows three integers I, J, Swhich designates a square (I, J) to (I+S-1, J+S-1) in the grid. The player has to predict the largest integer found in this square. The user will be given points based on the difference between the actual result and the given result.
Tommy doesn't like to lose. So, he made a plan, he will take help of a computer to generate the result. But since he is not a good programmer, he is seeking your help.
Input
Input starts with an integer T (≤ 3), denoting the number of test cases.
The first line of a case is a blank line. The next line contains two integers N (1 ≤ N ≤ 500), Q (0 ≤ Q ≤ 50000). Each of the next N lines will contain N space separated integers forming the grid. All the integers will be between 0 and 105.
Each of the next Q lines will contain a query which is in the form I J S (1 ≤ I, J ≤ N and 1 ≤ I + S, J + S < N and S > 0).
Output
For each test case, print the case number in a single line. Then for each query you have to print the maximum integer found in the square whose top left corner is (I, J) and whose bottom right corner is (I+S-1, J+S-1).
Sample Input
1
4 5
67 1 2 3
8 88 21 1
89 12 0 12
5 5 5 5
1 1 2
1 3 2
3 3 2
1 1 4
2 2 3
Sample Output
Case 1:
88
21
12
89
88
题意:
给定一个n*n(n<=500)的矩阵(即是正方形),每次询问以(x,y)为左上角,边长为s的正方形区域内的最大值。
题解:
用一般的二维RMQ预处理会超时。
因为所给矩阵是为正方形,所以我们每次只用存储正方形即可。
dp[i][j][k]:以(i,j)为左上角,边长为2^k的正方形区域内的最大值,每次倍增只需把大正方形拆成4个小正方形就好了。
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
const int MAX=;
int dp[MAX][MAX][],mm[MAX],val[MAX][MAX];
void initrmq(int n)
{
int lt,lb,rt,rb;
for(int k=;k<=mm[n];k++)
for(int i=;i+(<<k)-<=n;i++)
for(int j=;j+(<<k)-<=n;j++)
if(k==)
dp[i][j][k]=val[i][j];
else
{
lt=dp[i][j][k-]; //左上角
lb=dp[i+(<<k-)][j][k-]; //左下角
rt=dp[i][j+(<<k-)][k-]; //右上角
rb=dp[i+(<<k-)][j+(<<k-)][k-];//右下角
dp[i][j][k]=max(max(lt,lb),max(rt,rb));
}
}
int rmq(int x,int y,int s)
{
if(s==)return val[x][y];
int k=mm[s];
int lt=dp[x][y][k];
int lb=dp[x+s-(<<k)][y][k];
int rt=dp[x][y+s-(<<k)][k];
int rb=dp[x+s-(<<k)][y+s-(<<k)][k];
return max(max(lt,lb),max(rt,rb));
}
int main()
{
int i,j,k,T;
mm[]=-;
for(i=;i<=MAX;i++)
mm[i]=((i&(i-))==)?mm[i-]+:mm[i-];
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
int n,q;
scanf("%d%d",&n,&q);
for(i=;i<=n;i++)
for(j=;j<=n;j++)
scanf("%d",&val[i][j]);
initrmq(n);
printf("Case %d:\n",cas);
while(q--)
{
int x,y,s;
scanf("%d%d%d",&x,&y,&s);
printf("%d\n",rmq(x,y,s));
}
}
return ;
}
【LightOJ 1081】Square Queries(二维RMQ降维)的更多相关文章
- POJ 2019 Cornfields [二维RMQ]
题目传送门 Cornfields Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7963 Accepted: 3822 ...
- POJ 2019 Cornfields (二维RMQ)
Cornfields Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4911 Accepted: 2392 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 ...
- hdu 2888 二维RMQ模板题
Check Corners Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 【HDOJ 2888】Check Corners(裸二维RMQ)
Problem Description Paul draw a big m*n matrix A last month, whose entries Ai,j are all integer numb ...
- hdu 2888 二维RMQ
Check Corners Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hduacm 2888 ----二维rmq
http://acm.hdu.edu.cn/showproblem.php?pid=2888 模板题 直接用二维rmq 读入数据时比较坑爹 cin 会超时 #include <cstdio& ...
- HDU 2888 Check Corners (模板题)【二维RMQ】
<题目链接> <转载于 >>> > 题目大意: 给出一个N*M的矩阵,并且给出该矩阵上每个点对应的值,再进行Q次询问,每次询问给出代询问子矩阵的左上顶点和右下 ...
随机推荐
- freebsd默认不是gcc构建
xunsearch在freebsd上死活装不上,用gcc编译通过. freebsd默认不是gcc编译工程,所以下个gcc ./configure CC=gcc48 编译. 觉得clang编译器不可能有 ...
- python小练习2
结果 代码 鞋子价格=0 男孩价格=0 爆米花价格=0 计算完毕=0 for 鞋子动态价格 in range(0,20): if (计算完毕==1): break; #print("鞋子动态 ...
- mongodb 3.4复制搭建
mongodb数据库主从复制的原理:在主从结构中,主节点的操作记录称为oplog(operation log).oplog存储在一个系统数据库local的集合oplog.$main中,这个集合的每个文 ...
- Oracle数据库从入门到精通 多表查询知识以及范例
视频课程:李兴华 Oracle从入门到精通视频课程 学习者:阳光罗诺 视频来源:51CTO学院 总体内容: 多表查询的意义以及基本问题. 表的连接查询 SQL:1999语法标准对多表查询的支持. 数据 ...
- Oracle案例06—— OGG-01098 Could not flush "./dirdat/e1000004383" (error 28, No space left on device)
一.前言 自从换了新环境,各种问题层出不穷,如果不是之前积累的经验丰富,估计都歇菜了,看来作为数据库全栈工程师(oracle/mysql/sqlserver/sap hana/pg/mongodb/r ...
- RunDll32.exe 详解及[Windows批处理]清除IE缓存
Rundll32命令详解 文件作用:执行32位DLL文件中的内部函数 位置:X:(当前系统分区)\windows\system32 命令语法: Rundll32.exe DLLname,Functio ...
- 双十一问题:kafka消费能力低下原因思考
抛去cpu.内存等机器原因,在每个分区皆分配一个进程消费的情况下,利用扩机器来提高kafka消费速率已无能为力 此时发现,在实际洪峰时段的消费速率元达不到先前压测时的消费速率 原因思考: 1.洪峰时段 ...
- VMware workstation 虚拟机安装帮助文档(以windows server 2003为例)
本次安装以Windows server 2003为例: 1.在桌面上双击VMware快捷方式打开,并点击文件>新建虚拟机 2.这里选择默认的“典型”,点击下一步 3.选择浏览,找到windows ...
- CSAPP Bomb Lab记录
记录关于CSAPP 二进制炸弹实验过程 (CSAPP配套教学网站Bomb Lab自学版本,实验地址:http://csapp.cs.cmu.edu/2e/labs.html) (个人体验:对x86汇编 ...
- SOJ4478 Easy Problem II(模拟、栈)
Time Limit: 3000 MS Memory Limit: 131072 K Description 在数据结构中 我们学习过 栈 这种数据结构 通过栈 我们可以将1,2,3,...,n转化成 ...