http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1859

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

题意:
给出一个n*n的矩阵,有m次询问,求每次询问子矩阵中的最小值。
分析:
显然二维线段树随便乱搞搞即可了,线段树维护区域内的最小值。注意二维上的"pushup()"的写法,实际上也是要维护一棵线段树。

/*
*
* Author : fcbruce
*
* Date : 2014-08-24 13:03:05
*
*/
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10 #ifdef _WIN32
#define lld "%I64d"
#else
#define lld "%lld"
#endif #define maxm
#define maxn 300 using namespace std; int n;
int minv[maxn<<2][maxn<<2]; inline void pushup(int k_2d,int k)
{
minv[k_2d][k]=min(minv[k_2d][k*2+1],minv[k_2d][k*2+2]);
} void build_1d(int k,int l,int r,int k_2d,int type)
{
if (r-l==1)
{
if (type)
scanf( "%d",&minv[k_2d][k]);
else
minv[k_2d][k]=min(minv[k_2d*2+1][k],minv[k_2d*2+2][k]); return ;
} build_1d(k*2+1,l,l+r>>1,k_2d,type);
build_1d(k*2+2,l+r>>1,r,k_2d,type); pushup(k_2d,k);
} void build_2d(int k,int l,int r)
{
if (r-l==1)
build_1d(0,0,n,k,1);
else
{
build_2d(k*2+1,l,l+r>>1);
build_2d(k*2+2,l+r>>1,r); build_1d(0,0,n,k,0);
}
} int query_1d(int a,int b,int k,int l,int r,int k_2d)
{
if (b<=l || r<=a) return INF;
if (a<=l && r<=b) return minv[k_2d][k]; return min(query_1d(a,b,k*2+1,l,l+r>>1,k_2d),query_1d(a,b,k*2+2,l+r>>1,r,k_2d)); } int query_2d(int a,int b,int ya,int yb,int k,int l,int r)
{
if (b<=l || r<=a) return INF;
if (a<=l && r<=b) return query_1d(ya,yb,0,0,n,k); return min(query_2d(a,b,ya,yb,k*2+1,l,l+r>>1),query_2d(a,b,ya,yb,k*2+2,l+r>>1,r));
} int main()
{
#ifndef ONLINE_JUDGE
freopen("/home/fcbruce/文档/code/t","r",stdin);
#endif // ONLINE_JUDGE int T_T;
scanf( "%d",&T_T); while (T_T--)
{
scanf( "%d",&n);
build_2d(0,0,n); int m;
scanf( "%d",&m);
int x1,x2,y1,y2;
while (m--)
{
scanf( "%d%d%d%d",&x1,&y1,&x2,&y2);
x1--;
y1--;
printf( "%d\n",query_2d(x1,x2,y1,y2,0,0,n));
}
} return 0;
}

ZOJ 1859 Matrix Searching(二维线段树)的更多相关文章

  1. zoj 2589 Matrix Searching 二维线段树

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

  2. poj 2155:Matrix(二维线段树,矩阵取反,好题)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17880   Accepted: 6709 Descripti ...

  3. POJ 2155 Matrix (二维线段树)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17226   Accepted: 6461 Descripti ...

  4. POJ2155 Matrix 【二维线段树】

    题目链接 POJ2155 题解 二维线段树水题,蒟蒻本想拿来养生一下 数据结构真的是有毒啊,, TM这题卡常 动态开点线段树会TLE[也不知道为什么] 直接开个二维数组反倒能过 #include< ...

  5. POJ 2155 Matrix【二维线段树】

    题目大意:给你一个全是0的N*N矩阵,每次有两种操作:1将矩阵中一个子矩阵置反,2.查询某个点是0还是1 思路:裸的二维线段树 #include<iostream>#include< ...

  6. UVA 11992 Fast Matrix Operations (二维线段树)

    解法:因为至多20行,所以至多建20棵线段树,每行建一个.具体实现如下,有些复杂,慢慢看吧. #include <iostream> #include <cstdio> #in ...

  7. POJ2155 Matrix二维线段树经典题

    题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...

  8. POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)

    题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...

  9. poj 2155 matrix 二维线段树 线段树套线段树

    题意 一个$n*n$矩阵,初始全为0,每次翻转一个子矩阵,然后单点查找 题解 任意一种能维护二维平面的数据结构都可以 我这里写的是二维线段树,因为四分树的写法复杂度可能会退化,因此考虑用树套树实现二维 ...

随机推荐

  1. JAVA刚碰见的问题( java.lang.SecurityException: The jurisdiction policy files are not signed by a trusted signer)

    原文:刚碰见的问题 1.  failed to load the jni shared library jre bin server jvm.dll 解决:这个主要是eclipse的版本和安装的jdk ...

  2. BNU Box of Bricks

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=1596 这个题一开始以为要求最少移动次数,把我吓到了,原来只要求最少移动几个方块就行了..这一下就变简 ...

  3. C语言数据结构----栈的定义及实现

    本节主要说的是数据结构中的栈的基本定义和实现的方式,其中实现的方式采用的是复用顺序表和单向链表的方式. 一.栈的基本定义 1.栈是一种特殊的线性表,只能从固定的方向进出,而且栈进出的基本原则是:先进栈 ...

  4. STM32 控制步进电机 28BYJ-48

    STM32 控制步进电机 28BYJ-48  http://blog.chinaunix.net/uid-12664992-id-300272.html 步进电机驱动最简化的逻辑: //四相八拍:A- ...

  5. XSS漏洞解决方案之一:过滤器

    一:web.xml文件 <!-- 解决xss漏洞 --> <filter> <filter-name>xssFilter</filter-name>   ...

  6. 【每日一摩斯】-Troubleshooting: High CPU Utilization (164768.1) - 系列5

    Oracle(用户)进程 以下这些操作都是需要消耗大量CPU资源的:解析大型查询,存储过程编译或执行,空间管理和排序. 下面这几篇文章可以帮助采集关于使用高CPU资源的进程的更多信息: Note:35 ...

  7. SQL窗体函數一例

    需求: MSSQL,列出服務實例中全部數據庫的例如以下信息: 數據庫ID.數據庫名.創建日期.數據文件類型.數據文件大小.數據庫總大小.文件所在路徑. 寫法(後面的百分比為所花時間占比): -- 连接 ...

  8. C#、WinForm、ASP.NET - Md5散列加密

     MD5值概念解释: 转载自:http://free0007.iteye.com/blog/2047163 所 谓MD5,即"Message-Digest Algorithm 5(信息-摘要 ...

  9. 开源数据库连接池之Tomcat内置连接池

    本篇介绍几种开源数据库连接池,同时重点讲述如何使用Tomcat服务器内置的数据库连接池. 之前的博客已经重点讲述了使用数据库连接池的好处,即是将多次创建连接转变为一次创建而使用长连接模式.这样能减少数 ...

  10. QT_opengl_gluPerspective没有定义的处理方法

    原地址:http://blog.sina.com.cn/s/blog_6b11cdda0101fe27.html 例如: gluPerspective( 45.0, (GLfloat)width/(G ...