1. 程式人生 > >UVA 11992 Fast Matrix Operation(線段樹,多個線段樹)

UVA 11992 Fast Matrix Operation(線段樹,多個線段樹)

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).
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
Sample Output
45 0 5 78 5 7 69 2 7 39 2 7

題意:給你一個矩陣,給出以下操作

1.將子矩陣x1,y1,x2,y2所有元素加上V

2.將子矩陣x1,y1,x2,y2所有元素設為V

3查詢將子矩陣x1,y1,x2,y2的和,最大,小值


最簡單的就是每行開一個線段樹了,我就是這樣做的,因為題目有個條件,行數不會超過20,但是如果行數太大,感覺就要用二維的線段樹了,這個題吧,最主要的就是標記了,也是最容易錯的,因為有set 和add 這兩個功能,所以標記陣列要開兩個,大家仔細想想,它們的關係要搞懂,不然就一直WA,總的來說set的標記優先順序要高於add,大家最好再仔細想想它們的關係。搞清它們的關係之後,要注意一個問題,就是當一個節點存在兩個標記的時候怎麼辦,應該先處理哪一個?

我一開始想的是用set的標記把add的標記直接覆蓋掉,但是就是因為這樣一直錯,中間的情況還是比較複雜的,比如add函式在更新的過程中發現一個節點存在兩個標記,這時候不能用set標記直接覆蓋。具體應該是先處理set產生的標記,再處理add的標記,其實如果兩個標記同時存在,那一定是先set 再 add的情況,也只有這樣,才可進行處理。

還有,我之前一直錯add的疊加,即同種標記可以疊加。 類似於 add[rt<<1] += add[rt],自己一直容易寫錯,注意下。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int row,col,r1,r2,c1,c2,m;
const int maxn = 1e6+3;
#define inf 0x3f3f3f3f
struct spe
{
	int sum,maximum,lowest;
};
spe p[22][maxn<<2];
int add[22][maxn<<2];
int bigadd[22][maxn<<2];
vector<int> v;
void pushdown(int rr,int rt,int ln, int rn)
{
	if( bigadd[rr][rt] )
	{
		add[rr][rt<<1] = add[rr][rt<<1|1] = 0;
		p[rr][rt<<1].lowest = bigadd[rr][rt];
		p[rr][rt<<1|1].lowest = bigadd[rr][rt];
		p[rr][rt<<1].sum = bigadd[rr][rt]*ln;
		p[rr][rt<<1|1].sum = bigadd[rr][rt]*rn;
		p[rr][rt<<1].maximum = p[rr][rt<<1|1].maximum = bigadd[rr][rt];
		bigadd[rr][rt<<1] = bigadd[rr][rt<<1|1] = bigadd[rr][rt];
		bigadd[rr][rt] = 0;
		//add[rr][rt] = 0;
	}
	  if( add[rr][rt] )
	{
		add[rr][rt<<1] += add[rr][rt];
		add[rr][rt<<1|1] += add[rr][rt];
		p[rr][rt<<1].sum += add[rr][rt]*ln;
		p[rr][rt<<1|1].sum += add[rr][rt]*rn;
		p[rr][rt<<1].maximum += add[rr][rt];
		p[rr][rt<<1|1].maximum += add[rr][rt];
		p[rr][rt<<1].lowest += add[rr][rt];
		p[rr][rt<<1|1].lowest += add[rr][rt];
		add[rr][rt] = 0;
	}
}
void change(int rr, int rt)
{
	p[rr][rt].sum = p[rr][rt<<1].sum + p[rr][rt<<1|1].sum;
	p[rr][rt].maximum = max(p[rr][rt<<1].maximum , p[rr][rt<<1|1].maximum);
	p[rr][rt].lowest = min(p[rr][rt<<1].lowest,p[rr][rt<<1|1].lowest );
}
void increase( int rr, int l,int r,int rt, int val )
{
	if( l >= c1 && r <= c2 )
	{
		add[rr][rt] += val;
		p[rr][rt].sum += val*(r-l+1);
		p[rr][rt].lowest += val;
		p[rr][rt].maximum += val;
		return;
	}
	int m =( l+r )>>1;
	pushdown(rr,rt,m-l+1,r-m);
	if( m >= c1 )		increase(rr,l,m,rt<<1,val);
	if( m < c2 )			increase(rr,m+1,r,rt<<1|1,val);
	change(rr,rt);
}
void setval( int rr,int l, int r,int rt,int val)
{
	if( l >= c1 && r <= c2 )
	{
		p[rr][rt].sum =( r-l+1 )*val;
		p[rr][rt].maximum = p[rr][rt].lowest = val;
		bigadd[rr][rt] = val;
		add[rr][rt] = 0;
		return;
	}
	int m = ( l+r )>>1;
	pushdown(rr,rt,m-l+1,r-m);
	if( m >= c1 )	setval(rr,l,m,rt<<1,val);
	if( m < c2 )		setval(rr,m+1,r,rt<<1|1,val);
	change(rr,rt);
}
void query( int rr,int l, int r, int rt)
{
	if( l >= c1 && r <= c2 )
	{
		v.push_back(rt);
		return;
	}
	int m = (l+r)>>1;
	pushdown(rr,rt,m-l+1,r-m);
	if( m >= c1 )	query(rr,l,m,rt<<1);
	if( m < c2 )		query(rr,m+1,r,rt<<1|1);
}
int main()
{
    while( scanf("%d %d %d",&row,&col,&m) == 3 && row )
	{
		memset(bigadd,0,sizeof(bigadd));
		memset(add,0, sizeof add);
		memset(p,0,sizeof(p));
		int type,val;
		while( m-- )
		{
			scanf("%d %d %d %d %d",&type,&r1,&c1,&r2,&c2);
			if( type == 1 )
			{
				scanf("%d",&val);
				for( int i = r1 ; i <= r2 ; i++ )
					increase(i,1,col,1,val);
			}
			else if( type == 2 )
			{
				scanf("%d",&val);
				for( int i = r1 ; i <= r2 ; i++  )
					setval(i,1,col,1,val);
			}
			else if( type == 3 )
			{
				int big = -inf,small = inf,total = 0;
				for( int i = r1 ; i <= r2 ; i++ )
				{
					query(i,1,col,1);
					for( int j = 0 ; j < v.size() ; j++ )
					{
						if( p[i][ v[j] ].lowest < small )
							small = p[i][ v[j] ].lowest;
						if( p[i][ v[j] ].maximum > big )
							big = p[i][ v[j] ].maximum;
						total += p[i][ v[j] ].sum;
					}
					v.clear();
				}
				printf("%d %d %d\n",total,small,big);
			}
		}
	}
    return 0;
}