ZOJ 1859 Matrix Searching(二维线段树)
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
/*
*
* 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(二维线段树)的更多相关文章
- zoj 2589 Matrix Searching 二维线段树
题目链接 给一个n*n的矩阵, 给q个查询, 每次给出x1, y1, x2, y2, 求这个矩阵中的最小值. 代码基本上和上一题相同... #include<bits/stdc++.h> ...
- poj 2155:Matrix(二维线段树,矩阵取反,好题)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17880 Accepted: 6709 Descripti ...
- POJ 2155 Matrix (二维线段树)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17226 Accepted: 6461 Descripti ...
- POJ2155 Matrix 【二维线段树】
题目链接 POJ2155 题解 二维线段树水题,蒟蒻本想拿来养生一下 数据结构真的是有毒啊,, TM这题卡常 动态开点线段树会TLE[也不知道为什么] 直接开个二维数组反倒能过 #include< ...
- POJ 2155 Matrix【二维线段树】
题目大意:给你一个全是0的N*N矩阵,每次有两种操作:1将矩阵中一个子矩阵置反,2.查询某个点是0还是1 思路:裸的二维线段树 #include<iostream>#include< ...
- UVA 11992 Fast Matrix Operations (二维线段树)
解法:因为至多20行,所以至多建20棵线段树,每行建一个.具体实现如下,有些复杂,慢慢看吧. #include <iostream> #include <cstdio> #in ...
- POJ2155 Matrix二维线段树经典题
题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...
- POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)
题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...
- poj 2155 matrix 二维线段树 线段树套线段树
题意 一个$n*n$矩阵,初始全为0,每次翻转一个子矩阵,然后单点查找 题解 任意一种能维护二维平面的数据结构都可以 我这里写的是二维线段树,因为四分树的写法复杂度可能会退化,因此考虑用树套树实现二维 ...
随机推荐
- GoldenGate配置(二)之双向复制配置
GoldenGate配置(二)之双向复制配置 环境: Item Source System Target System Platform Red Hat Enterprise Linux Serve ...
- android事件传递机制以及onInterceptTouchEvent()和onTouchEvent()详解二之小秘与领导的故事
总结的不是很好,自己也有点看不懂,正好现在用到了,研究了一个,再次总结,方便大家查看 总则: 1.onInterceptTouchEvent中有个Intercept,这是什么意思呢?她叫拦截,你大概知 ...
- linux下编译.so 和.a 可能出现的问题 ?
1. 静态函数库 这类库的名字一般是libxxx.a:利用静态函数库编译成的文件比较大,因为整个 函数库的所有数据都会被整合进目标代码中,他的优点就显而易见了,即编译后的执行程序不需要外部的函数库支持 ...
- Eclipse用法和技巧二十二:快速调整字体大小
团队代码review的时候,一般都会一堆人围着显示器,或者投影仪.这个时候调整代码字体大小就显得很重要.下面直接说操作方式. 步骤一:Windows -> Preference 步 ...
- Demo XML 、 JSON 解析 AND 网络HTTP请求
有道云笔记分享:http://note.youdao.com/share/?id=7950b949a5017a698a9ecc95bc250ec5&type=note 后台服务端:C#.服务器 ...
- ASP.NET利用byte检测上传图片安全
) { //这里只测试上传第一张图片file[0] HttpPostedFile file0 = Request.Files[]; //转换成byte,读取图片MIME类型 Stream stream ...
- perl学习(8) 控制:unless,until,next,redo,last
Perl中实现了所有C 的操作符! Perl力求代码最少! 1.1.unless unless的含义是:除非条件为真,否则执行块中的代码,和if正好相反 unless($fred=~ /^[A-Z_] ...
- 通过 Spring RestTemplate 调用带请求体的 Delete 方法(Delete With Request Body)
Spring 框架的RestTemplate 类定义了一些我们在通过 java 代码调用 Rest 服务时经常需要用到的方法,使得我们通过 java 调用 rest 服务时更加方便.简单.但是 Res ...
- 用WebCollector制作一个爬取《知乎》并进行问题精准抽取的爬虫(JAVA)
简单介绍: WebCollector是一个无须配置.便于二次开发的JAVA爬虫框架(内核),它提供精简的的API.仅仅需少量代码就可以实现一个功能强大的爬虫. 怎样将WebCollector导入项目请 ...
- Appium 命令行启动配置
Appium 安装过后,会有图形界面,同样也支持命令行参数的启动和配置 Windws: 在windows 安装配置Appium有三种方式: Node install -g appium .exe文件安 ...