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. quartz在集群环境下的最终解决方案

    在集群环境下,大家会碰到一直困扰的问题,即多个 APP 下如何用 quartz 协调处理自动化 JOB . 大家想象一下,现在有 A , B , C3 台机器同时作为集群服务器对外统一提供 SERVI ...

  2. 在中间层 .NET 应用程序中通过授权管理器使用基于角色的安全

    基于角色的安全是从 Windows NT 的第一个版本开始在 Windows 平台上发展而来的.使用角色,操作系统可以通过检查称为 BUILTIN\Administrators 的组的安全上下文做出一 ...

  3. (转)淘淘商城系列——使用solrj来测试索引库

    http://blog.csdn.net/yerenyuan_pku/article/details/72892280 我们使用solrj来操作索引库,一般习惯先建一个单元测试类测试下增删改查方法是否 ...

  4. 并发编程学习笔记(4)----jdk5中提供的原子类及Lock使用及原理

    (1)jdk中原子类的使用: jdk5中提供了很多原子类,它会使变量的操作变成原子性的. 原子性:原子性指的是一个操作是不可中断的,即使是在多个线程一起操作的情况下,一个操作一旦开始,就不会被其他线程 ...

  5. N18_二叉树的镜像

    题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...

  6. luogu P3899 [湖南集训]谈笑风生 线段树合并

    Code: #include<bits/stdc++.h> #define maxn 300002 #define ll long long using namespace std; vo ...

  7. apache 添加虚拟机

    <VirtualHost *:80> DocumentRoot "E:/UPUPW_AP7.0/htdocs/xd.local/public" ServerName a ...

  8. 使用scrapy 爬取酷狗音乐歌手及歌曲名并存入mongodb中

    备注还没来得及写,共爬取八千多的歌手,每名歌手平均三十首歌曲算,大概二十多万首歌曲 run.py #!/usr/bin/env python # -*- coding: utf-8 -*- __aut ...

  9. reading/writing files in Python

    file types: plaintext files, such as .txt .py Binary files, such as .docx, .pdf, iamges, spreadsheet ...

  10. [bzoj2431][HAOI2009][逆序对数列] (dp计数)

    Description 对于一个数列{ai},如果有i<j且ai>aj,那么我们称ai与aj为一对逆序对数.若对于任意一个由1~n自然数组成的 数列,可以很容易求出有多少个逆序对数.那么逆 ...