POJ-3468-A Simple Problem with Integers(线段树 区间更新 区间和)

A Simple Problem with Integers

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 139191   Accepted: 43086
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 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

Hint

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

线段树模板题

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

struct node
{
    long long int lazy;
    long long int data;
    long long int l, r;
};

struct node tree[10000000];
long long int Begin[10000000];

void Buildtree( long long int root, long long int l, long long int r )
{
    tree[root].l = l;
    tree[root].r = r;
    tree[root].lazy = 0;

    if( l == r )
        tree[root].data = Begin[l];
    else
    {
        long long int mid = ( l + r ) / 2;
        Buildtree( root*2+1, l, mid );
        Buildtree( root*2+2, mid+1, r );

        tree[root].data = tree[root*2+1].data + tree[root*2+2].data;
    }

}

void Pushdown( long long int root )
{
    if( tree[root].lazy != 0 )
    {
        tree[root*2+1].lazy += tree[root].lazy;
        tree[root*2+2].lazy += tree[root].lazy;

        tree[root*2+1].data += (tree[root*2+1].r - tree[root*2+1].l + 1)*tree[root].lazy;
        tree[root*2+2].data += (tree[root*2+2].r - tree[root*2+2].l + 1)*tree[root].lazy;

        tree[root].lazy = 0;
    }
}

void Updata ( long long int root, long long int l, long long int r, long long int z )
{
    long long int i = tree[root].l, j = tree[root].r;

    if( l <= i && j <= r )
    {
        tree[root].lazy += z;
        tree[root].data += ( j - i + 1 ) * z;
        return;
    }

    Pushdown(root);
    long long int mid = ( i + j ) / 2;

    if( l <= mid )
        Updata( root*2+1, l, r, z );
    if( r > mid )
        Updata( root*2+2, l, r, z );

    tree[root].data = tree[root*2+1].data + tree[root*2+2].data;
    return;
}

long long int Query ( long int root, long long int l, long long int r )
{
    long long int i = tree[root].l, j = tree[root].r;

    if( l <= i && r >= j )
        return tree[root].data;

    Pushdown( root );
    long long int mid = ( i + j ) / 2;
    long long int sum = 0;
    if( l <= mid )
        sum += Query( root*2+1, l, r  );
    if( r > mid )
        sum += Query( root*2+2, l, r  );

    return sum;
}







int main()
{
    long long int i, n, q;
    scanf("%lld %lld", &n, &q);
    for( i=1; i<=n; i++ )
        scanf("%lld", &Begin[i]);
    Buildtree( 0, 1, n );

    while( q-- )
    {
        char order;
        long long int a, b, c;
        getchar();
        scanf("%c", &order);
        if( order == 'C' )
        {
            scanf("%lld %lld %lld", &a, &b, &c);
            Updata( 0, a, b, c);
        }
        else if( order == 'Q' )
        {
            scanf("%lld %lld", &a, &b);
            printf("%lld\n", Query( 0, a, b ));
        }
    }

	return 0;
}

猜你喜欢

转载自blog.csdn.net/winner647520/article/details/81516421