| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 83959 | Accepted: 25989 | |
| Case Time Limit: 2000MS | ||
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
Source
#include <iostream>
#include <cstdio>
using namespace std;
#define LL __int64
struct node
{
    int l,r;
    LL sum;
    LL add;
    //int flag;//用来表示有几个加数
} s[100000*4];
void InitTree(int l,int r,int k)
{
    s[k].l=l;
    s[k].r=r;
    s[k].sum=0;
    s[k].add=0;
    if (l==r)
        return ;
    int mid=(l+r)/2;
    InitTree(l,mid,2*k);
    InitTree(mid+1,r,2*k+1);
}
void UpdataTree(int l,int r,LL add,int k)
{
    if (s[k].l==l&&s[k].r==r)
    {
        s[k].add+=add;
        s[k].sum+=add*(r-l+1);
        return ;
    }
    if (s[k].add!=0)//加数为0就不需要改变了
    {
        s[2*k].add+=s[k].add;
        s[2*k+1].add+=s[k].add;
        s[2*k].sum+=s[k].add*(s[2*k].r-s[2*k].l+1);
        s[2*k+1].sum+=s[k].add*(s[2*k+1].r-s[2*k+1].l+1);
        s[k].add=0;
    }
    int mid=(s[k].l+s[k].r)/2;
    if (l>mid)
        UpdataTree(l,r,add,2*k+1);
    else if (r<=mid)
        UpdataTree(l,r,add,2*k);
    else
    {
        UpdataTree(l,mid,add,2*k);
        UpdataTree(mid+1,r,add,2*k+1);
    }
    s[k].sum=s[2*k].sum+s[2*k+1].sum;
}
LL SearchTree(int l,int r,int k)
{
    if (s[k].l==l&&s[k].r==r)
        return s[k].sum;
    if (s[k].add!=0)
    {
        s[2*k].add+=s[k].add;
        s[2*k+1].add+=s[k].add;
        s[2*k].sum+=s[k].add*(s[2*k].r-s[2*k].l+1);
        s[2*k+1].sum+=s[k].add*(s[2*k+1].r-s[2*k+1].l+1);
        s[k].add=0;
    }
    int mid=(s[k].l+s[k].r)/2;
    if (l>mid)
        return SearchTree(l,r,2*k+1);
    else if (r<=mid)
        return SearchTree(l,r,2*k);
    else
        return SearchTree(l,mid,2*k)+SearchTree(mid+1,r,2*k+1);
}
int main()
{
    int n,q;
    LL w;
    while (~scanf("%d%d",&n,&q))
    {
        InitTree(1,n,1);
        for (int i=1; i<=n; i++)
        {
            scanf("%lld",&w);
            UpdataTree(i,i,w,1);
        }
        for (int i=1; i<=q; i++)
        {
            char ch;
            int a,b;
            LL c;
            getchar();
            scanf("%c%d%d",&ch,&a,&b);
            if (ch=='C')
            {
                scanf("%lld",&c);
                UpdataTree(a,b,c,1);
            }
            else if (ch=='Q')
            {
                LL ans=SearchTree(a,b,1);
                printf ("%lld\n",ans);
            }
        }
    }
    return 0;
}
poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)
原文:http://blog.csdn.net/qiqi_skystar/article/details/50544715