树状数组

 1 int lowbit(int x)
 2 {
 3     return x & (-x);
 4 }
 5 
 6 int sum(int x)
 7 {
 8     int ret = 0;
 9     while(x > 0)
10     {
11         ret += tmp[x]; x -= lowbit(x);
12     }
13     return ret; 
14 } 
15 
16 void add(int x, int d)
17 {
18      while(x <= n)
19     {
20          tmp[x] += d; x += lowbit(x);
21     }
22 }
23 void init()
24 {
25    for(int i = 1; i <= n; ++i)
26       add(i,a[i]);
27 }

 

posted @ 2016-05-28 14:38  PosProteus  阅读(135)  评论(0编辑  收藏  举报