Segment Tree Interval modification

Source: Internet
Author: User

Setv[] come up and get it all negative. Sum in query don't forget to add one.

1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cmath>6 using namespacestd;7 Const intMAXN =100000+Ten;8 Const intMAXN3 = MAXN *3;9 Const intINF =-1u>>1;Ten intA[MAXN], minv[maxn3], maxv[maxn3], sumv[maxn3], setv[maxn3]; One intV, QL, QR, _min, _max, _sum; A intN, Q; - CharTP; - voidFreshintOintLcintRC) { theMinv[o] =min (MINV[LC], MINV[RC]); -Maxv[o] =Max (MAXV[LC], MAXV[RC]); -Sumv[o] = SUMV[LC] +SUMV[RC]; -     return ; + } - voidBuildintOintLintR) { +     if(L = = R) Minv[o] = Maxv[o] = Sumv[o] =A[l]; A     Else{ at         intM = L + R >>1, LC = O <<1, rc = LC |1; -Build (LC, L, M); Build (RC, M +1, R); Fresh (o, LC, RC); -     } -     return ; - } - voidPushdown (into) { in     if(Setv[o] >=0){ -         intLC = O <<1, rc = LC |1; toSETV[LC] = SETV[RC] =Setv[o]; +Setv[o] =-1; -     } the     return ; * } $ voidMaintain (intOintLintR) {Panax Notoginseng     if(Setv[o] >=0){ -Minv[o] = Maxv[o] =Setv[o]; theSumv[o] = (r-l +1) *Setv[o]; +     } A     Else if(L < R) Fresh (o, O <<1, (o <<1) |1); the     return ; + } - voidUpdateintOintLintR) { $     if(QL <= L && R <= qr) Setv[o] =v; $     Else{ -         intM = L + R >>1, LC = O <<1, rc = LC |1; - pushdown (o); the         if(QL <= m) Update (LC, L, M);ElseMaintain (LC, L, M); -         if(QR > M) update (RC, M +1, R);ElseMaintain (RC, M +1, R);Wuyi     } theMaintain (o, L, R);return ; - } Wu voidQueryintOintLintR) { -     if(Setv[o] >=0){ About_min =min (_min, setv[o]); $_max =Max (_max, Setv[o]); -_sum + = (min (R, QR)-Max (L, QL) +1) *Setv[o]; -     } -     Else if(QL <= L && R <=qr) { A_min =min (_min, minv[o]); +_max =Max (_max, Maxv[o]); the_sum + =Sumv[o]; -     } $     Else{ the         intM = L + R >>1, LC = O <<1, rc = LC |1; the         if(QL <=m) query (LC, L, m); the         if(QR > m) query (RC, M +1, R); the     } -     return ; in } the voidReadint&x) { thex =0;intsig =1;CharCH =GetChar (); About      while(!isdigit (CH)) {if(ch = ='-') sig =-1; CH =GetChar ();} the      while(IsDigit (ch)) x =Ten* x + CH-'0', ch =GetChar (); theX *= sig;return ; the } + voidReadChar&x) { -x =GetChar (); the      while(!isalpha (x)) x =GetChar ();Bayi     return ; the } the voidinit () { -memset (Setv,-1,sizeof(Setv));// ! - read (n); the      for(inti =1; I <= N; i + +) read (A[i]); theBuild1,1, n); the read (Q); the     return ; - } the voidWork () { the      while(q--) the     {94         Chartp=GetChar (); the          while(!isalpha (TP)) tp=GetChar (); the Read (QL), read (QR); the         if(ql>qr) swap (QL,QR);98         if(tp=='Q') About         { -_sum=0; _min=inf; _max=-INF;101Query1,1, n);102printf"Maxnum:%d, Minnum:%d, Sum:%d\n", _max,_min,_sum);103         }104         ElseRead (v), update (1,1, n); the     }106     return ;107 }108 voidprint () {109  the     return ;111 } the intMain () {113 init (); the Work (); the print (); the     return 0;117}

Segment Tree Interval modification

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.