1 树状数组
1.单点(+x),区间求和
理解
代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
//#define lowbit(i) i&(-i)
using namespace std;
int n,m;
int p[500005],a[500005];
int lowbit(int i){
return i&(-i);
}
void changex(int num,int h){
for(int i=num;i<=n;i+=lowbit(i)){
a[i]+=h;
}
return ;
}
int findx(int x){
int sum=0;
for(int i=x;i;i-=lowbit(i)){
sum+=a[i];
}
return sum;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&p[i]);
}
for(int i=1;i<=n;i++){
changex(i,p[i]);
}
while(m--){
int b=0,x=0,y=0;
scanf("%d%d%d",&b,&x,&y);
if(b==1){
changex(x,y);
}
if(b==2){
printf("%d
",findx(y)-findx(x-1));
}
}
return 0;
}