Wunsch @ 2023-01-26 18:14:03
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#define ll long long
#define LS o<<1
#define RS LS|1
using namespace std;
ll n,m,t[500001],a[500001],laz[500001],sum,x,y,k,q;
void build(ll o,ll l,ll r){
if(l==r){cin>>t[o];return;}
ll mid=l+r>>1;
build(LS,l,mid);
build(RS,mid+1,r);
t[o]=t[LS]+t[RS];
}
void down(ll o,ll l,ll r){
if(l==r||!laz[o])return;
ll mid=l+r>>1;
laz[LS]+=laz[o];
laz[RS]+=laz[o];
t[LS]+=laz[o]*(mid-l+1);
t[RS]+=laz[o]*(r-mid);
laz[o]=0;
}
void change(ll o,ll l,ll r,ll x,ll y,ll sum){
if(x<=l&&r<=y){laz[o]+=sum;t[o]+=(r-l+1)*sum;return;}
down(o,l,r);
ll mid=l+r>>1;
if(x<=mid)change(LS,l,mid,x,y,sum);
if(y>mid)change(RS,mid+1,r,x,y,sum);
t[o]=t[LS]+t[RS];
}
ll qury(ll o,ll l,ll r,ll x,ll y){
if(x<=l&&r<=y)return t[o];
down(o,l,r);
ll ret=0;
ll mid=l+r>>1;
if(x<=mid)ret+=qury(LS,l,mid,x,y);
if(y>mid)ret+=qury(RS,mid+1,r,x,y);
return ret;
}
int main(){
cin>>n>>m;
build(1,1,n);
for(int i=1;i<=m;i++){
cin>>q;
if(q==1){
cin>>x>>y>>k;change(1,1,n,x,y,k);
}
else{
cin>>x>>y;cout<<qury(1,1,n,x,y);
}
}
return 0;
}
by Eleveslaine @ 2023-01-26 18:17:43
头一次见写线段树不用结构体的。
您不存每个区间的
by Coffins @ 2023-01-26 18:20:39
@Franz_Liszt 不用结构体完全可以啊,我就没用过结构体
by Acoipp @ 2023-01-26 18:21:21
换行呢?
by Eleveslaine @ 2023-01-26 18:21:49
@Coffins 好吧,但是个人认为结构体稍微好写点
by Acoipp @ 2023-01-26 18:21:50
cout<<qury(1,1,n,x,y)<<endl;
by hello_world_djh @ 2023-01-26 18:22:52
@Wunsch 忘了换行了
by Coffins @ 2023-01-26 18:23:35
@Wunsch 差个换行就能A了
by Xy_top @ 2023-01-26 18:38:02
@Wunsch 不建议用 endl 换行,非常非常非常慢,可以用 "\n" 换行,快很多
by Wunsch @ 2023-01-26 18:44:26
@hello_world_djh (好吧我是傻子 不过谢谢
by Wunsch @ 2023-01-26 18:44:56
@Coffins okok谢谢