Test instructions
All 0 matrices of the R row C column have three operations 1 x1 y1 X2 y2 v sub-matrix (x1,y1,x2,y2) all elements increased by V
2 x1 y1 X2 y2 v sub-matrix (x1,y1,x2,y2) All elements are set to V
3 x1 y1 X2 y2 query sub-matrix element's and, Min, max values
Analysis: The interval of the segment tree is updated and the matrix is transformed into one dimension.
#include <map>#include<Set>#include<list>#include<cmath>#include<queue>#include<stack>#include<cstdio>#include<vector>#include<string>#include<cctype>#include<complex>#include<cassert>#include<utility>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespaceStd;typedef pair<int,int>Pii;typedefLong Longll;#defineLson (x) ((x<<1))#defineRson (x) ((x<<1) | |)#defineINF 0x3f3f3f3fConst intN =1000005;intR, C, M;structNode {intL, R; intsum, Max, Min, SUMV, Setv;} node[4*N];voidPushup (intx) {node[x].sum= Node[lson (x)].sum +Node[rson (x)].sum; NODE[X]. Max=Max (Node[lson (x)]. Max, Node[rson (x)]. MAX); NODE[X]. Min=min (Node[lson (x)]. Min, Node[rson (x)]. Min);}voidPushdown (intx) {if(Node[x].setv) {Node[lson (x)].SUMV= Node[rson (x)].SUMV =0; Node[lson (x)].setv= Node[rson (x)].setv =Node[x].setv; Node[lson (x)].sum= (Node[lson (x)].r-node[lson (x)].L +1) *Node[x].setv; Node[rson (x)].sum= (Node[rson (x)].r-node[rson (x)].L +1) *Node[x].setv; Node[lson (x)]. Max= Node[lson (x)]. Min =Node[x].setv; Node[rson (x)]. Max= Node[rson (x)]. Min =Node[x].setv; Node[x].setv=0; } if(NODE[X].SUMV) {Node[lson (x)].SUMV+=NODE[X].SUMV; Node[rson (x)].SUMV+=NODE[X].SUMV; Node[lson (x)].sum+ = (Node[lson (x)].r-node[lson (x)].L +1) *NODE[X].SUMV; Node[rson (x)].sum+ = (Node[rson (x)].r-node[rson (x)].L +1) *NODE[X].SUMV; Node[lson (x)]. Max+=NODE[X].SUMV; Node[lson (x)]. Min+=NODE[X].SUMV; Node[rson (x)]. Max+=NODE[X].SUMV; Node[rson (x)]. Min+=NODE[X].SUMV; NODE[X].SUMV=0; }}voidBuildintLintRintx) {NODE[X].L= L; NODE[X].R =R; if(L = =r) {node[x].sum= Node[x]. Max = Node[x]. Min = NODE[X].SUMV = Node[x].setv =0; return; } intMid = (L + r)/2; Build (L, Mid, Lson (x)); Build (Mid+1, R, Rson (x)); Pushup (x);}voidUpdate_add (intLintRintVintx) {if(NODE[X].L >= l && NODE[X].R <=r) {NODE[X].SUMV+=v; Node[x].sum+ = (NODE[X].R-NODE[X].L +1) *v; NODE[X]. Max+=v; NODE[X]. Min+=v; return; } pushdown (x); intMid = (NODE[X].L + NODE[X].R)/2; if(L <=mid) Update_add (L, R, V, Lson (x)); if(R >mid) Update_add (L, R, V, Rson (x)); Pushup (x);}voidUpdate_set (intLintRintVintx) {if(NODE[X].L >= l && NODE[X].R <=r) {Node[x].setv=v; Node[x].sum= (NODE[X].R-NODE[X].L +1) *v; NODE[X]. Max= Node[x]. Min =v; NODE[X].SUMV=0; return; } pushdown (x); intMid = (NODE[X].L + NODE[X].R)/2; if(L <=mid) Update_set (L, R, V, Lson (x)); if(R >mid) Update_set (L, R, V, Rson (x)); Pushup (x);} Node Query (intLintRintx) {Node ans; ans.sum=0; Ans. Max =0; Ans. Min =INF; if(NODE[X].L >= l && NODE[X].R <=r) {ans.sum=node[x].sum; Ans. Max=Node[x]. Max; Ans. Min=Node[x]. Min; returnans; } pushdown (x); intMid = (NODE[X].L + NODE[X].R)/2; if(L <=mid) {Node tmp=query (L, R, Lson (x)); Ans.sum+=tmp.sum; Ans. Max=Max (ans. Max, TMP. MAX); Ans. Min=min (ans. Min, TMP. Min); } if(R >mid) {Node tmp=query (L, R, Rson (x)); Ans.sum+=tmp.sum; Ans. Max=Max (ans. Max, TMP. MAX); Ans. Min=min (ans. Min, TMP. Min); } returnans;}intMain () { while(~SCANF ("%d%d%d", &r, &c, &m) {Build (1, R * C,1); intq, x1, y1, x2, y2, V; while(m--) {scanf ("%d", &q); if(q = =3) {scanf ("%d%d%d%d", &x1, &y1, &X2, &y2); intsum =0, Max =0, Min =INF; for(inti = x1; I <= x2; i++) {Node ans= Query ((I-1) * C + y1, (i-1) * C + y2,1); Sum+=ans.sum; Max=Max (max, ans. MAX); Min=min (min, ans. Min); } printf ("%d%d%d\n", Sum, Min, Max); } Else{scanf ("%d%d%d%d%d", &x1, &y1, &x2, &y2, &v); for(inti = x1; I <= x2; i++) { if(q = =1) Update_add ((I-1) * C + y1, (i-1) * C + y2, V,1); ElseUpdate_set ((I-1) * C + y1, (i-1) * C + y2, V,1); } } } } return 0;}
Uva11992-fast Matrix Operations (interval increment, value change)