A simple problem with integers

Source: Internet
Author: User

A simple problem with integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 99895 Accepted: 31162
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 numbersNandQ. 1≤N,Q≤100000.
The second line containsNNumbers, the initial values ofA1,A2, ... ,AN . -1000000000≤Ai ≤1000000000.
Each of the nextQLines represents an operation.
"Ca b C"Means addingCTo each ofaa ,aa +1, ... ,Ab . -10000≤C≤10000.
"Qa b"means querying the sum ofaa ,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
/*Hand Knock Template results various hand remnants! */#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#defineN 100010#definell Long Long#defineLson i*2,l,m#defineRson I*2+1,m+1,rusing namespacestd;/*when the segment tree interval is updated, the updated values are stored in the array add array, which you need to add to the sum array .*/ll Sum[n*4];ll add[n*4];voidPushdown (intIintNum///Update down{    if(Add[i])///equals 0, there's no need to update it.{sum[i*2] + = add[i]* (num-(num/2)); Sum[i*2+1] + = add[i]* (num/2); Add[i*2]+=Add[i]; Add[i*2+1]+=Add[i]; Add[i]=0;///The information of this node is updated, then there is nothing in the corresponding add array .    }}voidPushup (intI///Update down{Sum[i]=sum[i*2]+sum[i*2+1];}voidBuildintIintLintR) {Add[i]=0;///Initialize the value updated for each node to 0    if(l==r) {scanf ("%lld",&Sum[i]); //cout<<sum[i]<< "";        return ; }    intM= (L+R)/2;    Build (Lson);    Build (Rson); Pushup (i);}voidUpdateintQlintQrintValintIintLintR) {    if(ql<=l&&r<=qr) {Add[i]+=Val; Sum[i]+ = (ll) val* (r-l+1); return ; } pushdown (I,r-l+1); intM= (L+R)/2; if(ql<=m) update (Ql,qr,val,lson); if(m<qr) update (Ql,qr,val,rson); Pushup (i);} ll query (intQlintQrintIintLintR) {    //cout<< "l=" <<l<< "r=" <<r<<endl;    if(ql<=l&&r<=qr) {        returnSum[i]; } pushdown (I,r-l+1); intM= (L+R)/2; LL cur=0; if(ql<=m) cur+=query (Ql,qr,lson); if(M&LT;QR) cur+=query (Ql,qr,rson); returncur;}intMain () {//freopen ("C:\\users\\acer\\desktop\\in.txt", "R", stdin);    intn,q; Charop;  while(SCANF ("%d%d", &n,&q)! =EOF) {        //cout<<n<< "" <<m<<endl;Build1,1, N); //for (int i=1;i<= (n (n+1)/2); i++)//cout<<sum[i]<< ""; //cout<<endl; //cout<<endl;        intA,b,c;        GetChar ();  while(q--) {scanf ("%c",&op); //cout<<op<< "";            if(op=='Q') {scanf ("%d%d",&a,&b); //cout<<a<< "" <<b<<endl; //cout<< "Q" <<endl;printf"%lld\n", query (A, B,1,1, N)); }            Else{scanf ("%d%d%d",&a,&b,&c); //cout<< "C" <<endl;Update (A,B,C,1,1, N);        } getchar (); }    }    return 0;}

A simple problem with integers

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.