Single-point update is divided into two types: ① changing a value to another; ② adding a value to a value depends on the actual situation. The Code provides instructions.
# Include <cstdio> # include <cstdlib> # include <algorithm> using namespace STD; const int maxn = 2e5; // number of points struct point_segtree // Single Point update, interval summation, returns the maximum value of {int seg [maxn <2], sum [maxn <2], maxv [maxn <2], and MINV [maxn <2]. void push_down (int pos) {sum [POS] = sum [POS <1] + sum [POS <1 | 1]; MINV [POS] = min (MINV [POS <1], MINV [POS <1 | 1]); maxv [POS] = max (maxv [POS <1], maxv [POS <1 | 1]);} void build (int l, int R, int POS) {If (L = = R) {scanf ("% d", SEG + POS); maxv [POS] = MINV [POS] = sum [POS] = seg [POS]; return ;} int mid = (L + r)> 1; build (L, mid, POS <1); Build (Mid + 1, R, POS <1 | 1 ); push_down (POS);} void Update (int l, int R, int POs, int X, int v) // modify the position to the value of X, {If (L = r) {seg [POS] = V; // when you modify a value to V seg [POS] + = V; // when you modify the value, add V to a value and select sum [POS] = seg [POS]; MINV [POS] = seg [POS]; maxv [POS] = seg [POS];} else {int mid = (L + r)> 1; if (x <= mid) Update (L, mid, POS <1, x, V); else Update (Mid + 1, R, pos <1 | 1, x, V); push_down (POS) ;}} int _ sum, _ min, _ max; void Init () // initialize each query. {_ Sum = 0; _ max =-100000000; _ min = 100000000;} void query (int l, int R, int POs, int UA, int UB) // [UA, UB] is the interval to be queried {If (UA <= L & UB> = r) {_ sum + = sum [POS]; _ min = min (_ min, MINV [POS]); _ max = max (_ max, maxv [POS]); return ;} int mid = (L + r)> 1; if (UA <= mid) query (L, mid, POS <1, UA, UB ); if (UB> mid) query (Mid + 1, R, POS <1 | 1, UA, UB) ;}; int main (void) {return 0 ;}
Line Segment Tree single-point update, interval summation, and maximization templates (Interval Update templates to be continued)