D - A Simple Problem with Integers


Time Limit: 5000MS
 
Memory Limit: 131072KB
 
64bit IO Format: %I64d & %I64u
[Submit]   [Go Back]   [Status]
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 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
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 <stdio.h>
#include <string.h>
#include <algorithm>
#include<iostream>
using namespace std;
#define MAXN 102000
struct  node 
{
    int l;
    int r;
    __int64 sum;
    __int64 add;
}t[4*MAXN];
__int64 rr[MAXN];
void build(int id,int l,int r)
{
    int mid = (l+r)/2;
    t[id].l = l;
    t[id].r = r;
    if(r==l)
    {
        t[id].sum = rr[l];
        t[id].add = 0;
    }
    else
    t[id].add = 0;

    if(r>l)
    {
        build(id*2,l,mid);
        build(id*2+1,mid+1,r);
        t[id].sum = t[id*2].sum + t[id*2+1].sum;
    }
}
void change(int id,int start,int end,__int64 num)
{
    if(start==t[id].l && end==t[id].r)
    {
        t[id].add += num;
        return ;
    }
    t[id].sum+= (end-start+1)*num;
    int mid = (t[id].l + t[id].r)/2;
    if(t[id].add!=0)
    {
        t[id].sum += (t[id].r-t[id].l+1)*t[id].add;
        change(id*2,t[id].l,mid,t[id].add);
        change(id*2+1,mid+1,t[id].r,t[id].add);
        t[id].add = 0;
    }
    if(mid>=end)
    {
         change(id*2,start,end,num);
         
    }
    else if(mid+1<=start)
    {
          change(id*2+1,start,end,num);
    }
    else
    {
        change(id*2,start,mid,num);
        change(id*2+1,mid+1,end,num);
    }
}

__int64 ans;
void find(int id,int start,int end)
{
    if (t[id].l==start && t[id].r == end)
    {
        ans+=t[id].sum+(t[id].r-t[id].l+1)*t[id].add;
        return;
    }
    if (t[id].add!=0)
    {
        t[id].sum+= (t[id].r-t[id].l+1)*t[id].add;
        t[id*2].add+=t[id].add;
        t[id*2+1].add+=t[id].add;
        t[id].add = 0;
    }

    int mid = (t[id].l+t[id].r)/2;
    if(mid>=end)
    find(id*2,start,end);
    else if(start>=mid+1)
    find(id*2+1,start,end);
    else
    {
        find(id*2,start,mid);
        find(id*2+1,mid+1,end);
    }

}

 

int main()
{
	int n,m;
    int i,j;
    char str[10];
    int start,end;
    __int64 num;
    while (scanf("%d%d",&n,&m)!=EOF)
    {
        for (i=1;i<=n;i++)
        {
            scanf("%I64d",&rr[i]);
        }
        build(1,1,n);
        for (i=0;i<m;i++)
        {
            scanf("%s",str);
            if (strcmp(str,"C")==0)
            {
                scanf("%d%d%I64d",&start,&end,&num);
                change(1,start,end,num);
            }
            else
            {
                scanf("%d%d",&start,&end);
                ans = 0;
                find(1,start,end);
                printf("%I64d
",ans); } } } return 0; }