zoj 2589 Matrix Searching 二维线段树
给一个n*n的矩阵, 给q个查询, 每次给出x1, y1, x2, y2, 求这个矩阵中的最小值。
代码基本上和上一题相同...
#include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = ;
int maxx[maxn<<][maxn<<], minn[maxn<<][maxn<<], max_ans, min_ans, n;
void pushUp(int pos, int rt) {
minn[pos][rt] = min(minn[pos][rt<<], minn[pos][rt<<|]);
}
void sub_build(int sign, int pos, int l, int r, int rt) {
if(l == r) {
if(!sign) {
scanf("%d", &minn[pos][rt]);
} else {
minn[pos][rt] = min(minn[pos<<][rt], minn[pos<<|][rt]);
}
return ;
}
int m = l+r>>;
sub_build(sign, pos, lson);
sub_build(sign, pos, rson);
pushUp(pos, rt);
}
void build(int l, int r, int rt) {
if(l == r) {
sub_build(, rt, , n, );
return ;
}
int m = l+r>>;
build(lson);
build(rson);
sub_build(, rt, , n, );
}
void sub_update(int sign, int pos, int y, int l, int r, int rt, int val) {
if(l == r) {
if(!sign) {
minn[pos][rt] = val;
} else {
minn[pos][rt] = min(minn[pos<<][rt], minn[pos<<|][rt]);
}
return ;
}
int m = l+r>>;
if(y<=m)
sub_update(sign, pos, y, lson, val);
else
sub_update(sign, pos, y, rson, val);
pushUp(pos, rt);
}
void update(int x, int y, int l, int r, int rt, int val) {
if(l == r) {
sub_update(, rt, y, , n, , val);
return ;
}
int m = l+r>>;
if(x<=m)
update(x, y, lson, val);
else
update(x, y, rson, val);
sub_update(, rt, y, , n, , val);
}
void sub_query(int pos, int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
max_ans = max(max_ans, maxx[pos][rt]);
min_ans = min(min_ans, minn[pos][rt]);
return ;
}
int m = l+r>>;
if(L<=m)
sub_query(pos, L, R, lson);
if(R>m)
sub_query(pos, L, R, rson);
}
void query(int LX, int RX, int LY, int RY, int l, int r, int rt) {
if(LX<=l&&RX>=r) {
sub_query(rt, LY, RY, , n, );
return ;
}
int m = l+r>>;
if(LX<=m)
query(LX, RX, LY, RY, lson);
if(RX>m)
query(LX, RX, LY, RY, rson);
}
int main()
{
int t, x, y, l, q, cnt = ;
cin>>t;
while (t--) {
scanf("%d", &n);
build(, n, );
cin>>q;
while(q--) {
int LX, RX, LY, RY;
scanf("%d%d%d%d", &LX, &LY, &RX, &RY);
min_ans = inf, max_ans = ;
query(LX , RX, LY, RY, , n, );
printf("%d\n", min_ans);
}
}
}
zoj 2589 Matrix Searching 二维线段树的更多相关文章
- ZOJ 1859 Matrix Searching(二维线段树)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1859 Matrix Searching Time Limit: 10 Seco ...
- 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,每次翻转一个子矩阵,然后单点查找 题解 任意一种能维护二维平面的数据结构都可以 我这里写的是二维线段树,因为四分树的写法复杂度可能会退化,因此考虑用树套树实现二维 ...
随机推荐
- jQuery源码笔记——回调对象
回调对象是一个多用途的回调列表对象,提供了强大的的方式来管理回调函数列表. 最简单的缓存对象 function Callbacks(){ var list = [], self = { add: fu ...
- 性能监控工具javamelody与spring的集成
详细信息可以访问javamelody的官方网站 我在集成的过程中,一直出现commonHibernateDao的加载问题, 另外,根据官方文档,如果你的应用与monitoring-spring.xml ...
- JAVA G1收集器 第11节
JAVA G1收集器 第11节 上两章我们讲了新生代和年老代的收集器,那么这一章的话我们就要讲一个收集范围涵盖整个堆的收集器——G1收集器. 先讲讲G1收集器的特点,他也是个多线程的收集器,能够充分利 ...
- Python核心编程读笔 1
第一章 欢迎来到Python世界 1 Python特点: 高级的可进行系统调用的解释性语言 面向对象 可升级.扩展.移植 自动内存管理器(内存管理由Python解释器负责) 2 安装 Windows的 ...
- ajax防止重复提交请求1
ajax防止重复提交请求 A. 独占型提交 只允许同时存在一次提交操作,并且直到本次提交完成才能进行下一次提交. module.submit = function() { if (this.pro ...
- 图的邻接矩阵实现(c)
参考:算法:c语言实现 一书 图的邻接矩阵实现 #ifndef GRAPH #define GRAPH /* 图的邻接矩阵实现 */ #include<stdio.h> #include& ...
- 安装solaris_11.2与windows双系统(VM10模拟实现)(一)
感慨:这周刚接触solaris,装solaris很蛋疼,一个字:慢! 在上面安装软件包依然很慢,无线网也很不稳定. 在上面搭建环境更蛋疼,一个字:惨! 什么环境之类的废话就不多说了,一般的电脑都可以. ...
- 分享一个自用的 Inno Setup 软件打包脚本
此脚本支持打包mysql.安装mysql服务.安装windows服务.操作ini文件.操作注册表.高效压缩文件等功能,基本能满足常用的软件打包需求. ;定义各种常量 #define MyAppName ...
- JSP中的Attribute和InitParameter
属性:Attribute类型:应用/上下文,请求,会话(ServletContext,HttpServletRequest/ServletRequest,HttpSession)设置方法:setAtt ...
- Qt信号槽机制的实现(面试的感悟,猜测每一个类保存的一个信号和槽的二维表,实际使用函数指针 元对象 还有类型安全的检查设定等等)
因为面试时问了我这道题,导致我想去了解信号槽到底是如何实现的,于是贴着顺序看了下源码,大致了解了整个框架.网上关于信号槽的文章也很多,但是大部分都是将如何应用的,这里我就写一下我所理解的如何实现吧, ...