POJ 3468 A Simple Problem with Integers (线段树)

A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 72740   Accepted: 22453
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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



模板题。
#include <iostream>

#include <cstdio>

#include <string>

#include <queue>

#include <vector>

#include <map>

#include <algorithm>

#include <cstring>

#include <cctype>

#include <cstdlib>

#include <cmath>

#include <ctime>

#include <climits>

using    namespace    std;



const    int    SIZE = 100005;

long    long    TREE[SIZE * 4],LAZY[SIZE * 4],N,Q;



void    build(int,int,int);

void    update(int,int,int,int,int,int);

void    push_down(int,int,int);

long    long    que(int,int,int,int,int);

int    main(void)

{

    char    op;

    int    a,b,c;



    while(~scanf("%d%d",&N,&Q))

    {

        build(1,1,N);

        while(Q --)

        {

            scanf(" %c%d%d",&op,&a,&b);

            if(op == 'Q')

                printf("%lld\n",que(a,b,1,1,N));

            else

            {

                scanf("%d",&c);

                update(a,b,1,1,N,c);

            }

        }

    }



    return    0;

}



void    build(int node,int left,int right)

{

    LAZY[node] = 0;

    if(left == right)

        scanf("%lld",&TREE[node]);

    else

    {

        int    mid = (left + right) >> 1;

        build(node * 2,left,mid);

        build(node * 2 + 1,mid + 1,right);

        TREE[node] = TREE[node * 2] + TREE[node * 2 + 1];

    }

}



void    update(int L,int R,int node,int left,int right,int add)

{

    if(left >= L && right <= R)

    {

        TREE[node] += add * (right - left + 1);

        LAZY[node] += add ;

        return    ;

    }

    if(right < L || left > R)

        return    ;



    push_down(node,left,right);

    int    mid = (left + right) >> 1;

    update(L,R,node * 2,left,mid,add);

    update(L,R,node * 2 + 1,mid + 1,right,add);

    TREE[node] = TREE[node * 2] + TREE[node * 2 + 1];

}



long    long    que(int L,int R,int node,int left,int right)

{

    if(left >= L && right <= R)

        return    TREE[node];

    if(right < L || left > R)

        return    0;



    push_down(node,left,right);

    int    mid = (left + right) >> 1;

    return    que(L,R,node * 2,left,mid) + que(L,R,node * 2 + 1,mid + 1,right);

}



void    push_down(int node,int left,int right)

{

    if(LAZY[node])

    {

        int    mid = (left + right) >> 1;

        TREE[node * 2] += LAZY[node] * (mid - left + 1);

        LAZY[node * 2] += LAZY[node];

        TREE[node * 2 + 1] += LAZY[node] * (right - mid);

        LAZY[node * 2 + 1] += LAZY[node];

        LAZY[node] = 0;

    }

}

 

你可能感兴趣的:(Integer)