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