题目:
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.
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.
You need to answer all Q commands in order. One answer in a line.
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 4Sample Output
4 55 9 15Hint
思路:
[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