Line Segment Tree single-point update, interval summation, and maximization templates (Interval Update templates to be continued)

Source: Internet
Author: User

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)

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.