二维树状数组模板

int n,c[N][N];
int lowbit(int x)
{
	return x&(-x);
}
int query_sum(int x, int y)
{
	int res = 0;
	for (int i = x; i > 0; i -= lowbit(i))
	{
		for (int j = y; j > 0; j -= lowbit(j))
		{
			res += c[i][j];
		}
	}
	return res;
}

void add(int x, int y, int val)
{
	for (int i = x; i <= n; i += lowbit(i))
	{
		for (int j = y; j <= n; j += lowbit(j))
		{
			c[i][j] += val;
		}
	}
}

你可能感兴趣的:(二维树状数组模板)