树状数组的模板,修改单点的值,查询某个区间
1 #include<cstdio> 2 #include<cctype> 3 using namespace std; 4 5 int c[500010],n,m; 6 7 int read() 8 { 9 int x=0,f=1; 10 char c=getchar(); 11 while (!isdigit(c)) 12 f=c=='-'?-1:1,c=getchar(); 13 while (isdigit(c)) 14 x=(x<<1)+(x<<3)+(c^48),c=getchar(); 15 return x*f; 16 } 17 18 int lowbit(int x) 19 { 20 return x&-x; 21 } 22 23 int query(int x) 24 { 25 int ans=0; 26 for (;x;x-=lowbit(x)) 27 ans+=c[x]; 28 return ans; 29 } 30 31 int update(int x,int val) 32 { 33 for (;x<=n;x+=lowbit(x)) 34 c[x]+=val; 35 } 36 37 int main() 38 { 39 int i,j,t; 40 n=read(),m=read(); 41 for (i=1;i<=n;i++) 42 { 43 j=read(); 44 update(i,j); 45 } 46 while (m--) 47 { 48 t=read(); 49 i=read(); 50 j=read(); 51 if (t==1) 52 update(i,j); 53 else 54 printf("%d ",query(j)-query(i-1)); 55 } 56 return 0; 57 }