那写线段树的模板啊 写这干嘛
by lhz2022 @ 2024-10-05 23:03:45
```cpp
#include <bits/stdc++.h>
using namespace std;
struct hns{
int l,r,n;
}d[500010*4];
int ans,n,m,a[500010];
void build(int l,int r,int p){
if(l==r){
d[p].l=l,d[p].r=r,d[p].n=a[r];
return;
}
// int m=r+((r-l)>>1);
int m=l+r>>1;
build(l,m,p*2);
build(m+1,r,p*2+1);
d[p].n=d[p*2].n+d[p*2+1].n,d[p].l=l,d[p].r=r;
return;
}
void check(int l,int r,int p){
if((d[p].l>=l && d[p].r<=r)||(d[p].l==d[p].r)){
ans+=d[p].n;
return;
}
if(d[p*2].r>=l) check(l,r,p*2);
if(d[p*2+1].l<=r) check(l,r,p*2+1);
return;
}
void pls(int p,int x,int k){
d[p].n+=k;
if(d[p].l==d[p].r) return;
if(x<=d[p*2].r) pls(p*2,x,k);
if(x>=d[p*2+1].l) pls(p*2+1,x,k);
return;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n>>m;
for(int i=1; i<=n; i++){
cin>>a[i];
}
build(1,n,1);
for(int i=1; i<=m; i++){
int a;
cin>>a;
if(a==1){
int b,c;
cin>>b>>c;
pls(1,b,c);
}else{
int b,c;
cin>>b>>c;
ans=0;
check(b,c,1);
cout<<ans<<"\n";
}
}
return 0;
}
```
这样就过了
by jade1695 @ 2024-10-06 10:53:34
@[jade1695](/user/177282) 感谢,已关,麻烦解释一下问题可以吗
by xuyunlong120820 @ 2024-10-08 17:36:42
check里lrp位置是反的
int m=r+((r-l)>>1);
写成
int m=l+((r-l)>>1);或
int m=l+r>>1;
就行
by jade1695 @ 2024-10-08 18:26:12
@[jade1695](/user/177282) 谢谢
by xuyunlong120820 @ 2024-10-09 10:04:22