Matrix Searching


Time Limit: 10 Seconds     
Memory Limit: 32768 KB


Given an n*n matrix A, whose entries Ai,j are integer numbers ( 1 <= i <= n, 1 <= j <= n ). An operation FIND the minimun number in a given ssub-matrix.

Input

The first line of the input contains a single integer T , the number of test cases.

For each test case, the first line contains one integer n (1 <= n <= 300), which is the sizes of the matrix, respectively. The next n lines with n integers each gives the elements of the matrix.

The next line contains a single integer N (1 <= N <= 1,000,000), the number of queries. The next N lines give one query on each line, with four integers r1, c1, r2, c2 (1 <= r1 <= r2 <= n, 1 <= c1 <= c2 <= n), which are the indices of the upper-left corner
and lower-right corner of the sub-matrix in question.

Output

For each test case, print N lines with one number on each line, the required minimum integer in the sub-matrix.

Sample Input

1

2

2 -1

2 3

2

1 1 2 2

1 1 2 1

Sample Output

-1

2


Author: PENG, Peng

Source: ZOJ Monthly, June 2007

帮学妹找了一晚上的bug。,,各种调试,。各种报错,。感觉自己水爆了,,赶紧水道高级点的水题压压惊~~

ac代码

#include<stdio.h>
#include<string.h>
#include<math.h>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a>b?b:a)
int map[301][301];
int minv[301][301][9][9];
int mm[306];
int n,m;
void initrmq()
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
minv[i][j][0][0]=map[i][j];
}
}
int ii,jj;
for(ii=0;ii<=mm[n];ii++)
for(jj=0;jj<=mm[n];jj++)
{
if(ii+jj)
{
for(i=1;i+(1<<ii)-1<=n;i++)
for(j=1;j+(1<<jj)-1<=n;j++)
{
if(ii)
minv[i][j][ii][jj]=min(minv[i][j][ii-1][jj],minv[i+(1<<(ii-1))][j][ii-1][jj]);
else
minv[i][j][ii][jj]=min(minv[i][j][ii][jj-1],minv[i][j+(1<<(jj-1))][ii][jj-1]);
}
}
}
}
int q_min(int x1,int y1,int x2,int y2)
{
int k1=mm[x2-x1+1];
int k2=mm[y2-y1+1];
x2=x2-(1<<k1)+1;
y2=y2-(1<<k2)+1;
return min(min(minv[x1][y1][k1][k2],minv[x1][y2][k1][k2]),min(minv[x2][y1][k1][k2],minv[x2][y2][k1][k2]));
}
void init()
{
mm[0]=-1;
int i;
for(i = 1;i <= 305;i++)
mm[i] = ((i&(i-1))==0)?mm[i-1]+1:mm[i-1]; }
int main()
{
int t;
init();
scanf("%d",&t);
while(t--)
{
//int n,m;
scanf("%d",&n);
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
scanf("%d",&map[i][j]);
}
initrmq();
scanf("%d",&m);
while(m--)
{
int r1,c1,r2,c2;
scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
printf("%d\n",q_min(r1,c1,r2,c2));
}
}
}

ZOJ 题目2859 Matrix Searching(二维RMQ)的更多相关文章

  1. 牛客 - 700I - Matrix Again - 二维RMQ - 二分

    https://ac.nowcoder.com/acm/contest/700/I 二维RMQ,贴个板子,注意爆内存,用char就可以了,char也可以存负数. 然后二分枚举对角线长度,理由很简单. ...

  2. ZOJ 1859 Matrix Searching(二维线段树)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1859 Matrix Searching Time Limit: 10 Seco ...

  3. zoj 2589 Matrix Searching 二维线段树

    题目链接 给一个n*n的矩阵, 给q个查询, 每次给出x1, y1, x2, y2, 求这个矩阵中的最小值. 代码基本上和上一题相同... #include<bits/stdc++.h> ...

  4. hdu 2888 二维RMQ

    Check Corners Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  5. hdu2888 二维RMQ

    Check Corners Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  6. hdu 2888 二维RMQ模板题

    Check Corners Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  7. HDU 2888 Check Corners (模板题)【二维RMQ】

    <题目链接> <转载于 >>> > 题目大意: 给出一个N*M的矩阵,并且给出该矩阵上每个点对应的值,再进行Q次询问,每次询问给出代询问子矩阵的左上顶点和右下 ...

  8. POJ 2019 Cornfields [二维RMQ]

    题目传送门 Cornfields Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7963   Accepted: 3822 ...

  9. 【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 ...

随机推荐

  1. Spring Boot (31) 数据验证

    曾经参数的验证是这样的: public String test(User user){ if(user == null){ throw new NullPointerException("u ...

  2. C#学习-EF在三层中使用

    1.搭建普通三层 DAL层,BLL层,Model层,Web层: DAL层引用Model层 BLL层引用DAL层和Model层 Web层引用BLL层和Model层 2.实现EF三层的搭建(添加引用,修改 ...

  3. 使用Spring框架的步骤

    “好记性,不如烂笔头”.今天正式接触了Spring框架,第一次接触Spring框架感觉Spring框架简化了好多程序代码,开发效率大大提高.现在介绍使用Spring框架的步骤.(使用spring-fr ...

  4. canvas一周一练 -- canvas绘制立体文字(2)

    运行效果: <!DOCTYPE html> <html> <head> </head> <body> <canvas id=" ...

  5. vue基础---Class 与 Style 绑定

    [一]绑定HTML Class (1)对象语法 ①普通绑定class <div id="area" v-bind:class="className"> ...

  6. input password密码验证跳转页面

    代码如下: 查询密码 <input type="password" id="pwd" /> 页面如下: 密码校验成功后跳转页面: window.lo ...

  7. Django-REST-Framework JWT 实现SSO认证(上)

    一.什么是Django-REST-Framework? Django-REST-framework 是基于Django框架的一个web RESTful风格开发的框架,它可以实现API接口的快速开发,但 ...

  8. Latex Notes

    latex Table of Contents 1. Presentation/Slides with Beamer 2. Drawing in LaTex With TikZ 3. Tracked ...

  9. cmake 学习笔记(一) buildsystem

    参见网址: http://www.cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html Introduction 基于CMake的构建 ...

  10. String HDU 5672(双指针)

    String HDU 5672(双指针) 传送门 题意:一个字符串中找到所有拥有不少于k个不同的字符的子串. import java.io.*; import java.util.*; public ...