树状数组核心代码

int lowbit(int x)
{
	return x&-x;
}
int add(int pos,int a)
{

	while (pos<=n)
	{
		c[pos]+=a;
		pos+=lowbit(pos);
	}
	return 0;
}
int sum(int pos)
{

	int ans=0;
	while (pos>0)
	{
		ans+=c[pos];
		pos-=lowbit(pos);
	}
	return ans;
}

posted @ 2016-03-13 11:19  Code-dream  阅读(150)  评论(0编辑  收藏  举报