ZOJ 题目2859 Matrix Searching(二维RMQ)
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)的更多相关文章
- 牛客 - 700I - Matrix Again - 二维RMQ - 二分
https://ac.nowcoder.com/acm/contest/700/I 二维RMQ,贴个板子,注意爆内存,用char就可以了,char也可以存负数. 然后二分枚举对角线长度,理由很简单. ...
- ZOJ 1859 Matrix Searching(二维线段树)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1859 Matrix Searching Time Limit: 10 Seco ...
- zoj 2589 Matrix Searching 二维线段树
题目链接 给一个n*n的矩阵, 给q个查询, 每次给出x1, y1, x2, y2, 求这个矩阵中的最小值. 代码基本上和上一题相同... #include<bits/stdc++.h> ...
- hdu 2888 二维RMQ
Check Corners Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 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 ...
- HDU 2888 Check Corners (模板题)【二维RMQ】
<题目链接> <转载于 >>> > 题目大意: 给出一个N*M的矩阵,并且给出该矩阵上每个点对应的值,再进行Q次询问,每次询问给出代询问子矩阵的左上顶点和右下 ...
- POJ 2019 Cornfields [二维RMQ]
题目传送门 Cornfields Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7963 Accepted: 3822 ...
- 【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 ...
随机推荐
- 每天学点Linux命令:倒叙打印文件第二行的前100个大写字母
sed -n | rev 处理第二行 grep:提取大写字母 o: 不显示非结果 tr:删除换行 Cut:截取1-100个字符 rev:逆序 断断续续搞了好长时间. ...
- 打开VMware Workstation,虚拟机不见了
1 打开VM,发现虚拟机不见了 如图所示: 此时先别急着再次安装虚拟机. 2 先打开设备上所有已安装过的虚拟机,看你需要的还在不在 3 总结 如果打开后发现你要的虚拟机还存在,直接打开就好.否则,就得 ...
- PMBOK项目管理九大知识领域和五大流程 --美国IT项目管理硕士笔记(二)
PMBOK 项目管理 九大知识领域和五大流程 PMI Project Management Institute.PMI 是世界上最大的非盈利机构,是项目管理领域的领导者.PMI制定项目管理行业标准 ...
- 关于vue构建项目的一些指令
第一步: brew install nodejs(MAC机子下) Windows直接官网下载对应版本node.js 第二步: 获取nodejs模块安装目录访问权限(Windows系统忽略)sudo ...
- java基础学习日志--Stirng内存案例
案例一: public class test1 { public static void mb_swap(String Str1,String Str2) { String temp=Str1; St ...
- 洛谷——P2236 [HNOI2002]彩票
P2236 [HNOI2002]彩票 给你$m$个数,从中挑$n$个数,使得这$n$个数的倒数之和恰好等于$\frac{x}{y}$ 常见的剪纸思路: 如果当前的倒数和加上最小可能的倒数和$>$ ...
- 解决windows安装TensorFlow2.0beta版本时ERROR: Cannot uninstall 'wrapt'问题
pip install -U --ignore-installed wrapt enum34 simplejson netaddr 参考:https://bugs.launchpad.net/rall ...
- Python介绍以及Python 优缺点
Python是先编译成字节码,然后在解释执行的一门语言,而不是单纯的解释型语言 Python应用场景: Web应用开发. 操作系统管理,服务器运维的自动化脚本, 网络爬虫 科学计算 桌面软件 游戏 服 ...
- calculate Cp history (from Fluent) using Matlab
input data : unscaled time history of moment/thrust from ANSYS fluent example of input data, "m ...
- vue-router2.0二级路由的简单使用
1.app.vue中 <template> <div id="app"> <router-view></router-view> & ...