Poj 3468 a simple problem with integers (line segment tree)

Source: Internet
Author: User

// Line segment tree <br/> # include <iostream> <br/> using namespace STD; <br/> // note the unification of variables. When you use long int, in the end, all values are summed up by long Int. Otherwise, WA <br/> long int data [100010], ans; <br/> struct seg <br/> {<br/> long int sum, add, L, R; // sum indicates the sum of the modified intervals, add indicates the increase in the range <br/>} segtree [400010]; <br/> // the space of the static line segment tree must be four times the size of the given range, the worst case is that the bottom layer of the tree is divided into one interval point and one empty node <br/> void buildtree (int fa, int L, int R) // create a line segment tree <br/>{< br/> segtree [fa]. L = L; <br/> segtree [fa]. R = r; <br/> segtree [fa]. add = 0; <br/> If (L = r) segtree [fa]. sum = data [l]; <br/> else <br/>{< br/> int mid = (L + r)> 1; <br/> buildtree (2 * Fa, L, mid); <br/> buildtree (2 * Fa + 1, Mid + 1, R ); <br/> segtree [fa]. sum = segtree [2 * Fa]. sum + segtree [2 * Fa + 1]. SUM; // Add and <br/>}< br/> void modify (int fa, int L, int R, int add) // modify the range value <br/>{< br/> If (segtree [fa]. L = L & segtree [fa]. R = r) // indicates the range marker with full coverage, indicating that all values in this range must be added with add <br/> {<br/> segtree [fa]. add + = add; <br/> return; <br/>}< br/> segtree [fa]. sum + = add * (R-l + 1); // if it is a full overwrite range, add the sum value in the process of finding the overwrite range, note the order of the preceding steps <br/> int mid = (segtree [fa]. L + segtree [fa]. r)> 1; <br/> If (r <= mid) <br/> modify (2 * Fa, L, R, add ); <br/> else if (L> mid) <br/> modify (2 * Fa + 1, L, R, add ); <br/> else <br/> {<br/> modify (2 * Fa, L, mid, add); <br/> modify (2 * Fa + 1, mid + 1, R, add); <br/>}< br/> void search (int fa, int L, int R, int add) // search across zones <br/>{< br/> Add + = segtree [fa]. add; // with the search process, record the size to be increased within the full coverage interval <br/> If (segtree [fa]. L = L & segtree [fa]. R = r) <br/>{< br/> ans + = segtree [fa]. sum + Add * (segtree [fa]. r-segtree [fa]. L + 1); // locate the target interval and add ans <br/> return; <br/>}< br/> int mid = (segtree [fa]. L + segtree [fa]. r)> 1; <br/> If (r <= mid) <br/> Search (2 * Fa, L, R, add ); <br/> else if (L> mid) <br/> Search (2 * Fa + 1, L, R, add ); <br/> else <br/> {<br/> Search (2 * Fa, L, mid, add); <br/> Search (2 * Fa + 1, mid + 1, R, add); <br/>}< br/> int main () <br/>{< br/> // freopen ("in.txt", "r", stdin); <br/> int n, m, a, B, add; <br/> char command; <br/> scanf ("% d", & N, & M); <br/> for (INT I = 1; I <= N; ++ I) scanf ("% i64d", & Data [I]); </P> <p> buildtree (1,1, N ); </P> <p> for (INT I = 1; I <= m; ++ I) <br/> {<br/> scanf ("/N "); // clear the buffer. When reading a single string, <br/> scanf ("% C", & command); <br/> If (command = 'C ') <br/>{< br/> scanf ("% d", & A, & B, & add); <br/> modify (1,, b, add); <br/>}< br/> else <br/> {<br/> ans = 0; <br/> scanf ("% d", & A, & B); <br/> Search (1, A, B, 0 ); <br/> printf ("% i64d/N", ANS); <br/>}< br/> return 0; <br/>}< br/>

 

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.