POJ 3468-A Simple Problem with Integers(线段树_区间更新+lazy标记)

来源:互联网 发布:做网络销售公司好做吗 编辑:程序博客网 时间:2024/04/30 02:31

A Simple Problem with IntegersCrawling in process...Crawling failedTime Limit:5000MS    Memory Limit:131072KB    64bit IO Format:%I64d & %I64u

SubmitStatus

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 abc" means adding c to each of Aa,Aa+1, ... ,Ab. -10000 ≤ c ≤ 10000.
"Q ab" 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 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4

Sample Output

455915

Hint

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


题意: 给你N个数,Q个操作,操作有两种,‘Q a b ’是询问a~b这段数的和,‘C a b c’是把a~b这段数都加上c;
思路:此题运用到了lazy思想,就是记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。
ps:这一篇博客写的很详细,可以看看,加深理解lazy http://blog.csdn.net/acceptedxukai/article/details/6933446

#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const double pi= acos(-1.0);#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1const int MAXN=100010;LL sum[MAXN<<2];LL lazy[MAXN<<2];void PushUp(int rt){    sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void PushDown(int rt,int m)//lazy的思想在这体现;就是从当前根节点rt向下更新每个子节点的值 {    if(lazy[rt]){        lazy[rt<<1]+=lazy[rt];        lazy[rt<<1|1]+=lazy[rt];        sum[rt<<1]+=lazy[rt]*(m-(m>>1));        sum[rt<<1|1]+=lazy[rt]*(m>>1);        lazy[rt]=0;//更新后需要还原    }}void Build(int l,int r,int rt){    lazy[rt]=0;    if(l==r){        scanf("%lld",&sum[rt]);        return ;    }    int mid=(l+r)>>1;    Build(lson);    Build(rson);    PushUp(rt);}void Update(int ll,int rr,int x,int l,int r,int rt){    if(ll<=l&&rr>=r){        lazy[rt]+=x;        sum[rt]+=(LL)x*(r-l+1);        return ;    }    PushDown(rt,r-l+1);    int mid=(l+r)>>1;    if(ll<=mid)        Update(ll,rr,x,lson);    if(rr>mid)        Update(ll,rr,x,rson);    PushUp(rt);}LL Query(int ll,int rr,int l,int r,int rt){    if (ll<=l&&rr>=r){        return sum[rt];    }    PushDown(rt,r-l+1);    LL ans=0;    int mid=(r+l)>>1;    if(ll<=mid)        ans+=Query(ll,rr,lson);    if(rr>mid)        ans+=Query(ll,rr,rson);    return ans;}int main(){    int n,q;    int a,b,c;    char str[10];    scanf("%d %d",&n,&q);    Build(1,n,1);    while(q--){        getchar();        scanf("%s",str);        if(str[0]=='Q'){            scanf("%d %d",&a,&b);            LL ans=Query(a,b,1,n,1);            printf("%lld\n",ans);        }        else{            scanf("%d %d %d",&a,&b,&c);            Update(a,b,c,1,n,1);        }    }    return 0;}


0 0
原创粉丝点击