poj3468 线段树成段更新水题

poj 3468 A Simple Problem with Integers

题目:

ou 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.

题意:

就是给一个比较长的序列和两种操作,Q时询问区间所有的数字之和,C是给区间上的所有数字都加上某一个数字。

思路:

看上去就是一道线段树的成段更新,说到成段更新就一定要会打lazy标记,lazy标记的作用就是,当update操作到的某一个节点p表示的区间[l,r]完全在操作范围[x,y]内时,就不用继续操作p的子节点,直接把tree[p]+=(r-l+1)*num,lazy[p]+=num;就想当于标记好这个区间内数字的都应该加上lazy[p]的数值,就可以返回了。
然后是考虑什么时候要把lazy标记下方,就是对于当update操作,或是query操作到一点p是,发现[l,r]并不完全被[x,y]包含,这时就要继续操作他们的子节点,在操作之前就应该下方lazy标记,然后把lazy[p]清空即可,记得update操作最后要更新tree[p]=tree[p<<1]+tree[p<<1|1];

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 101000
using namespace std;
typedef long long ll;
ll tree[N<<2];
ll lazy[N<<2];
ll n,m;
void build(ll p,ll l,ll r)
{
    if(l==r)
    {
        scanf("%I64d",&tree[p]);
        return;
    }
    ll mid=(l+r)>>1;
    build(p<<1,l,mid);
    build((p<<1)|1,mid+1,r);
    tree[p]=tree[p<<1]+tree[(p<<1)|1];
}
void pushdown(ll p,ll m)
{
    if(lazy[p])
    {
        lazy[p<<1]+=lazy[p];
        lazy[p<<1|1]+=lazy[p];
        tree[p<<1]+=lazy[p]*(m-(m>>1));
        tree[p<<1|1]+=lazy[p]*(m>>1);
        lazy[p]=0;
    }

}
void update(ll p,ll l,ll r,ll x,ll y,ll val)
{
    if(x<=l&&r<=y)
    {
        lazy[p]+=val;
        tree[p]+=val*(r-l+1);
        return ;
    }
    pushdown(p,r-l+1);
    ll mid=(l+r)>>1;
    if(x<=mid)
        update(p<<1,l,mid,x,y,val);
    if(y>=mid+1)
        update( (p<<1)|1,mid+1,r,x,y,val);
    tree[p]=tree[p<<1]+tree[p<<1|1];
}
ll query(ll p,ll l,ll r,ll x,ll y)
{
    if(x<=l&&r<=y)
        return tree[p];
    pushdown(p,r-l+1);
    ll mid=(l+r)>>1;
    ll tmp=0;
    if(x<=mid)
        tmp+=query(p<<1,l,mid,x,y);
    if(y>=1+mid)
        tmp+=query((p<<1)|1,mid+1,r,x,y);
    return tmp;
}
int main()
{
    scanf("%I64d%I64d",&n,&m);
    memset(tree,0,sizeof(tree));
    memset(lazy,0,sizeof(lazy));
    build(1,1,n);
    ll x,y,z;
    for(ll i=1;i<=m;i++)
    {
        char s[6];
        scanf("%s",s);
        if(s[0]=='Q')
        {
            scanf("%I64d%I64d",&x,&y);
            ll ans=query(1,1,n,x,y);
            printf("%I64d\n",ans);
        }
        else
        {
            scanf("%I64d%I64d%I64d",&x,&y,&z);
            update(1,1,n,x,y,z);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值