Each line has a line segment tree ....
Fast Matrix Operations
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 81 1 2 4 4 53 2 1 4 41 1 1 3 4 23 1 2 4 43 1 1 3 42 2 1 4 4 23 1 2 4 41 1 1 4 3 3
Output for the sample input
45 0 578 5 769 2 739 2 7
Rujia Liu's present 3: a data structure contest celebrating the 100th Anniversary of Tsinghua University
Special thanks: yeji Shen, dun Liang
Note: Please make sure to test your program with the gift I/O files before submitting!
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>const int maxn=100100;using namespace std;const int INF=0x3f3f3f3f;int setv[30][maxn<<2],addv[30][maxn<<2],sum[30][maxn<<2],maxnum[30][maxn<<2],minnum[30][maxn<<2];void pushup(int num,int rt){ int ls=rt<<1,rs=rt<<1|1; sum[num][rt]=sum[num][ls]+sum[num][rs]; maxnum[num][rt]=max(maxnum[num][ls],maxnum[num][rs]); minnum[num][rt]=min(minnum[num][ls],minnum[num][rs]);}void pushdown(int num,int l,int r,int rt){ int m=(l+r)/2; int ls=rt<<1,rs=rt<<1|1; if(setv[num][rt]>0) { setv[num][ls]=setv[num][rs]=setv[num][rt]; addv[num][ls]=addv[num][rs]=0; sum[num][ls]=(m-l+1)*setv[num][rt]; sum[num][rs]=(r-m)*setv[num][rt]; minnum[num][ls]=minnum[num][rs]=setv[num][rt]; maxnum[num][ls]=maxnum[num][rs]=setv[num][rt]; setv[num][rt]=-1; } if(addv[num][rt]>0) { addv[num][ls]+=addv[num][rt]; addv[num][rs]+=addv[num][rt]; sum[num][ls]+=addv[num][rt]*(m-l+1); sum[num][rs]+=addv[num][rt]*(r-m); minnum[num][ls]+=addv[num][rt]; minnum[num][rs]+=addv[num][rt]; maxnum[num][ls]+=addv[num][rt]; maxnum[num][rs]+=addv[num][rt]; addv[num][rt]=0; }}void build(int num,int l,int r,int rt){ addv[num][rt]=0; setv[num][rt]=-1; if(l==r) { minnum[num][rt]=maxnum[num][rt]=sum[num][rt]=0; return ; } int m=(l+r)/2; build(num,l,m,rt<<1); build(num,m+1,r,rt<<1|1); pushup(num,rt);}void update(int type,int value,int num,int L,int R,int l,int r,int rt){ if(L<=l&&r<=R) { if(type==1)///add { addv[num][rt]+=value; sum[num][rt]+=value*(r-l+1); minnum[num][rt]+=value; maxnum[num][rt]+=value; } else if(type==2)///set { addv[num][rt]=0; setv[num][rt]=value; sum[num][rt]=(r-l+1)*value; minnum[num][rt]=maxnum[num][rt]=value; } return ; } pushdown(num,l,r,rt); int m=(l+r)/2; if(L<=m) update(type,value,num,L,R,l,m,rt<<1); if(R>m) update(type,value,num,L,R,m+1,r,rt<<1|1); pushup(num,rt);}void query(int num,int L,int R,int l,int r,int rt,int& s,int& maxv,int& minv){ // cout<<" .......... "<<l<<" "<<r<<" "<<rt<<endl; if(L<=l&&r<=R) { s=sum[num][rt]; maxv=maxnum[num][rt]; minv=minnum[num][rt]; return ; } pushdown(num,l,r,rt); int m=(l+r)/2; int ts,tmaxv,tminv; s=0;maxv=-1;minv=INF; if(L<=m) { query(num,L,R,l,m,rt<<1,ts,tmaxv,tminv); s+=ts; maxv=max(maxv,tmaxv); minv=min(minv,tminv); } if(m<R) { query(num,L,R,m+1,r,rt<<1|1,ts,tmaxv,tminv); s+=ts; maxv=max(maxv,tmaxv); minv=min(minv,tminv); }}int main(){ int n,m,q; while(scanf("%d%d%d",&m,&n,&q)!=EOF) { for(int i=1;i<=m;i++) build(i,1,n,1); int type,x1,x2,y1,y2,v; while(q--) { scanf("%d",&type); if(type==1||type==2) { scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&v); for(int i=x1;i<=x2;i++) update(type,v,i,y1,y2,1,n,1); } else if(type==3) { scanf("%d%d%d%d",&x1,&y1,&x2,&y2); int s=0,minv=INF,maxv=-1; int ts,tmin,tmax; for(int i=x1;i<=x2;i++) { query(i,y1,y2,1,n,1,ts,tmax,tmin); s+=ts; maxv=max(maxv,tmax); minv=min(minv,tmin); } printf("%d %d %d\n",s,minv,maxv); } } } return 0;}/*Sample Input4 4 81 1 2 4 4 53 2 1 4 41 1 1 3 4 23 1 2 4 43 1 1 3 42 2 1 4 4 23 1 2 4 41 1 1 4 3 3Output for the Sample Input45 0 578 5 769 2 739 2 7*/
Ultraviolet A 11992 Fast Matrix Operations