Infinity_Fantasy @ 2023-08-21 18:23:44
本蒟蒻丑陋的代码
#include<bits/stdc++.h>
using namespace std;
long long n,m,x,y,z,k,q,d[270000],b[270000],a[100005];
void build(long long s,long long t,long long p){
if(s==t){
b[p]=a[s];
return;
}
long long m=s+((t-s)>>1);
build(s,m,p<<1);
build(m+1,t,(p<<1)|1);
d[p]=d[p<<1]+d[(p<<1)|1];
}
void update(long long l,long long r,long long c,long long s,long long t,long long p){
if(l<=s && t<=r){
d[p]+=(t-s+1)*c,b[p]+=c;
return;
}
long long m=s+((t-s)>>1);
if(b[p])
d[p<<1]+=b[p]*(m-s+1),d[(p<<1)|1]+=b[p]*(t-m),
b[p<<1]+=b[p],b[(p<<1)|1]+=b[p];
b[p]=0;
if(l<=m) update(l,r,c,s,m,p<<1);
if(r>m) update(l,r,c,m+1,t,(p<<1)|1);
d[p]=d[p<<1]+d[(p<<1)|1];
}
long long getsum(long long l,long long r,long long s,long long t,long long p){
if(l<=s && t<=r) return d[p];
long long m=s+((t-s)>>1);
if(b[p])
d[p<<1]+=b[p]*(m-s+1),d[(p<<1)|1]+=b[p]*(t-m),
b[p<<1]+=b[p],b[(p<<1)|1]+=b[p];
b[p]=0;
long long sum=0;
if(l<=m) sum=getsum(l,r,s,m,p<<1);
if(r>m) sum+=getsum(l,r,m+1,t,(p<<1)|1);
return sum;
}
int main(){
scanf("%lld%lld",&n,&q);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
build(1,n,1);
while(q--){
scanf("%lld%lld%lld",&x,&y,&z);
if(x==1) printf("%lld\n",getsum(y,z,1,n,1));
else scanf("%lld",&k),update(y,z,k,1,n,1);
}
return 0;
}
by Didncan_yu @ 2023-08-21 18:43:33
1
void build(long long s,long long t,long long p){
if(s==t){
d[p]=a[s];//b改d
return;
}
}
2
if(l<=m) sum+=getsum(l,r,s,m,p<<1);//=改+=
if(r>m) sum+=getsum(l,r,m+1,t,(p<<1)|1);
3
while(q--){
scanf("%lld%lld%lld",&x,&y,&z);
if(x==2){//1改为2
printf("%lld\n",getsum(y,z,1,n,1));
}
else{
scanf("%lld",&k),update(y,z,k,1,n,1);
}
}
这样应该就行了
by 2011qiqi @ 2023-08-21 18:47:17
@AK_CCF 6
by Infinity_Fantasy @ 2023-08-21 19:48:24
@yuziyang 感谢巨佬
by Infinity_Fantasy @ 2023-08-21 19:48:44
@yuziyang orz