YRCTTT @ 2024-07-05 09:00:41
#include<bits/stdc++.h>
using namespace std;
int n,q,a[2000005];
struct segtree {
int lc,rc;
long long val,lz_tag;
int tlen;
bool in;
} s[2000005];
void build(int l,int r,int node) {
if(l>r) {
return ;
}
s[node].in=true;
if(l==r) {
s[node].val=a[l];
s[node].tlen=1;
return ;
}
int _left=s[node].lc;
int _right=s[node].rc;
int mid=(l+r)>>1;
build(l,mid,_left);
build(mid+1,r,_right);
s[node].tlen=s[_left].tlen+s[_right].tlen;
s[node].val=s[_left].val+s[_right].val;
}
void update(int L,int R,int l,int r,int val,int node) {
int _left=s[node].lc;
int _right=s[node].rc;
int mid=(L+R)>>1;
if(L>r||R<l||!s[node].in)return ;
if(s[node].lz_tag) {
s[_left].lz_tag+=s[node].lz_tag;
s[_left].val+=s[node].lz_tag*(mid-L+1);
s[_right].lz_tag+=s[node].lz_tag;
s[_right].val+=s[node].lz_tag*(R-mid);
s[node].lz_tag=0;
}
if(L>R)return;
if(l<=L&&R<=r) {
s[node].lz_tag+=val;
s[node].val+=s[node].lz_tag*(R-L+1);
return ;
}
update(L,mid,l,r,val,_left);
update(mid+1,R,l,r,val,_right);
s[node].val=s[_left].val+s[_right].val;//未传递
}
int query(int L,int R,int l,int r,int node) {
if(L>r||R<l)return 0;
if(l<=L&&R<=r) {
return s[node].val;
}
int mid=(L+R)>>1;
int _left=s[node].lc;
int _right=s[node].rc;
if(s[node].lz_tag!=0) {
s[_left].lz_tag+=s[node].lz_tag;
s[_left].val+=s[node].lz_tag*(mid-L+1);
s[_right].lz_tag+=s[node].lz_tag;
s[_right].val+=s[node].lz_tag*(R-mid);
s[node].lz_tag=0;
}
int lans=0,rans=0;
lans=query(L,mid,l,r,_left);
rans=query(mid+1,R,l,r,_right);
return lans+rans;
}
int main() {
cin>>n>>q;
for(int i=1; i<=n; i++) {
cin>>a[i];
s[i].lc=i<<1;
s[i].rc=i<<1|1;
s[s[i].lc].in=false;
s[s[i].rc].in=false;
s[i].in=false;
}
build(1,n,1);
for(int i=1; i<=q; i++) {
int t,x,y,k;
cin>>t;
if(t==1) {
cin>>x>>y>>k;
update(1,n,x,y,k,1);
} else {
cin>>x>>y;
cout<<query(1,n,x,y,1)<<endl;
}
}
}
by gavinliu266 @ 2024-07-05 09:39:43
update 的 val 要开 long long。
by gavinliu266 @ 2024-07-05 09:46:18
@YRCTTT
by YRCTTT @ 2024-07-05 09:51:20
@gavinliu266 已关注,谢谢
by YRCTTT @ 2024-07-05 09:53:39
30pts还是不变,救救孩子吧
by gavinliu266 @ 2024-07-05 12:03:28
a 数组也开 long long。
by gavinliu266 @ 2024-07-05 12:06:40
@YRCTTT
by YRCTTT @ 2024-07-05 13:53:52
30pts的悲剧能看一下update与query有什么问题吗,感激不尽。
by YRCTTT @ 2024-07-05 20:12:50
此贴已结,Update函数出问题