DemonPlayer @ 2025-01-09 15:59:09
#include<bits/stdc++.h>
#define int long long
#define lson(p) p<<1
#define rson(p) p<<1|1
using namespace std;
const int maxn=100005;
int n,m,opt,l,r,k;
int a[maxn];
struct node{
int l;
int r;
int val;
int lzy;
};
node tree[maxn<<2];
void buid(int p,int l,int r){
tree[p].l=l;
tree[p].r=r;
if(l==r){
tree[p].val=a[l];
return;
}
int mid;
mid=(tree[p].l+tree[p].r)/2;
buid(lson(p),l,mid);
buid(rson(p),mid+1,r);
tree[p].val=tree[lson(p)].val+tree[rson(p)].val;
return;
}
void pushDown(int p){
int ls=lson(p);
int rs=rson(p);
if(tree[p].lzy){
tree[ls].val+=tree[p].lzy*(tree[ls].r-tree[ls].l+1);
tree[rs].val+=tree[p].lzy*(tree[rs].r-tree[rs].l+1);
tree[ls].lzy+=tree[p].lzy;
tree[ls].lzy+=tree[p].lzy;
tree[p].lzy=0;
}
return;
}
void upd(int p,int l,int r,int v){
if(l<=tree[p].l&&r>=tree[p].r){
tree[p].val+=v*(tree[p].r-tree[p].l+1);
tree[p].lzy+=v;
return;
}
pushDown(p);
int mid=(tree[p].r+tree[p].l)/2;
int ls=lson(p);
int rs=rson(p);
if(l<=mid){
upd(ls,l,r,v);
}
if(r>mid){
upd(rs,l,r,v);
}
tree[p].val=tree[ls].val+tree[rs].val;
return;
}
int qusum(int p,int l,int r){
if(l<=tree[p].l&&r>=tree[p].r){
return tree[p].val;
}
pushDown(p);
int sum=0;
int mid=(tree[p].l+tree[p].r)/2;
if(l<=mid){
sum+=qusum(lson(p),l,r);
}else if(r>mid){
sum+=qusum(rson(p),l,r);
}
return sum;
}
signed main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
buid(1,1,n);
while(m--){
scanf("%d%d%d",&opt,&l,&r);
if(opt==1){
scanf("%lld",&k);
upd(1,l,r,k);
}else{
cout<<qusum(1,l,r)<<'\n';
}
}
return 0;
}
by guoxinchen @ 2025-01-09 16:14:00
@DemonPlayer qusum里的else if改成if
by lyh0217 @ 2025-01-09 16:14:30
@DemonPlayer
两个小bug
1.将pushDown函数中的
tree[ls].lzy+=tree[p].lzy;
tree[ls].lzy+=tree[p].lzy;
改为
tree[ls].lzy+=tree[p].lzy;
tree[rs].lzy+=tree[p].lzy;
2.将qusum中的
if(l<=mid){
sum+=qusum(lson(p),l,r);
}else if(r>mid){
sum+=qusum(rson(p),l,r);
}
改为
if(l<=mid){
sum+=qusum(lson(p),l,r);
}if(r>mid){
sum+=qusum(rson(p),l,r);
}
by masonxiong @ 2025-01-09 16:19:58
@DemonPlayer
tree[rs].lzy+=tree[p].lzy;
。你似乎把 rs
错写成了 ls
。else
删去。你凭什么认为查询的区间只在一个儿子里面。%d
改为 %lld
,你都 #define int long long
了怎么还用 %d
。by WYX1210 @ 2025-01-09 16:26:40
@DemonPlayer\ 1.去掉77行的else if\ 2.#define int long long 就不要用%d\ 3.42行ls改为rs
by DemonPlayer @ 2025-01-10 16:21:53
@guoxinchen@lyh0217@masonxiong@WYX1210 谢谢各位,已关(使用小号)。