二维线段树模版

HDU 4819 二维线段树模版题

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF = 999999999;
const int maxn = 810;
int a[maxn][maxn];
int st_min[maxn<<2][maxn<<2];
int st_max[maxn<<2][maxn<<2];
int n;
int mi, ma;
void pushup(int rx, int rt)
{
	st_min[rx][rt] = min(st_min[rx][rt<<1], st_min[rx][rt<<1|1]);
	st_max[rx][rt] = max(st_max[rx][rt<<1], st_max[rx][rt<<1|1]);
}
void build_y(int l, int r, int rt, int flag, int rx)
{
	if(l == r)
	{
		if(flag)
		{
			int x;
			scanf("%d", &x);
			st_max[rx][rt] = st_min[rx][rt] = x;
		}
		else
		{
			st_max[rx][rt] = max(st_max[rx<<1][rt], st_max[rx<<1|1][rt]);
		 	st_min[rx][rt] = min(st_min[rx<<1][rt], st_min[rx<<1|1][rt]);
		}

		return;
	}
	int m = (l + r) >> 1;
	build_y(l, m, rt<<1, flag, rx);
	build_y(m+1, r, rt<<1|1, flag, rx);

	pushup(rx, rt);

}

void build_x(int l, int r, int rt)
{
	if(l == r)
	{
		build_y(1, n, 1, 1, rt);
		return;
	}
	int m = (l + r) >> 1;
	build_x(l, m, rt<<1);
	build_x(m+1, r, rt<<1|1);

	build_y(1, n, 1, 0, rt);
}

void update_y(int x, int l, int r, int rt, int c, int flag, int rx)
{
	if(l == r)
	{
		if(flag)
		{
			st_max[rx][rt] = st_min[rx][rt] = c;
		}
		else
		{
			st_max[rx][rt] = max(st_max[rx<<1][rt], st_max[rx<<1|1][rt]);
		 	st_min[rx][rt] = min(st_min[rx<<1][rt], st_min[rx<<1|1][rt]);
		}
		return;
	}
	int m = (l + r) >> 1;
	if(x <= m)
		update_y(x, l, m, rt<<1, c, flag, rx);
	else
		update_y(x, m+1, r, rt<<1|1, c, flag, rx);

	pushup(rx, rt);
}
void update_x(int x, int y, int l, int r, int rt, int c)
{
	if(l == r)
	{
		update_y(y, 1, n, 1, c, 1, rt);
		return;
	}
	int m = (l + r) >> 1;
	if(x <= m)
		update_x(x, y, l, m, rt<<1, c);
	else
		update_x(x, y, m+1, r, rt<<1|1, c);
	update_y(y, 1, n, 1, c, 0, rt);
}

void query_y(int x, int y, int l, int r, int rt, int rx)
{

	if(x == l && y == r)
	{
		mi = min(st_min[rx][rt], mi);
		ma = max(st_max[rx][rt], ma);
		return;
	}

	int m = (l + r) >> 1;
	if(y <= m)
		query_y(x, y, l, m, rt<<1, rx);
	else if(x > m)
		query_y(x, y, m+1, r, rt<<1|1, rx);
	else
	{
		query_y(x, m, l, m, rt<<1, rx);
		query_y(m+1, y, m+1, r, rt<<1|1, rx);
	}
}

void query_x(int x1, int y1, int x2, int y2, int l, int r ,int rt)
{
	//printf("%d %d\n", x1, y1);
	if(x1 == l && y1 == r)
	{
		query_y(x2, y2, 1, n, 1, rt);
		return;
	}
	int m = (l + r) >> 1;
	if(y1 <= m)
		query_x(x1, y1, x2, y2, l, m, rt<<1);
	else if(x1 > m)
		query_x(x1, y1, x2, y2, m+1, r, rt<<1|1);
	else
	{
		query_x(x1, m, x2, y2, l, m, rt<<1);
		query_x(m+1, y1, x2, y2, m+1, r, rt<<1|1);
	}
}
int main()
{
	int cas = 1;
	int T;
	scanf("%d", &T);
	while(T--)
	{

		scanf("%d", &n);
		/*for(int i = 1; i <= n; i++)
			for(int j = 1; j <= n; j++)
				scanf("%d", &a[i][j]);
		*/
		build_x(1, n, 1);

		int q;
		scanf("%d", &q);
		printf("Case #%d:\n", cas++);
		while(q--)
		{
			int x, y, z;
			scanf("%d %d %d", &x, &y, &z);
			mi = INF;
			ma = -INF;
			int x1 = max(x-z/2, 1);
			int y1 = max(y-z/2, 1);
			int x2 = min(x+z/2, n);
			int y2 = min(y+z/2, n);
			query_x(x1, x2, y1, y2, 1, n, 1);
			printf("%d\n", (mi+ma)/2);
			update_x(x, y, 1, n, 1, (mi+ma)/2);
			//puts("dsf");
		}
	}
	return 0;
}
时间: 2024-08-27 05:02:11

