UVA 11992 二维线段树

There is a matrix containing at most 106 elements divided into r rows and c columns. Each element has a location (x,y) where 1<=x<=r,1<=y<=c. Initially, all the elements are zero. You need to handle four kinds of operations:

1 x1 y1 x2 y2 v

Increment each element (x,y) in submatrix (x1,y1,x2,y2) by v (v>0)

2 x1 y1 x2 y2 v

Set each element (x,y) in submatrix (x1,y1,x2,y2) to v

3 x1 y1 x2 y2

Output the summation, min value and max value of submatrix (x1,y1,x2,y2)

In the above descriptions, submatrix (x1,y1,x2,y2) means all the elements (x,y) satisfying x1<=x<=x2 and y1<=x<=y2. It is guaranteed that 1<=x1<=x2<=r, 1<=y1<=y2<=c. After any operation, the sum of all the elements in the matrix does not exceed 109.

Input

There are several test cases. The first line of each case contains three positive integers r, c, m, where m (1<=m<=20,000) is the number of operations. Each of the next m lines contains a query. There will be at most twenty rows in the matrix. The input is terminated by end-of-file (EOF). The size of input file does not exceed 500KB.

Output

For each type-3 query, print the summation, min and max.

Sample Input

4 4 8
1 1 2 4 4 5
3 2 1 4 4
1 1 1 3 4 2
3 1 2 4 4
3 1 1 3 4
2 2 1 4 4 2
3 1 2 4 4
1 1 1 4 3 3

Output for the Sample Input

45 0 5
78 5 7
69 2 7
39 2 7
题意:有一个r行c列的全0矩阵,支持一下3种操作
1 x1 y1 x2 y2 v 子矩阵所有元素增加v
2 x1 y1 x2 y2 v 子矩阵所有元素置v
3 x1 y1 x2 y2 v 查询子矩阵的元素和,最小值和最大值
思路:线段树懒惰标记的典型应用了,此题主要是练下二维线段树转换为一维的技巧、   
      若c列,则每行需要占用数组4*c。将每行都建一棵线段树即可。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 1000800
#define lson id<<1,l,mid
#define rson id<<1|1,mid+1,r
#define inf 0x3f3f3f3f
int r,c,m;
struct ST
{
	int l,r,add,set,sum,maxkey,minkey;
}st[maxn<<2];

inline int max(int a,int b)
{
	return a>b?a:b;
}

inline int min(int a,int b)
{
	return a>b?b:a;
}

void PushUp(int k,int id)
{
	int t = 4*k*c;
	st[t+id].sum = st[t+(id<<1)].sum + st[t+(id<<1|1)].sum;
	st[t+id].maxkey = max(st[t+(id<<1)].maxkey,st[t+(id<<1|1)].maxkey);
	st[t+id].minkey = min(st[t+(id<<1)].minkey,st[t+(id<<1|1)].minkey);
}

void PushDown(int k,int id)
{
	int t = 4*k*c;
	if(st[t+id].set != -1)
	{
		st[t+(id<<1)].add = st[t+(id<<1|1)].add = 0;
		st[t+(id<<1)].set = st[t+(id<<1|1)].set = st[t+id].set;
		st[t+(id<<1)].maxkey = st[t+(id<<1)].minkey = st[t+id].set;
		st[t+(id<<1|1)].maxkey = st[t+(id<<1|1)].minkey = st[t+id].set;
		st[t+(id<<1)].sum = (st[t+(id<<1)].r - st[t+(id<<1)].l + 1) * st[t+id].set;
		st[t+(id<<1|1)].sum = (st[t+(id<<1|1)].r - st[t+(id<<1|1)].l + 1) * st[t+id].set;
		st[t+id].set = -1;
	}

	if(st[t+id].add)
	{
		st[t+(id<<1)].add += st[t+id].add;
		st[t+(id<<1|1)].add += st[t+id].add;
		st[t+(id<<1)].maxkey += st[t+id].add;
		st[t+(id<<1)].minkey += st[t+id].add;
		st[t+(id<<1)].sum += (st[t+(id<<1)].r - st[t+(id<<1)].l + 1) * st[t+id].add;
		st[t+(id<<1|1)].maxkey += st[t+id].add;
		st[t+(id<<1|1)].minkey += st[t+id].add;
		st[t+(id<<1|1)].sum += (st[t+(id<<1|1)].r - st[t+(id<<1|1)].l + 1) * st[t+id].add;
		st[t+id].add = 0;
	}
}

