POJ3468 线段树入门-区间修改

本文介绍了一种解决区间加法与查询问题的有效方法。通过使用标记下推和懒惰传播的技术,实现在给定区间内批量修改数值,并能够快速查询指定区间内数值之和的功能。

题目:

You have N integers, A1A2, ... , 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 A1A2, ... , 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.

思路:

[L,R]+=C

区间修改和点修改相比,有好几个地方不一样。首先要加上下推标记,这里是一个区间内的数都加C,做法是将当前要加的区间标记,下推到子区间,在下推的过程中,根据左右子节点的个数,分别进行累加,下推结束后,将当前节点的下推标记删除。

void PushDown(int rt,int ln,int rn)
{
    //ln,rn为左子树,右子树的数字数量。
    if(Add[rt]){
        //下推标记
        Add[rt<<1]+=Add[rt];
        Add[rt<<1|1]+=Add[rt];
        //修改子节点的Sum使之与对应的Add相对应
        sum[rt<<1]+=Add[rt]*ln;
        sum[rt<<1|1]+=Add[rt]*rn;
        //清除本节点标记
        Add[rt]=0;
    }
}

注意Query修改的地方:

ll Query(int L,int R,int l ,int r,int rt)
{
    //L,R是查询区间,不会改变
    if(L<=l&&R>=r)
        return sum[rt];
   int  mid=(l+r)/2;
   ll ANS1=0;
    PushDown(rt,mid-l+1,r-mid);////////////////////////
    //因为一开始[L,R]肯定是在[l,r]里边,
    //所以不会出现[L,R]跑到[l,r]外边的情况
    if(L<=mid)  //和左子树区间有交集
        ANS1+=Query(L,R,l,mid,rt<<1);
    if(R>mid)
        ANS1+=Query(L,R,mid+1,r,rt<<1|1);
    return ANS1;

}

在点修改中,Update标红的地方是else,为什么呢?因为点修改,它不是在左子树,就是在右子树,而区间的话可能两边都有。
void Update(int L,int R,int C,int l,int r,int rt)
{

    if(L <= l && r <= R){//如果本区间完全在操作区间[L,R]以内、、、、、、、、、、、、、、、、、、
        sum[rt]+=C*(r-l+1);//更新数字和,向上保持正确
        Add[rt]+=C;//增加Add标记,表示本区间的Sum正确,子区间的Sum仍需要根据Add的值来调整
        return ;
    }
    int mid=(l+r)/2;
     PushDown(rt,mid-l+1,r-mid);//下推标记、、、、、、、、、、、、、、、、、、、、、、
    if(L<=mid)
        Update(L,R,C,l,mid,rt<<1);
     if(R>mid)   //和点修改不一样的地方、、、、、、、、、、、、、、、、、、、、、、、、、、、
        Update(L,R,C,mid+1,r,rt<<1|1);
    PushUp(rt);//更新本节点
}

#include <iostream>
#include<stdio.h>
using namespace std;
#define maxn 100008
typedef long long ll;
int A[maxn];
ll sum[maxn*4],Add[maxn*4];
void PushUp(int rt)
{  //这里是求和,rt表示当前节点的实际存储位置,下标
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
    //自顶向下
    if(l==r)
    {sum[rt]=ll(A[l]);
   // cout<<"rt:"<<rt<<" "<<sum[rt]<<" ";
    return;
    }
    int mid=(l+r)/2;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    PushUp(rt);//左右子树建好后,更新本节点信息
}
void PushDown(int rt,int ln,int rn)
{
    //ln,rn为左子树,右子树的数字数量。
    if(Add[rt]){
        //下推标记
        Add[rt<<1]+=Add[rt];
        Add[rt<<1|1]+=Add[rt];
        //修改子节点的Sum使之与对应的Add相对应
        sum[rt<<1]+=Add[rt]*ln;
        sum[rt<<1|1]+=Add[rt]*rn;
        //清除本节点标记
        Add[rt]=0;
    }
}
ll Query(int L,int R,int l ,int r,int rt)
{
    //L,R是查询区间,不会改变
    if(L<=l&&R>=r)
        return sum[rt];
   int  mid=(l+r)/2;
   ll ANS1=0;
    PushDown(rt,mid-l+1,r-mid);
    //因为一开始[L,R]肯定是在[l,r]里边,
    //所以不会出现[L,R]跑到[l,r]外边的情况
    if(L<=mid)  //和左子树区间有交集
        ANS1+=Query(L,R,l,mid,rt<<1);
    if(R>mid)
        ANS1+=Query(L,R,mid+1,r,rt<<1|1);
    return ANS1;

}

void Update(int L,int R,int C,int l,int r,int rt)
{

    if(L <= l && r <= R){//如果本区间完全在操作区间[L,R]以内
        sum[rt]+=C*(r-l+1);//更新数字和,向上保持正确
        Add[rt]+=C;//增加Add标记,表示本区间的Sum正确,子区间的Sum仍需要根据Add的值来调整
        return ;
    }
    int mid=(l+r)/2;
     PushDown(rt,mid-l+1,r-mid);//下推标记
    if(L<=mid)
        Update(L,R,C,l,mid,rt<<1);
     if(R>mid)   //和点修改不一样的地方
        Update(L,R,C,mid+1,r,rt<<1|1);
    PushUp(rt);//更新本节点
}
int main()
{
   // int r=1<<2+1,t=1<<2|1;//+优先级高
   // cout<<r<<t<<endl;
   int N,Q;
   scanf("%d%d",&N,&Q);
   for(int i=1;i<=N;i++)
    scanf("%d",&A[i]);
   build(1,N,1);

   char C;
   int a,b,d;
   while(Q--)
   {
        getchar();//注意字符和整型切换
       scanf("%c%d%d",&C,&a,&b);
       if(C=='Q')
       {
           ll ans=Query(a,b,1,N,1);
           printf("%lld\n",ans);

       }
       if(C=='C')
       {
           scanf("%d",&d);
           Update(a,b,d,1,N,1);
       }

   }
    return 0;
}
参考:https://blog.youkuaiyun.com/zearot/article/details/48299459

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值