Poj 3468 line segment tree

Source: Internet
Author: User
A Simple Problem with Integers
Time Limit:5000 MS   Memory Limit:131072 K
Total Submissions:19890   Accepted:5238

Case Time Limit:2000 MS

 

Description

You haveNIntegers,A1,A2 ,...,AN. You need to deal with two kinds of operations. one type of operation is to add some given number to each number in a given interval. the other is to ask for the sum of numbers in a given interval.

 

Input

The first line contains two numbersNAndQ. 1 ≤N,Q≤ 100000.
The second line containsNNumbers, the initial valuesA1,A2 ,...,AN.-1000000000 ≤Ai≤ 1000000000.
Each of the nextQLines represents an operation.
"CA B C"Means addingCTo eachAa,Aa+ 1 ,...,AB.-10000 ≤C≤ 10000.
"QA B"Means querying the sumAa,Aa+ 1 ,...,AB.

 

Output

You need to answer allQCommands in order. One answer in a line.

 

 

Sample Input

 

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

455915

Hint

The sums may exceed the range of 32-bit integers. classic questions about the number of line segments, quickly calculate the total range, add an attribute to each line segment node to represent the total increase of the line segment, if you want to find the Child Line Segment and the Child Line Segment, you can use the sum of the Child section and the length of the Child dangerous section * add .. # Include <iostream> <br/> using namespace std; </p> <p> struct LINE <br/>{< br/> int left, right; <br/>__ int64sum, add; <br/>} tree [500000]; </p> <p> void build_line_tree (int v, int left, int right) // create a line segment tree <br/>{< br/> int mid = (left + right)/2; </p> <p> if (left> right) <br/> return; </p> <p> tree [v]. left = left; <br/> tree [v]. right = right; <br/> tree [v]. sum = 0; <br/> tree [v]. add = 0; <br/> if (left = right) <br/> return; <br/> build_l Ine_tree (v + 1) * 2-1, left, mid); <br/> build_line_tree (v + 1) * 2, mid + 1, right ); <br/>}</p> <p> void add_line_tree (int v, int left, int right, int C) <br/> {<br/> int mid = (tree [v]. left + tree [v]. right)/2; <br/> if (left = tree [v]. left & right = tree [v]. right) <br/>{< br/> if (left! = Right) <br/> tree [v]. add + = C; </p> <p> tree [v]. sum + = (right-left + 1) * C; <br/> return; <br/>}< br/> tree [v]. sum + = (right-left + 1) * C; <br/> if (mid> = right) <br/> add_line_tree (v + 1) * 2-1, left, right, C); <br/> else if (mid <left) <br/> add_line_tree (v + 1) * 2, left, right, C ); <br/> else <br/> {<br/> add_line_tree (v + 1) * 2-1, left, mid, C ); <br/> add_line_tree (v + 1) * 2, mid + 1, right, C ); <br/>}</p> <p >__ int64 getsum_line_tree (in T v, int left, int right) <br/>{< br/>__ int64 mid = (tree [v]. left + tree [v]. right)/2, j; <br/> if (left = tree [v]. left & right = tree [v]. right) <br/> {<br/> return tree [v]. sum; <br/>}< br/> j = tree [v]. add * (right-left + 1); <br/> if (mid> = right) <br/> return j + getsum_line_tree (v + 1) * 2-1, left, right); <br/> else if (mid <left) <br/> return j + getsum_line_tree (v + 1) * 2, left, right ); <br/> else <br/> {<br/> return j + getsum_line_tr Ee (v + 1) * 2-1, left, mid) + getsum_line_tree (v + 1) * 2, mid + 1, right ); <br/>}</p> <p> int main () <br/>{< br/> int n, I, c, t, a, B, d; char o; <br/> while (scanf ("% d", & n )! = EOF) <br/>{< br/> build_line_tree (0, 1, n); <br/> cin> c; <br/> for (I = 1; I <= n; I ++) <br/>{< br/> scanf ("% d", & t); <br/> add_line_tree (0, I, i, t); <br/>}< br/> while (c --) <br/>{< br/> cin> o; <br/> if (o = 'q') <br/>{< br/> scanf ("% d", & a, & B ); <br/> printf ("% I64d/n", getsum_line_tree (0, a, B )); <br/>}< br/> else <br/> {<br/> scanf ("% d", & a, & B, & d ); </p> <p> add_line_tree (0, a, B, d); <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.