happy_zero @ 2023-02-01 12:10:23
题目中说的是 RE
,开到 AC
了?
代码如下:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+5;
int n,m;
int a[N],t[N<<2];
int lazy[N];
void pushup(int x){
t[x]=t[x<<1]+t[x<<1|1];
}
void f(int k,int v,int l,int r){
lazy[k]+=v;
t[k]+=(r-l+1)*v;
}
void pushdown(int k,int l,int r){
int m=l+((r-l)>>1);
f(k<<1,lazy[k],l,m);
f(k<<1|1,lazy[k],m+1,r);
lazy[k]=0;
}
void build(int k,int l,int r){
if(l==r)t[k]=a[l];
else{
int m=l+((r-l)>>1);
build(k<<1,l,m);
build(k<<1|1,m+1,r);
pushup(k);
}
}
void updata(int v,int k,int l,int r,int L,int R){
if(l>=L&&r<=R)f(k,v,l,r);
else{
pushdown(k,l,r);
int m=l+((r-l)>>1);
if(L<=m)updata(v,k<<1,l,m,L,R);
if(R>m)updata(v,k<<1|1,m+1,r,L,R);
pushup(k);
}
}
int query(int k,int l,int r,int L,int R){
if(l>=L&&r<=R)return t[k];
else{
int res=0;
pushdown(k,l,r);
int m=l+((r-l)>>1);
if(L<=m)res+=query(k<<1,l,m,L,R);
if(R>m)res+=query(k<<1|1,m+1,r,L,R);
return res;
}
}
signed main(){
std::ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>a[i];
build(1,1,n);
while(m--){
int op,l,r,x;
cin>>op>>l>>r;
if(op==1){
cin>>x;
updata(x,1,1,n,l,r);
}
else cout<<query(1,1,n,l,r)<<endl;
}
return 0;
}
by happy_zero @ 2023-02-01 12:18:48
@xiehuiying
thx. 大佬nb!
by xuweiyi @ 2023-07-06 14:17:02
@happy_zero 同问,所以能不能解答下/kk
by happy_zero @ 2023-07-06 15:39:00
@xuweiyi lazy 数组也要开四倍空间
by xuweiyi @ 2023-07-06 15:40:05
@happy_zero 刚解决力,thx
by xuweiyi @ 2023-07-06 15:40:56
@happy_zero 我不会告诉你是因为我在宏定义中写了加法运算