首页 > 代码库 > 树状数组

树状数组

 1 int c[MAXN];
 2 int n;
 3 
 4 int lowbit(int x){
 5     return (x&(-x));
 6 }
 7 
 8 void updata(int x,int v){
 9     while(x<=n){//从小到大更新
10         c[i] += v;
11         x += lowbit(x);
12     }
13 }
14 
15 int getsum(int x){
16     int sum = 0;
17     while(x > 0){//从大到小求和
18         sum += c[x];
19         x -= lowbit(x);
20     }
21     return sum;
22 }

 

树状数组