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,每次翻转一个子矩阵,然后单点查找 题解 任意一种能维护二维平面的数据结构都可以 我这里写的是二维线段树,因为四分树的写法复杂度可能会退化,因此考虑用树套树实现二维 ...
随机推荐
- Android Activity 常用功能设置(全屏、横竖屏等)
Activity全屏设置 方式1:AndroidManifest.xml <activity android:name="myAcitivty" android:theme ...
- POJ 1287:Networking(最小生成树Kruskal)
id=1287">Networking Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5976 Acce ...
- 深入理解mysql之BDB系列(1)---BDB相关基础知识
深入理解mysql之BDB系列(1) ---BDB相关基础知识 作者:杨万富 一:BDB体系结构 1.1.BDB体系结构 BDB总体的体系结构如图1.1所看到的,包括五个子系统(见图1.1 ...
- VS2010对C++11的支持列表(感觉大部分都不支持)
c++11,就是之前的c++0x,已经成为了最新的c++标准.像咱这样天天用c++的,就赶紧follow一下.学习成果,放在这里,不说分享,至少自己增强下记忆. 首先,给出一些有用的链接. http: ...
- android HorizontalListView
最近搞android 用到一个HorizontalListView 网上搜了一把.有一些国人实现的.但也有一些基本上是cp人家的开源项目. 本人找了两个.记录一下. 其一:https://github ...
- POJ 2250(最长公共子序列 变形)
Description In a few months the European Currency Union will become a reality. However, to join the ...
- HTML&JS笔记(1)
canvas基本绘图 <!DOCTYPE html> <html> <body> <meta charset="utf-8"> &l ...
- axure网格设置
Axure默认的界面是没有吧网格显示出来,没有网格在制作原型的时候,对齐方面不是很好,个人习惯还是把网格显示出来,便于组件对齐和布局. 其实本来这篇文章应该叫做网格与参考线,只是本人对参考线的应用还很 ...
- QT使用scrollarea显示图片,完美解决方案
需求: 在界面上点击“显示图片”按钮,会调用scrollarea窗口显示图片,窗口大小能根据图片大小自动调整,但是最大为1024*768,图片过大就要有滚动条来显示 IDE环境: QT Creator ...
- Spark on Mesos: 搭建Mesos的一些问题
资源管理系统 Spark可以搭建在Mesos上或YARN上,两个都是资源管理系统.了解资源管理系统的话,可以先参看以下几篇文章: 浅谈Borg/YARN/Mesos/Torca/Corona一类系统 ...