void buildtree(int k,int id,int l,int r)
{
	int t = 4*k*c;
	st[t+id].l = l,st[t+id].r = r;
	st[t+id].set = -1;st[t+id].add = 0;
	st[t+id].sum = st[t+id].maxkey = st[t+id].minkey = 0;
	if(l == r)	return;
	int mid = (l+r) >> 1;
	buildtree(k,lson);
	buildtree(k,rson);
	PushUp(k,id);
}

void Update(int k,int id,int l,int r,int key,int ope)
{
	int t = 4*k*c;
	if(st[t+id].l == l && st[t+id].r == r)
	{
		if(ope == 1)//ADD
		{
			st[t+id].add += key;
			st[t+id].maxkey += key;
			st[t+id].minkey += key;
			st[t+id].sum += (r - l + 1) * key;
		}
		else//SET
		{
			st[t+id].add = 0;
			st[t+id].set = key;
			st[t+id].maxkey = st[t+id].minkey = key;
			st[t+id].sum = (r - l + 1) * key;
		}
		return;
	}
	PushDown(k,id);
	if(st[t+(id<<1)].r >= r)
	{
		Update(k,id<<1,l,r,key,ope);
	}
	else if(st[t+(id<<1|1)].l <= l)
	{
		Update(k,id<<1|1,l,r,key,ope);
	}
	else 
	{
		Update(k,id<<1,l,st[t+(id<<1)].r,key,ope);
		Update(k,id<<1|1,st[t+(id<<1|1)].l,r,key,ope);
	}
	PushUp(k,id);
}

int Query(int k,int id,int l,int r,int ope)
{
	int t = 4*k*c;
	if(st[t+id].l == l && st[t+id].r == r)
	{
		switch(ope)
		{
		case(1):return st[t+id].sum;
		case(2):return st[t+id].minkey;
		case(3):return st[t+id].maxkey;
		}
	}
	PushDown(k,id);
	if(st[t+(id<<1)].r >= r)
		return Query(k,id<<1,l,r,ope);
	else if(st[t+(id<<1|1)].l <= l)
		return Query(k,id<<1|1,l,r,ope);
	else 
	{
		switch(ope)
		{
		case(1):return Query(k,id<<1,l,st[t+(id<<1)].r,ope) + Query(k,id<<1|1,st[t+(id<<1|1)].l,r,ope);
		case(2):return min(Query(k,id<<1,l,st[t+(id<<1)].r,ope),Query(k,id<<1|1,st[t+(id<<1|1)].l,r,ope));
		case(3):return max(Query(k,id<<1,l,st[t+(id<<1)].r,ope),Query(k,id<<1|1,st[t+(id<<1|1)].l,r,ope));
		}
	}
}

int main()
{
	//freopen("in.txt","r",stdin);
	int x1,x2,y1,y2,v,ope;
	while(scanf("%d%d%d",&r,&c,&m)==3)
	{
		for(int i = 1;i <= r;i++)	buildtree(i-1,1,1,c);
		while(m--)
		{
			scanf("%d%d%d%d%d",&ope,&x1,&y1,&x2,&y2);
			if(ope != 3)
			{
				scanf("%d",&v);
				for(int i = x1;i <= x2;i++)	Update(i-1,1,y1,y2,v,ope);
			}
			else 
			{
				int ans = 0,Min = inf,Max = -inf;
				for(int i = x1;i <= x2;i++)
				{
					ans += Query(i-1,1,y1,y2,1);
					Min = min(Min,Query(i-1,1,y1,y2,2));
					Max = max(Max,Query(i-1,1,y1,y2,3));
				}
				printf("%d %d %d\n",ans,Min,Max);
			}
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值