感觉没有太大问题,自己也造了几组范围小一点的数据,样例过了,但是0分,求WA。。。
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m;
int a[10000009];
int tree[10000009];
int lazy_tag[10000009];
void build(int l,int r,int p){
if(l==r){
tree[p]=a[l];
return;
}
int mid=(l+r)/2;
build(l,mid,2*p);
build(mid+1,r,2*p+1);
tree[p]=tree[p*2]+tree[p*2+1];
}
int getsum(int l,int r,int s,int t,int p){
if(l<=s&&t<=r) return tree[p];
int mid=(s+t)/2;
if(lazy_tag[p]){
tree[p*2]+=lazy_tag[p]*(mid-l+1);
tree[p*2+1]+=lazy_tag[p]*(r-mid);
lazy_tag[2*p]+=lazy_tag[p];
lazy_tag[2*p+1]+=lazy_tag[p];
}
lazy_tag[p]=0;
int sum=0;
if(l<=mid) sum+=getsum(l,r,s,mid,2*p);
if(r>mid) sum+=getsum(l,r,mid+1,t,2*p+1);
return sum;
}
void update(int l,int r,int s,int t,int k,int p){
if(l<=s&&t<=r){
tree[p]+=(t-s+1)*k;
lazy_tag[p]+=k;
return;
}
int mid=(s+t)/2;
if(lazy_tag[p]){
tree[2*p]+=lazy_tag[p]*(mid-s+1);
tree[2*p+1]+=lazy_tag[p]*(t-mid);
lazy_tag[2*p]+=lazy_tag[p];
lazy_tag[2*p+1]+=lazy_tag[p];
}
lazy_tag[p]=0;
if(l<=mid) update(l,r,s,mid,k,2*p);
if(r>mid) update(l,r,mid+1,t,k,2*p+1);
tree[p]=tree[p*2]+tree[p*2+1];
}
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
build(1,n,1);
while(m--){
int op;
cin>>op;
if(op==1){
int x,y,k;
cin>>x>>y>>k;
update(x,y,1,n,k,1);
}
if(op==2){
int k;
cin>>k;
update(1,1,1,n,k,1);
}
if(op==3){
int k;
cin>>k;
update(1,1,1,n,-1*k,1);
}
if(op==4){
int l,r;
cin>>l>>r;
cout<<getsum(l,r,1,n,1)<<endl;
}
if(op==5){
cout<<getsum(1,1,1,n,1)<<endl;
}
}
}