POJ 3468 A Simple Problem with Integers

来源:互联网 发布:神户钢铁造假 知乎 编辑:程序博客网 时间:2024/05/17 02:36

Time Limit:5000MS Memory Limit:131072KB

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.

【题意】 给一段序列,有两种指令,”C a b c”表示给[a, b]区间中的值全部增加c,”Q a b” 询问[a, b]区间中所有值的和。

【解题思路】 一道存粹的线段树区间更新求和题目,单点更新的升级版,其实做法和单点更新差不多,相较于单点更新它多了一个延缓标记—-laz,并多了两个函数,用于更新传递laz标记及节点的值,此题关键就是节点信息的更新及传递,所以要理解laz的用处,并在恰当的位置加入一个pushdown()函数。

(laz的用途)
每次更新并不需要更新到叶节点;
只需更新到相应的段就可以了,然后记录laz;
下次更新或者查询的时候,如果要查到该段的子节点;
就把laz加到子节点上去,再将该laz设为0;
这样就可以节省很多的时间。

【AC代码】

#include<stdio.h>#define fff 100005using namespace std;long long a[fff];struct node{    int l,r,len;    long long sum,laz;}tr[fff*4];void pushup(int id)  //向上更新节点的值{    tr[id].sum=tr[id*2].sum+tr[id*2+1].sum;}void pushdown(int id)  //将当前结点的laz标记向下传递,并向下更新节点的值(记得将当前节点的laz标记置零){    if(tr[id].laz)    {        tr[id*2].laz+=tr[id].laz;        tr[id*2+1].laz+=tr[id].laz;        tr[id*2].sum+=tr[id].laz*tr[id*2].len;        tr[id*2+1].sum+=tr[id].laz*tr[id*2+1].len;        tr[id].laz=0;  //当前结点laz标记置零,以免影响下次更新    }}void build(int id,int l,int r){    tr[id].l=l;    tr[id].r=r;    tr[id].laz=0;    tr[id].len=r-l+1;    if(l==r)    {        tr[id].sum=a[l];        return;    }    int mid=(l+r)/2;    build(id*2,l,mid);    build(id*2+1,mid+1,r);    pushup(id);}long long ans(int id,int l,int r){    if(l==tr[id].l&&r==tr[id].r)        return tr[id].sum;    pushdown(id);  //每次询问的时候就可以向下更新,因此不会存在使用还未更新的节点的值来求和    int mid=(tr[id].l+tr[id].r)/2;    if(mid>=r)        return ans(id*2,l,r);    else if(mid<l)        return ans(id*2+1,l,r);    else    {        long long x=ans(id*2,l,mid);        long long y=ans(id*2+1,mid+1,r);        return x+y;    }}void update(int id,int l,int r,int x){    if(tr[id].l==l&&tr[id].r==r)    {        tr[id].laz+=x;        tr[id].sum+=x*tr[id].len;        return;    }    pushdown(id);  //将函数置于此,询问节点是就可以向下更新,且访问的不会是叶子节点    int mid=(tr[id].l+tr[id].r)/2;    if(mid<l)        update(id*2+1,l,r,x);    else if(mid>=r)        update(id*2,l,r,x);    else    {        update(id*2,l,mid,x);        update(id*2+1,mid+1,r,x);    }    pushup(id);}int main(){    int n,q,x,y,z,i;    char m[2];    while(scanf("%d%d",&n,&q)!=EOF)    {        for(i=1;i<=n;i++)            scanf("%lld",&a[i]);        build(1,1,n);        while(q--)        {            scanf("%s",m);            if(m[0]=='C')            {                scanf("%d%d%d",&x,&y,&z);                update(1,x,y,z);            }            else if(m[0]=='Q')            {                scanf("%d%d",&x,&y);                printf("%lld\n",ans(1,x,y));            }        }    }    return 0;}
0 0
原创粉丝点击