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 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 4Sample Output
4
55
9
15Hint
The sums may exceed the range of 32-bit integers.
题意:线段树区间更新。
如果不太理解,可以输出中间变量,有助于理解。
代码:
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
typedef struct node
{
    int l, r;
    ll s, add;
// s 为点 l到r 数值之和。
// add 为点 l到r 需要增加的数。
} Tree;
const int N = 1000000;
Tree T[N];
void build(int l, int r,int i)
{
    T[i].l = l;
    T[i].r = r;
    T[i].add = 0;
    T[i].s = 0;
    if(r == l)
        return ;
    int mid = (l+r) >> 1;
    build(l, mid, i<<1);
    build(mid+1, r, i<<1|1);
    T[i].s = T[i<<1].s + T[i<<1|1].s;
}
void update(int l, int r, int i,int add)
{
    if(T[i].l > r || T[i].r < l)
        return ;
    if(T[i].l >= l && T[i].r <= r)
    {
        T[i].s += (T[i].r - T[i].l + 1)*add;
        T[i].add += add;
        return ;
    }
    if(T[i].add)
    {
        T[i<<1].s += (T[i<<1].r - T[i<<1].l + 1) * T[i].add;
        T[i<<1].add += T[i].add;
        T[i<<1|1].s += (T[i<<1|1].r - T[i<<1|1].l + 1) * T[i].add;
        T[i<<1|1].add += T[i].add;
        T[i].add = 0;
    }
    update(l, r, i<<1, add);
    update(l, r, i<<1|1, add);
    T[i].s = T[i<<1].s + T[i<<1|1].s;
}
ll ans = 0;
void query(int l, int r, int i)
{
    if(T[i].l > r || T[i].r < l)
        return ;
    if(T[i].l >= l && T[i].r <= r)
    {
        ans += T[i].s;
        return ;
    }
    if(T[i].add)
    {
        T[i<<1].s += (T[i<<1].r - T[i<<1].l + 1) * T[i].add;
        T[i<<1].add += T[i].add;
        T[i<<1|1].s += (T[i<<1|1].r - T[i<<1|1].l + 1) * T[i].add;
        T[i<<1|1].add += T[i].add;
        T[i].add = 0;
    }
    query(l, r, i<<1);
    query(l, r, i<<1|1);
    T[i].s = T[i<<1].s + T[i<<1|1].s;
}
int main()
{
    int n, q;
    scanf("%d %d",&n, &q);
    build(1, n, 1);
    for(int i=1; i<=n; i++)
    {
        int tem;
        scanf("%d",&tem);
        update(i, i, 1, tem);
    }
    for(int i=1; i<=q; i++)
    {
        getchar();
        char op;
        scanf("%c", &op);
        if(op == 'Q')
        {
            ans = 0;
            int l, r;
            scanf("%d %d",&l, &r);
            query(l, r, 1);
            printf("%lld\n",ans);
        }
        else
        {
            int a, b, c;
            scanf("%d %d %d",&a, &b, &c);
            update(a, b, 1, c);
        }
    }
    return 0;
}









