SN_Tashkent @ 2023-05-23 19:07:03
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+15;
struct Tree{
int l,r;
ll sum,add;
}tr[4*N];
int n,m,a[N];
void build(int u,int l,int r)//建树
{
tr[u].l=l;
tr[u].r=r;
if(l==r)
tr[u].sum=a[l];
else
{
int mid=(l+r)/2;
build(u*2,l,mid);
build(u*2+1,mid+1,r);
tr[u].sum=tr[u*2].sum+tr[u*2+1].sum;
}
}
void pushdown(int u)//更新懒标记
{
if(tr[u].add)
{
tr[u*2].sum+=tr[u].add*(tr[u*2].r-tr[u*2].l+1);
tr[u*2+1].sum+=tr[u].add*(tr[u*2+1].r-tr[u*2+1].l+1);
tr[u*2].add+=tr[u].add;
tr[u*2+1].add+=tr[u].add;
tr[u].add=0;
}
}
void update(int u,int x,int y,ll k)
{
int l=tr[u].l,r=tr[u].r,mid=(l+r)/2;
if(x<=l&&y>=r)
{
tr[u].sum=(ll)(r-l+1)*k;
tr[u].add+=k;//懒标记,子节点还未更新,在下次查询以及区间修改时更新
}
else
{
pushdown(u);
if(x<=mid)
update(u*2,x,y,k);
if(y>mid)
update(u*2+1,x,y,k);
tr[u].sum=tr[u*2].sum+tr[u*2+1].sum;
}
}
ll query(int u,int x,int y)//查询
{
int l=tr[u].l,r=tr[u].r,mid=(l+r)/2;
if(x<=l&&y>=r)
return tr[u].sum;
pushdown(u);
ll ans=0;
if(x<=mid)
ans+=query(u*2,x,y);
if(y>mid)
ans+=query(u*2+1,x,y);
return ans;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>a[i];
build(1,1,n);
while(m--)
{
int num,x,y,k;
cin>>num;
if(num==1)
{
cin>>x>>y>>k;
update(1,x,y,k);
}
else
{
cin>>x>>y;
cout<<query(1,x,y)<<endl;
}
}
return 0;
}
by SN_Tashkent @ 2023-05-23 19:09:29
by lg10 @ 2023-05-23 19:12:37
区间更新时对于完全包含的区间,“sum+=”少打了加号
by Fze_8 @ 2023-05-23 19:16:57
tr[u].sum=(ll)(r-l+1)*k;
这句话改成 tr[u].sum+=(ll)(r-l+1)*k;
,因为是相加不是赋值
by SN_Tashkent @ 2023-05-23 19:17:05
@xjy2333
啊?好吧我脑子让门挤了属于是,谢谢您嘞,过喽过喽
by SN_Tashkent @ 2023-05-23 19:17:48
此贴结,感谢诸位