单点修改,区间求和模板
1 /* 2 洛谷 3374 3 树状数组 模板 4 */ 5 6 #include <bits/stdc++.h> 7 8 #define MAX 500010 9 using namespace std; 10 int c[MAX]; 11 int arr[MAX]; 12 int n,m; 13 int lowbit(int x) { return x&(-x); 14 } 15 int sum (int x) 16 { 17 int ans = 0; 18 for (int i=x;i>0;i-=lowbit(i)) 19 ans +=c[i]; 20 return ans; 21 } 22 void update(int x,int val) 23 { 24 for (int i=x;i<=n;i+=lowbit(i)) 25 c[i]+=val; 26 } 27 void init() 28 { 29 for (int i=1;i<=n;++i) 30 { 31 c[i] = 0; 32 for (int j = i-lowbit(i)+1;j<=i;++j) 33 c[i] +=arr[j]; 34 } 35 } 36 37 int main () 38 { 39 int a,b,d; 40 // memset(c,0,sizeof(c)); 41 cin >>n>>m; 42 for (int i=1;i<=n;++i) 43 scanf("%d",&arr[i]); 44 init(); 45 for(int i=0;i<m;++i) 46 { 47 scanf("%d%d%d",&a,&b,&d); 48 if (1==a) 49 update(b,d); 50 else 51 printf ("%d ",sum(d)-sum(b-1)) ; 52 } 53 return 0; 54 }