A simple problem with integers POJ 3,468 multi-tree array solves the problem of interval modification.

Source: Internet
Author: User

A simple problem with integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 69589 Accepted: 21437
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
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <string>7#include <vector>8#include <Set>9#include <map>Ten#include <stack> One#include <queue> A#include <sstream> -#include <iomanip> - using namespacestd; thetypedefLong LongLL; - Const intinf=0x4fffffff; - Const intexp=1e-5; - Const intms=100005; +  - LL Sum[ms]; +LL c[2][ms]; A intn,q; at  - intLowbit (intx) - { -       returnx& (-x); - } -  in voidUpdata (intNointx,ll value) - { to        while(x<=N) +       { -c[no][x]+=value; thex+=lowbit (x); *       } $ }Panax Notoginseng  -LL Getsum (intNointx) the { +LL res=0; A        while(x>0) the       { +res+=C[no][x]; -x-=lowbit (x); $       } $       returnRes; - } -  the voidSolve () - {Wuyiscanf"%d%d",&n,&Q); thesum[0]=0; -        for(intI=1; i<=n;i++) Wu       { -scanf"%lld",&sum[i]); Aboutsum[i]+=sum[i-1]; $       } -memset (C,0,sizeof(C)); -       intL,r; - LL C; A       Charcmd[Ten]; +        for(intI=1; i<=q;i++) the       { -scanf"%s", cmd); $             if(cmd[0]=='Q') the             { thescanf"%d%d",&l,&R); theLL ans=sum[r]-sum[l-1]+ (Getsum (0, R)-getsum (1, R) * (N-R))-(Getsum (0, L-1)-getsum (1, L-1) * (n-l+1)); theprintf"%lld\n", ans); -             } in             Else the             { thescanf"%d%d%lld",&l,&r,&c); AboutUpdata (0, l,c* (n-l+1)); theUpdata (0, r+1, (N-R) * (-c)); the  theUpdata (1, l,c); +Updata (1, r+1,-c); -             } the       }Bayi } the  the  - intMain () - { the solve (); the       return 0; the}

A simple problem with integers POJ 3,468 multi-tree array solves the problem of interval modification.

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.