Segment Tree Point Modification

Source: Internet
Author: User

Note that it is easy to write wrong: the closing interval of update and query and the recursion of query.

1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cmath>6 using namespacestd;7 Const intMAXN =1000000+Ten;8 Const intMAXN3 = MAXN *3;9 Const intINF =-1u>>1;Ten intA[MAXN], minv[maxn3], maxv[maxn3], sumv[maxn3], X, V, QL, QR, _min, _max, _sum; One intN, Q, TT =0; A CharTP; - voidFreshintOintLcintRC) { -Minv[o] =min (MINV[LC], MINV[RC]); theMaxv[o] =Max (MAXV[LC], MAXV[RC]); -Sumv[o] = SUMV[LC] +SUMV[RC]; -     return ; - } + voidBuildintOintLintR) { -     if(L = = R) Sumv[o] = Minv[o] = Maxv[o] =A[l]; +     Else{ A         intM = L + R >>1, LC = O <<1, rc = LC |1; at Build (LC, L, M); -Build (RC, M +1, R); - Fresh (o, LC, RC); -     } -     return ; - } in voidUpdateintOintLintR) { -     if(L = = R) Minv[o] = Maxv[o] = Sumv[o] =v; to     Else{ +         intM = L + R >>1, LC = O <<1, rc = LC |1; -         if(x <=m) Update (LC, L, m); the         ElseUpdate (RC, M +1, R); * Fresh (o, LC, RC); $     }Panax Notoginseng     return ; - } the voidQueryintOintLintR) { +     if(QL <= L && R <=qr) { A_min =min (_min, minv[o]); the_max =Max (_max, Maxv[o]); +_sum = _sum +Sumv[o]; -     } $     Else{ $         intM = L + R >>1, LC = O <<1, rc = LC |1; -         if(QL <=m) query (LC, L, m); -         if(QR > m) query (RC, M +1, R); the     } -     return ;Wuyi } the voidReadint&x) { -x =0;intsig =1;CharCH =GetChar (); Wu      while(!isdigit (CH)) {if(ch = ='-') sig =-1; CH =GetChar ();} -      while(IsDigit (ch)) x =Ten* x + CH-'0', ch =GetChar (); AboutX *= sig;return ; $ } - voidReadChar&x) { -x =GetChar (); -      while(!isalpha (x)) x =GetChar (); A     return ; + } the voidinit () { - read (n); $      for(inti =1; I <= N; i + +) read (A[i]); theBuild1,1, n); the read (Q); the     return ; the } - voidWork () { in      while(q--){ the Read (TP); Read (QL); read (QR) ; the         if(TP = ='Q'){ About_sum =0; _max =-inf; _min =INF; theQuery1,1, n); theprintf"Maxnum:%d, Minnum:%d, Sum:%d\n", _max, _min, _sum); the         } +         Else{ -x = QL; v =qr; theUpdate1,1, n);Bayi         } the     } the     return ; - } - voidprint () { the  the     return ; the } the intMain () { - init (); the Work (); the print (); the     return 0;94}

Segment Tree Point 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.