Poj 3468 a simple problem with integers (segment tree region update)

Source: Internet
Author: User
A simple problem with Integers
Time limit:5000 Ms   Memory limit:131072 K
Total submissions:62431   Accepted:19141
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 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4

Sample output

455915

Hint

The sums may exceed the range of 32-bit integers.

Source

Poj monthly -- 2007.11.25, basic region update of the Yang Yi line segment tree
1 # include <cstdio> 2 # include <iostream> 3 # include <cstring> 4 # include <stdlib. h> 5 # include <algorithm> 6 # define ll _ int64 7 using namespace STD; 8 const int maxn = 100000 + 10; 9 const int INF = 0x3f3f3f; 10 int B [maxn]; 11 struct node 12 {13 int L, R; 14 LL Val, Col; 15 int mid () 16 {17 return (L + r) /2; 18} 19} A [maxn * 4]; 20 21 void btree (INT step, int L, int R) 22 {23 A [STEP]. L = L; 24 A [STEP]. R = r; 25 A [STEP]. col = 0; 26 if (L = r) 27 {28 A [STEP]. val = B [l]; 29 // scanf ("% d", & A [STEP]. val); the input will be wa 30 return; 31} 32 int mid = A [STEP]. mid (); 33 btree (Step * 2, L, mid); 34 btree (Step * 2 + 1, Mid + 1, R); 35 A [STEP]. val = A [step * 2]. val + A [step * 2 + 1]. val; 36} 37 38 void ptree (INT step, int L, int R, ll num) 39 {40 if (L = A [STEP]. L & A [STEP]. R = r) // If the matching region is found, the record will not be updated. The value to be updated is 41 {42 A [STEP]. col + = num; 43 return; 44} 45 46 [STEP]. val + = (R-L + 1) * num; // if it is not a qualified region, update 47 48 int mid = A [STEP] for the current node. mid (); 49 If (r <= mid) 50 ptree (Step * 2, L, R, num); 51 else if (L> mid) 52 ptree (Step * 2 + 1, L, R, num); 53 else 54 {55 ptree (Step * 2, L, mid, num ); 56 ptree (Step * 2 + 1, Mid + 1, R, num); 57} 58} 59 60 ll qtree (INT step, int L, int R) 61 {62 if (L = A [STEP]. L & A [STEP]. R = r) 63 {64 return a [STEP]. val + A [STEP]. col * (R-L + 1); 65} 66 67 a [STEP]. val + = (a [STEP]. R-A [STEP]. L + 1) * A [STEP]. col; // update the current node 68 69 int mid = A [STEP]. mid (); // update 70 ptree to the child node (Step * 2, a [STEP]. l, mid, a [STEP]. COL); 71 ptree (Step * 2 + 1, Mid + 1, a [STEP]. r, a [STEP]. COL); 72 A [STEP]. col = 0; 73 74 if (r <= mid) 75 return qtree (Step * 2, L, R); 76 else if (L> mid) 77 return qtree (Step * 2 + 1, L, R); 78 else 79 return qtree (Step * 2, L, mid) + qtree (Step * 2 + 1, mid + 1, R); 80} 81 int main () 82 {83 // freopen ("in.txt", "r", stdin ); 84 int N, Kase; 85 char ch; 86 int star, en, num; 87 while (scanf ("% d", & N, & Kase )! = EOF) 88 {89 for (INT I = 1; I <= N; I ++) 90 scanf ("% d", & B [I]); 91 btree (1, 1, n); 92 While (Kase --) 93 {94 CIN> CH; 95 If (CH = 'C ') 96 {97 scanf ("% d", & star, & en, & num); // input % i64d will re 98 ptree (1, star, en, num); 99} 100 else101 {102 scanf ("% d", & star, & en); 103 printf ("% i64d \ n ", qtree (1, Star, en); 104} 105} 106 return 0; 107}
View code

 

Poj 3468 a simple problem with integers (segment tree region update)

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.