POJ3468 A Simple Problem with Integers (成段更新,区间求和+lazy)

A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 68153 Accepted: 21020
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

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

Source

 
 
代码:
#include<cmath>
#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;

#define ls ( o << 1 )
#define rs ( o << 1 | 1 )
#define lson ls , l , mid
#define rson rs , mid + 1 , r
#define root 1 , 1 , n
#define rt o , l , r

#define LL long long
const int N = 100100;

int t[N];

struct node
{
    int le,re,lazy;
    long long sum,insum;
}s[N*4];
void pushdown(int o)
{
    s[ls].insum += s[o].insum;
    s[rs].insum += s[o].insum;
    s[ls].sum += (s[ls].re-s[ls].le+1)*s[o].insum;
    s[rs].sum += (s[rs].re-s[rs].le+1)*s[o].insum;
    s[ls].lazy = s[rs].lazy = 1
    s[o].lazy = s[o].insum = 0;
}
void build (int o , int l , int r)
{
    s[o].le = l,s[o].re = r;
    s[o].insum = 0;
    s[o].lazy = 0;
    if (l == r)
    {
        s[o].sum = t[l];
        return;
    }
    int mid = ( l + r ) >> 1;
    build(lson);
    build(rson);
    s[o].sum = s[ls].sum + s[rs].sum;
}
void update(int o , int l , int r, int v)
{
    if(s[o].le == l && s[o].re == r)
    {
        s[o].lazy = 1;
        s[o].insum += v;
        s[o].sum += (LL)(r-l+1)*v;//防止溢出
        return ;
    }
    if(s[o].lazy)
        pushdown(o);
    int mid = ( s[o].le + s[o].re ) >> 1;
    if(r <= mid)
        update(ls, l, r, v);
    else if(l >= mid+1)
        update(rs, l, r, v);
    else
    {
        update(lson,v);
        update(rson,v);
    }
    s[o].sum = s[ls].sum + s[rs].sum;
}
LL query(int o, int l, int r)
{
    long long ans = 0;
    if(s[o].le == l && s[o].re == r)
        return s[o].sum;
    if(s[o].lazy)
        pushdown(o);
    int mid = ( s[o].le + s[o].re ) >> 1;
    if(r <= mid)
        ans = query(ls, l, r);
    else if(l >= mid+1)
        ans = query(rs, l, r);
    else
    {
        ans +=query(lson);
        ans +=query(rson);
    }
    return ans;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int n,q,a,b,c;
    char op[5];
    scanf("%d%d",&n,&q);
    for(int i=1; i<=n; i++)
        scanf("%d",&t[i]);
    build(1,1,n);
    for(int i=1; i<=q; i++)
    {
        scanf("%s",op);
        if(op[0] == 'C')
        {
            scanf("%d%d%d",&a,&b,&c);
            update(1,a,b,c);
        }
        else
        {
            scanf("%d%d",&a,&b);
            printf("%I64d\n",query(1,a,b));
        }
    }
    return 0;
}
View Code
posted @ 2015-02-21 23:34  Doli  阅读(111)  评论(0)    收藏  举报