Line Segment tree (junior template)

Source: Internet
Author: User

The up function is used to update the value of the Father's Day point void push (int w) {sum [w] = sum [2 * w] + sum [2 * w + 1]; // update node value} spof update first finds the number of P and then updates its value void add (int p, int D, int L, int R, int W) {If (L = r) {sum [w] + = D; return;} int M = (L + r)/2; If (P <= m) add (p, D, L, M, 2 * w); else add (p, D, m + 1, R, 2 * w + 1 ); push (w );} **************************************** ** interval summation int query (INT P1, int P2, int L, int R, int W) {If (P1 <= L & P2> = r) // The range is completely overwritten return sum [w]; int M = (L + r)/2; int Re = 0; If (P1 <= m) RE + = query (P1, P2, L, M, 2 * w); If (P2> m) RE + = query (P1, P2, m + 1, R, 2 * w + 1); Return re ;} ************************************* 88 interval Update down function void Pushdown (int w, int m) {If (LZ [w]) {LZ [2 * w] + = LZ [w]; LZ [2 * w + 1] + = LZ [w]; sum [2 * w] + = (LZ [w] * (m-(m/2 ))); sum [2 * w + 1] + = (LZ [w] * (M/2); LZ [w] = 0; // remove latency tags} void Update (int A, int B, int da, int L, int R, int W) {if (a <= L & B> = r) {LZ [w] + = da; // Multiple Delay markers sum [w] + = da * (R-l + 1); return;} Pushdown (W, R-l + 1 ); // update int M = (L + r)/2; if (a <= m) add (a, B, da, L, M, 2 * w ); if (B> m) add (a, B, da, m + 1, R, 2 * w + 1); pushup (w );}

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.