题解
2026-05-17 20:05:47
发布于:上海
5阅读
0回复
0点赞
就基础的线段树区间修改
#include<iostream>
using namespace std;
typedef long long ll;
const int N=1e6+5;
ll n,m,a[N];
struct node{
ll l,r,v,lg;
}t[4*N];//线段树
void build(ll p,ll l,ll r){//建树
t[p].l=l,t[p].r=r;
if(l==r){
t[p].v=a[l];
return ;
}
ll mid=(l+r)>>1;
build(p*2,l,mid);
build(p*2+1,mid+1,r);
t[p].v=t[p*2].v+t[p*2+1].v;
}
void lan(ll p){
if(t[p].lg){
t[p*2].v+=(t[p*2].r-t[p*2].l+1)*t[p].lg;
t[p*2+1].v+=(t[p*2+1].r-t[p*2+1].l+1)*t[p].lg;
t[p*2].lg+=t[p].lg;
t[p*2+1].lg+=t[p].lg;
t[p].lg=0;
}
}
void update(ll p,ll l,ll r,ll z){
if(l<=t[p].l && r>=t[p].r){
t[p].v+=(t[p].r-t[p].l+1)*z;
t[p].lg+=z;
return ;
}
lan(p);
ll mid=(t[p].l+t[p].r)>>1;
if(l<=mid)update(p*2,l,r,z);
if(r>mid)update(p*2+1,l,r,z);
t[p].v=t[p*2].v+t[p*2+1].v;
}
ll f(ll p,ll x,ll y){
if(x<=t[p].l && y>=t[p].r)
return t[p].v;
lan(p);
ll mid=(t[p].l+t[p].r)>>1;
ll ans=0;
if(x<=mid)ans+=f(p*2,x,y);
if(y>mid)ans+=f(p*2+1,x,y);
return ans;
}
int main(){
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
build(1,1,n);
while(m--){
int opt,x,y;
scanf("%d%d%d",&opt,&x,&y);
if(opt==1){
int k;
scanf("%d",&k);
update(1,x,y,k);
}
else{
cout<<f(1,x,y)<<"\n";
}
}
return 0;
}
这里空空如也




有帮助,赞一个