UVA 11992(Fast Matrix Operations-线段树区间加&改)[Template:SegmentTree]
2015-05-13 22:06
417 查看
Fast Matrix Operations
Thereis 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 vIncrementeach element (x,y) in submatrix (x1,y1,x2,y2) by v (v>0)
2 x1 y1 x2 y2 vSeteach element (x,y) in submatrix (x1,y1,x2,y2) to v
3 x1 y1 x2 y2Outputthe summation, min value and max value of submatrix (x1,y1,x2,y2)Inthe 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
Thereare 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 terminatedby end-of-file (EOF). The size of input file does not exceed 500KB.Output
Foreach 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 3Output for the Sample Input
45 0 578 5 769 2 739 2 7线段树,"区间加"和"区间修改"操作由于矩阵最多有20行,所以可以将每行合到一个序列上#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<functional>#include<iostream>#include<cmath>#include<cctype>#include<ctime>#include<vector>using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=pre[x];p;p=next[p])#define Forpiter(x) for(int &p=iter[x];p;p=next[p])#define Lson (o<<1)#define Rson ((o<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define MEM2(a,i) memset(a,i,sizeof(a));#define INF (2139062143)#define F (100000007)#define MAXR (20+1)#define MAXN (8000000+10)#define MAXQ (20000+10)typedef long long ll;ll mul(ll a,ll b){return (a*b)%F;}ll add(ll a,ll b){return (a+b)%F;}ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}void upd(ll &a,ll b){a=(a%F+b%F)%F;}class SegmentTree{ll a[MAXN],minv[MAXN],sumv[MAXN],maxv[MAXN],addv[MAXN],setv[MAXN];int n;public:SegmentTree(){ }SegmentTree(int _n):n(_n){ }void mem(int _n){n=_n;For(i,4*n+3) a[i]=minv[i]=sumv[i]=maxv[i]=addv[i]=0,setv[i]=-1;}void maintain(int o,int L,int R){sumv[o]=maxv[o]=minv[o]=0;if (L<R) //只考虑左右子树{sumv[o]=sumv[Lson]+sumv[Rson];minv[o]=min(minv[Lson],minv[Rson]);maxv[o]=max(maxv[Lson],maxv[Rson]);} //只考虑add操作if (setv[o]>0) sumv[o]=setv[o]*(R-L+1),minv[o]=maxv[o]=setv[o];minv[o]+=addv[o];maxv[o]+=addv[o];sumv[o]+=addv[o]*(R-L+1);}int y1,y2,v;void update(int o,int L,int R) //y1,y2,v{if (y1<=L&&R<=y2) {addv[o]+=v;}else{pushdown(o);int M=(R+L)>>1;if (y1<=M) update(Lson,L,M); else maintain(Lson,L,M);if (M< y2) update(Rson,M+1,R); else maintain(Rson,M+1,R);}maintain(o,L,R);}void update2(int o,int L,int R){if (y1<=L&&R<=y2) {setv[o]=v;addv[o]=0;}else{pushdown(o);int M=(R+L)>>1;if (y1<=M) update2(Lson,L,M); else maintain(Lson,L,M); //维护pushodown,再次maintainif (M< y2) update2(Rson,M+1,R); else maintain(Rson,M+1,R);}maintain(o,L,R);}void pushdown(int o){if (setv[o]>=0){setv[Lson]=setv[Rson]=setv[o];addv[Lson]=addv[Rson]=0;setv[o]=-1;}if (addv[o]){addv[Lson]+=addv[o];addv[Rson]+=addv[o];addv[o]=0;}}void query2(int o,int L,int R,ll add){if (setv[o]>=0){_sum+=(setv[o]+addv[o]+add)*(min(R,y2)-max(L,y1)+1);_min=min(_min,setv[o]+addv[o]+add);_max=max(_max,setv[o]+addv[o]+add);} else if (y1<=L&&R<=y2){_sum+=sumv[o]+add*(R-L+1);_min=min(_min,minv[o]+add);_max=max(_max,maxv[o]+add);} else {int M=(L+R)>>1;if (y1<=M) query2(Lson,L,M,add+addv[o]);if (M< y2) query2(Rson,M+1,R,add+addv[o]);}}ll _min,_max,_sum;void add(ll v,int l,int r){y1=l,y2=r;this->v=v;update(1,1,n);}void set(ll v,int l,int r){y1=l,y2=r;this->v=v;update2(1,1,n);}ll ask(int l,int r,int b=0){_sum=0,_min=INF,_max=-1;y1=l,y2=r;query2(1,1,n,0);switch(b){case 1:return _sum;case 2:return _min;case 3:return _max;default:break;}}//先set后add}S;int main(){// freopen("uva11992.in","r",stdin);// freopen(".out","w",stdout);int R,n,Q;while (~scanf("%d%d%d",&R,&n,&Q)){S.mem(R*n);int p,x1,y1,x2,y2;while(Q--){scanf("%d%d%d%d%d",&p,&x1,&y1,&x2,&y2);ll v;if (p<=2) scanf("%lld",&v);if (p==1) {Fork(i,x1,x2) S.add(v,(i-1)*n+y1,(i-1)*n+y2);}else if (p==2) {Fork(i,x1,x2) S.set(v,(i-1)*n+y1,(i-1)*n+y2);} else {ll s=0,mi=INF,ma=-1;Fork(i,x1,x2){S.ask((i-1)*n+y1,(i-1)*n+y2);s+=S._sum;mi=min(mi,S._min);ma=max(ma,S._max);}printf("%lld %lld %lld\n",s,mi,ma);}}}return 0;}
相关文章推荐
- Uva11992 Fast Matrix Operations(线段树区间修改+更新)
- 线段树(2):区间修改 (uva 11992 FastMatrixOperations)
- uva 11992 Fast Matrix Operations(线段树,区间修改)
- uva 11992 Fast Matrix Operations (线段树区间更新)
- [uva11992]Fast Matrix Operations(多延迟标记,线段树,区间更新)
- UVa 11992 Fast Matrix Operations (线段树,区间修改)
- Uva 11992 - Fast Matrix Operations题解(线段树区间更新+区间Set+Add,查询最大值,最小值,总和)
- UVa 11992 Fast Matrix Operations (线段树区间修改大杂烩)
- UVA 11992 Fast Matrix Operations (线段树区间更新)
- UVa 11992 (线段树 区间修改) Fast Matrix Operations
- UVA 11992- Fast Matrix Operations(线段树区间各种操作)
- XTU1238:Segment Tree(线段树区间更新)
- UVA 11992 Fast Matrix Operations (二维线段树)
- UVa 11992 Fast Matrix Operations (线段树)
- Uva 11992 Fast(线段树)
- 线段树(Fast Matrix Operations,UVA 11992)
- CF242:XOR on Segment(线段树区间更新 & 二进制)
- [线段树双lazy]UVa11992 - Fast Matrix Operations
- UVA 11992 Fast MatrixOperations(线段树)
- AOJ 2450 Do use segment tree (树链剖分 + 线段树区间合并)