UVa 11992 (segment tree interval modified) Fast Matrix Operations

Source: Internet
Author: User

A more comprehensive topic.

A two-dimensional segment tree that supports the add and set operations of the interval and then queries the Sum,min,max of the sub-matrices

After writing this problem is also drunk, code warehouse also has a code is in the process of query also pushdown down pass the mark.

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 6 Const intMaxnode =1<< -;7 int_sum, _min, _max, op, x1, x2, y1, y2, X, V;8 9 structIntervaltreeTen { One     intMaxv[maxnode], Minv[maxnode], Sumv[maxnode], Setv[maxnode], Addv[maxnode]; A  -     voidMaintain (intOintLintR) -     { the         intLC = o*2, rc = o*2+1; -         if(R >L) -         { -Sumv[o] = SUMV[LC] +SUMV[RC]; +Minv[o] =min (MINV[LC], MINV[RC]); -Maxv[o] =Max (MAXV[LC], MAXV[RC]); +         } A         if(Setv[o] >=0{Maxv[o] = Minv[o] = Setv[o]; Sumv[o] = (r-l+1)*setv[o];} at         if(Addv[o]) {Minv[o] + = Addv[o]; Maxv[o] + addv[o]; Sumv[o] + = (r-l+1)*addv[o];} -     } -  -     voidPushdown (into) -     { -         intLC = o*2, rc = o*2+1; in         if(Setv[o] >=0) -         { toSETV[LC] = SETV[RC] =Setv[o]; +ADDV[LC] = ADDV[RC] =0; -Setv[o] =-1; the         } *         if(Addv[o]) $         {Panax NotoginsengADDV[LC] + =Addv[o]; -ADDV[RC] + =Addv[o]; theAddv[o] =0; +         } A     } the  +     voidUpdateintOintLintR) -     { $         intLC = o*2, rc = o*2+1; $         if(Y1 <= L && R <=y2) -         { -             if(OP = =1) Addv[o] + =v; the             Else{Setv[o] = v; addv[o] =0; } -         }Wuyi         Else the         { -Pushdown (o);//Passing marks down Wu             intM = (L + R)/2; -             if(y1 <= m) Update (LC, L, M);ElseMaintain (LC, L, M); About             if(Y2 > M) update (RC, m+1, R);ElseMaintain (RC, m+1, R); $         } - maintain (o, L, R); -     } -  A     voidQueryintOintLintRintadd) +     { the         if(Setv[o] >=0) -         { $             intv = setv[o] + addv[o] +add; the_sum + = v * (min (y2, R)-Max (Y1, L) +1); the_min =min (_min, v); the_max =Max (_max, v); the         } -         Else if(Y1 <= L && R <=y2) in         { the_sum + = Sumv[o] + add* (r-l+1); the_min = min (_min, Minv[o] +add); About_max = Max (_max, Maxv[o] +add); the         } the         Else the         { +             intM = (L + R)/2; -             if(y1 <= M) query (o*2, L, M, add +Addv[o]); the             if(y2 > M) query (o*2+1, m+1, R, add +Addv[o]);Bayi         } the     } the }; -  - Const intMAXR = -+5; the Const intINF =1000000000; the  the Intervaltree TREE[MAXR]; the  - intMain () the { the     //freopen ("In.txt", "R", stdin); the 94     intR, C, M; the      while(SCANF ("%d%d%d", &r, &c, &m) = =3) the     { thememset (Tree,0,sizeof(tree));98          for(intx =1; x <= R; X + +) About         { -memset (Tree[x].setv,-1,sizeof(Tree[x].setv));101tree[x].setv[1] =0;102         }103 104          while(m--) the         {106scanf"%d%d%d%d%d", &op, &x1, &y1, &AMP;X2, &y2);107             if(Op <3)108             {109scanf"%d", &v); the                  for(x = x1; <= x2; x + +) Tree[x].update (1,1, c);111             } the             Else113             { the_sum =0; _max =-inf; _min =INF; the                  for(x = x1; <= x2; x + +) Tree[x].query (1,1C0); theprintf"%d%d%d\n", _sum, _min, _max);117             }118         }119     } - 121     return 0;122}
code June

UVa 11992 (segment tree interval modified) Fast Matrix Operations

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.