A Simple Problem with Integers



Description

You have N integers, 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 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 abc" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q ab" 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

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

4
55
9
15

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


#include
#define CLEAR(a) memset((a),0,sizeof((a)))

using namespace std;

typedef long long LL;
const int maxn=1e5+10;
const int maxl=4*maxn;
const int inf=99999999;
const float eps=1e-3;

int L[maxl],R[maxl],mid[maxl];
LL v[maxl],d[maxl],a[maxl];

inline void pullup(int k)
{
    d[k]=d[k<<1]+d[k<<1|1];
}

void pushdown(int k,LL num)
{
    if(v[k])
    {
        v[k<<1]+=v[k];
        v[k<<1|1]+=v[k];
        d[k<<1]+=(num-(num>>1))*v[k];
        d[k<<1|1]+=(num>>1)*v[k];
        v[k]=0;
    }
}

void build(int l,int r,int k)
{
    L[k]=l;R[k]=r;mid[k]=(l+r)>>1;
    v[k]=0;
    if (l=R[k])
    {
        d[k]+=num*(R[k]-L[k]+1);
        v[k]+=num;
        return;
    }
    pushdown(k,R[k]-L[k]+1);
    if (x<=mid[k]) modify(k<<1,x,y,num);
    if (y>mid[k]) modify(k<<1|1,x,y,num);
    pullup(k);
}

LL query(int k,int x,int y)
{
    if (x<=L[k]&&y>=R[k]) return d[k];
    pushdown(k,R[k]-L[k]+1);
    pullup(k);
    return ((x<=mid[k])?query(k<<1,x,y):0)+((y>mid[k])?query((k<<1)|1,x,y):0);
}

int main()
{

    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        //CLEAR(v);CLEAR(d);
        for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
        build(1,n,1);
        for(int i=1;i<=m;i++)
        {
            int a,b;
            LL c;
            char s[3];
            scanf("%s",s);
            if (s[0]=='C')
            {
                scanf("%d%d%I64d",&a,&b,&c);
                modify(1,a,b,c);
            }
            else if (s[0]=='Q')
            {
                scanf("%d%d",&a,&b);
                printf("%I64d\n",query(1,a,b));
            }
        }
    }
    return 0;
}


你可能感兴趣的:(ACM)