UVA 11992 (Fast Matrix Operations-line segment tree interval plus & amp; change) [Template: SegmentTree]

Source: Internet
Author: User

UVA 11992 (Fast Matrix Operations-line segment tree interval addition & modification) [Template: SegmentTree]

 

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 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 Input45 0 5 

78 5 7
69 2 7
39 2 7
 

 

 

Line Segment tree, "interval addition" and "interval modification" operations

Since a matrix can contain up to 20 rows, each row can be merged into a sequence.

 

 

 

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         
           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
          
            = 0; I --) # define Forp (x) for (int p = pre [x]; p = next [p]) # define Forpiter (x) for (int & p = iter [x]; 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 ()); # define MEMi (a) memset (a, 128, sizeof (a); # define MEM2 (a, I) memset (a, I, sizeof ()); # define INF (2139062143) # define F (100000007) # define MAXR (20 + 1) # define MAXN (8000000 + 10) # define MAXQ (20000 + 10) typedef 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
           
             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); // maintain pushodown, again maintain if (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 query 2 (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; v Oid 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 first and then add} S; int main () {// freopen ("uva11992.in", "r", stdin); // freopen (". out "," w ", stdout); int R, n, Q; while (~ Scanf ("% d", & R, & n, & Q) {S. mem (R * n); int p, x1, y1, x2, y2; while (Q --) {scanf ("% 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 \ n", s, mi, ma) ;}} return 0 ;}
           
          
         
        
       
      
     
    
   
  
 


 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.