BaiBaiShaFeng @ 2024-11-17 15:15:34
#include <bits/stdc++.h>
using namespace std;
const int MN=1e5+10;
struct node{
int l, r;
long long dat;
long long tag;
}sak[4*MN];
int a[MN];
void pushup(const int x){
sak[x].dat=sak[x*2].dat+sak[x*2+1].dat;
}
void build(int x, int l, int r){
if(l==r){
sak[x].dat=a[l];
return;
}
int mid=(l+r)/2;
build(x*2, l, mid);
build(x*2+1,mid+1,r);
pushup(x);
}
void maketag(int pos, int len, long long x){
sak[pos].tag+=x;
sak[pos].dat+=len*x;
}
void pushdown(int u, int l, int r){
int mid=(l+r)/2;
maketag(u*2, mid-l+1, sak[u].tag);
maketag(u*2+1, r-mid, sak[u].tag);
sak[u].tag=0;
}
bool ou(int l, int r, int L, int R){
return (L>r)||(R<l);
}
bool in(int l, int r, int L, int R){
return (l<=L) & (R<=r);
}
long long search(int pos, int L, int R, int l, int r){
if(in(l, r, L, R)){
return sak[pos].dat;
}else if(!ou(l, r, L, R)){
int mid = (L+R)/2;
pushdown(pos, L, R);
int xx = 0;
if(L<=mid) xx+= search(pos*2, L, mid, l, r);
if(R>=mid) xx+= search(pos*2+1, mid+1, R, l, r);
return xx;
//return search(pos*2, L, mid, l, r) + search(pos*2+1, mid+1, R, l, r);
}
}
void update(int pos, int L, int R, int l, int r, long long x){
if(in(l, r, L, R)){
maketag(pos, R-L+1, x);
}else if(!ou(l, r, L, R)){
int mid = (L+R)/2;
pushdown(pos, L, R);
update(pos*2, L, mid, l, r, x);
update(pos*2+1, mid+1, R, l, r, x);
pushup(pos);
}
}
int main(){
int n, m;
scanf("%d %d", &n, &m);
for(int i=1; i<=n; i++){
scanf("%d", &a[i]);
}
build(1,1,n);
for(int t=1; t<=m; t++){
int op, x, y;
long long k;
scanf("%d", &op);
if(op==1){
scanf("%d%d%lld", &x, &y, &k);
update(1, 1, n, x, y, k);
}else{
scanf("%d%d", &x, &y);
cout<<search(1,1,n,x,y)<<endl;
}
}
return 0;
}
by Little_Fox_Fairy @ 2024-11-17 15:32:18
search 函数里面:
if(L<=mid) xx+= search(pos*2, L, mid, l, r);
if(R>=mid) xx+= search(pos*2+1, mid+1, R, l, r);
这里应该是
if(l<=mid) xx+= search(pos*2, L, mid, l, r);
if(r>=mid) xx+= search(pos*2+1, mid+1, R, l, r);
还有下面 update 函数里面 :
update(pos*2, L, mid, l, r, x);
update(pos*2+1, mid+1, R, l, r, x);
也要加上
if (l<=mid) update(pos*2, L, mid, l, r, x);
if (r>mid)update(pos*2+1, mid+1, R, l, r, x);
by Little_Fox_Fairy @ 2024-11-17 15:32:45
@baibaishafeng123
by Little_Fox_Fairy @ 2024-11-17 15:34:20
然后 search 函数里面的 xx 也要开 long long 。
by Little_Fox_Fairy @ 2024-11-17 15:34:39
@baibaishafeng123
by BaiBaiShaFeng @ 2024-11-17 15:40:21
ok谢谢大佬