首页 技术 正文
技术 2022年11月15日
0 收藏 891 点赞 3,706 浏览 2293 个字

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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,122
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,594
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,439
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,210
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,846
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,931