樹狀數組

#define lowbit(x) (x&(-x))

int const maxn=3333;
int tree[maxn],n;

void add(int p,int val)
{
	while(p<=n)
	{
		tree[p]+=val;
		p+=lowbit(p);
	}
}

int getsum(int p)
{
	int sum=0;
	while(p>0)
	{
		sum+=tree[p];
		p-=lowbit(p);
	}
	return sum;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章