poj3468 A simple problem with integers (tree-like array procedure)

Source: Internet
Author: User

Topic PortalA simple problem with integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 142198 Accepted: 44136
Case Time Limit: 2000MS

Description

You have N integers, a1, a2, ..., an. You need to deal with both kinds of operations. One type of operation is to add some given number to each number in a given interval. The other are to ask for the sum of numbers in a given interval.

Input

The first line contains the numbers N and Q. 1 ≤ N,Q ≤100000.
The second line contains N numbers, the initial values of a1, a2, ..., an. -1000000000≤ Ai ≤1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding C to each of AA, aa+1, ..., ab. -10000≤ C ≤10000.
"Q a b" means querying the sum of aa, aa+1, ..., Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

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, Yang Yi Test Instructions: to a sequence, q operation, encountered C to [L,r] and add X, encountered Q to ask [L,r] and Solution : tree-like array approach The basic function of the tree array is to realize the single-point updating and the prefix and to realize the update of the fast implementation interval. first, let's look at [l,r] plus x, i<l, when the prefix and invariant, l<=i<=r, the prefix and increase x* (i-l+1) =x*i-x* (L-1);I>r, increased x* (r-l+1) =x*r-x* (L-1);At this point we define two tree-like arraysbit1: Prefixes and tree-like arraysbit0: Addition tree-like arrayfrom the above prefix and increment, the prefix from greater than L and will add a thing, that is-x* (L-1), then the maintenance of this thing is Updata (bit0,l,-x* (L-1));This will achieve the L prefix and will add this thing, and then we find x*r This can also be pre-maintenance, the same is Updata (bit0,r,x*r);a x*i, because I is a variable, so this can only be added when summing, while the addition array {updata (bit1,l,x); Updata (bit1,r,x);} We found this justgood implementation of the [L,r] prefix and add x, then for each of the I, in the prefix and the time to *i on the line.then Sum[i]=sum (bit1,i) *i+sum (bit0,i);Code:
#include <iostream>#include<string.h>using namespaceStd;typedefLong Longll;#defineMAX 100005intn,q;intA[max];ll Bit0[max],bit1[max];voidUpdata (ll *b,intIintval) {     while(i<=N) {B[i]+=Val; I+=i&-i; }}ll Query (LL*b,inti) {LL res=0;  while(i>0) {res+=B[i]; I-=i&-i; }    returnRes;}intMain () {Ios_base::sync_with_stdio (0); Cin.tie (0); Cout.tie (0);  while(cin>>n>>q) {memset (bit0,0,sizeof(bit0)); memset (Bit1,0,sizeof(bit1));  for(intI=1; i<=n;i++) {cin>>A[i];        Updata (Bit0,i,a[i]); }         while(q--)        {            intl,r,x; Charch; CIN>>ch; CIN>>l>>R; if(ch=='C') {cin>>x; Updata (bit0,l,-x* (l1));                Updata (BIT1,L,X); Updata (Bit0,r+1, x*R); Updata (Bit1,r+1,-x); }            Else{ll sum=0; Sum+=query (bit0,r) +query (bit1,r) *R; Sum-=query (bit0,l-1) +query (bit1,l-1) * (l1); cout<<sum<<Endl; }        }    }}

Reference Blog: https://www.cnblogs.com/detrol/p/7586083.html

79098936

poj3468 A simple problem with integers (tree-like array procedure)

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.