二维线段树模版的相关文章

HDU1832 二维线段树求最值(模板)

Luck and Love Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 50 Accepted Submission(s): 20   Problem Description 世界上上最远的距离不是相隔天涯海角而是我在你面前可你却不知道我爱你                ―― 张小娴 前段日子,枫冰叶子给Wiskey做了个征婚启事,聘

ZOJ 2859 二维线段树

思路:自己写的第二发二维线段树1A,哈哈,看来对二维的push操作比较了解了:但是还没遇到在两个线段树中同时进行push操作的,其实这题我是想在x维和y维同时进行push操作的,但是想了好久不会,然后看到这题又给出10秒,然后想想在x维线段直接单点查询肯定也过了,然后在第二维就只有pushup操作,在第一维线段树没有pushup操作.要是在第一维也有pushup操作的话,那就不用单点查询那么慢了.不过也A了,想找题即在二维同时进行pushup和pushdown操作的. #include<iost

[POJ2155] Matrix(二维线段树,树套树)

题目链接:http://poj.org/problem?id=2155 题意:给一个01矩阵,两个操作,翻转:子矩阵里每一个数都由0变1,1变0. 查询:查询某一点是0还是1. 一直以为二维线段树就是开一个线段树数组的我- 这题暴力更新每一个小矩形,翻转就+1,最后看看某点的奇偶. 写屎了,特别注意的是在外层查询的时候要先把当前层的内层query掉,接着再向下扩展.这样外层就不用lazy啦 1 #include <algorithm> 2 #include <iostream> 3

poj1195 Mobile phones 二维线段树入门

二维线段树就是树套树,线段树套线段树... #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) #define lson l,m,rt<<1 #

UVA 11297 Census ——二维线段树

[题目分析] 二维线段树模板题目. 简直就是无比的暴力.时间复杂度为两个log. 标记的更新方式比较奇特,空间复杂度为N^2. 模板题目. [代码] #include <cstdio> #include <cstring> //#include <cmath> #include <cstdlib> #include <map> #include <set> #include <queue> #include <str

tyvj P1716 - 上帝造题的七分钟 二维树状数组区间查询及修改 二维线段树

P1716 - 上帝造题的七分钟 From Riatre    Normal (OI)总时限:50s    内存限制:128MB    代码长度限制:64KB 背景 Background 裸体就意味着身体. 描述 Description “第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵.第二分钟,L说,要能修改,于是便有了将左上角为(a,b),右下角为(c,d)的一个矩形区域内的全部数字加上一个值的操作.第三分钟,k说,要能查询,于是便有了求给定矩形区域内的全部数字和的操作.第

POJ 2155 二维线段树

POJ 2155  二维线段树 思路:二维线段树就是每个节点套一棵线段树的树. 刚开始因为题目是求A[I,J],然后在y查询那直接ans^=Map[i][j]的时候没看懂,后面自己把图画出来了才理解. 因为只有0和1,所以可以用异或来搞,而不需要每次都需要修改. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map> #incl

POJ 2155 二维线段树 经典的记录所有修改再统一遍历 单点查询

本来是想找一个二维线段树涉及懒惰标记的,一看这个题,区间修改,单点查询,以为是懒惰标记,敲到一半发现这二维线段树就不适合懒惰标记,你更新了某段的某列,但其实其他段的相应列也要打标记,但因为区间不一样,又不好打...也可能是我这是在套用一维线段树的思想,还有更好的二维线段树懒惰标记方法 反正到现在我还没搞定二维线段树的懒惰标记,因为这道题不用懒惰标记,因为是二进制序列,区间修改仅限于翻转操作,那就只要记录每次操作,最后查询的时候从上往下把所有修改都来上一遍,就可以了.就类似于树状数组的第二种用法,

【BZOJ4785】[Zjoi2017]树状数组 树套树(二维线段树)

[BZOJ4785][Zjoi2017]树状数组 Description 漆黑的晚上,九条可怜躺在床上辗转反侧.难以入眠的她想起了若干年前她的一次悲惨的OI 比赛经历.那是一道基础的树状数组题.给出一个长度为 n 的数组 A,初始值都为 0,接下来进行 m 次操作,操作有两种: 1 x,表示将 Ax 变成 (Ax + 1) mod 2. 2 l r,表示询问 sigma(Ai) mod 2,L<=i<=r 尽管那个时候的可怜非常的 simple,但是她还是发现这题可以用树状数组做.当时非常yo