下面是树状数组的三个常用函数:
//c++代码
inline int lowbit(int x)
{
return x&(-x);
}
void updata(int x,int y)
{
while(x<=n)//n是数组元素个数
{
a[x]+=y;
x+=lowbit(x);
}
}
int sum(int k)//求a[1]+a[2]+.......+a[k]
{
int res=0;
while(k)
{
a[k]+=res;
k-=lowbit(k);
}
return res;
}
![委屈](http://static.blog.csdn.net/xheditor/xheditor_emot/default/wronged.gif)