一名苦逼的OIer,想成为ACMer

Iowa_Battleship

洛谷4054 [JSOI2009]计数问题

原题链接

二维树状数组模板题。
对每一种颜色开一棵二维树状数组统计即可。

#include<cstdio>
using namespace std;
const int N = 310;
const int M = 110;
int C[M][N][N], a[N][N], n, m;
inline int re()
{
	int x = 0;
	char c = getchar();
	bool p = 0;
	for (; c < '0' || c > '9'; c = getchar())
		p |= c == '-';
	for (; c >= '0' && c <= '9'; c = getchar())
		x = x * 10 + c - '0';
	return p ? -x : x;
}
inline int lowbit(int x)
{
	return x & -x;
}
void add(int co, int x, int y, int z)
{
	int i;
	for (; x <= n; x += lowbit(x))
		for (i = y; i <= m; i += lowbit(i))
			C[co][x][i] += z;
}
int ask(int co, int x, int y)
{
	int s = 0, i;
	for (; x; x -= lowbit(x))
		for (i = y; i; i -= lowbit(i))
			s += C[co][x][i];
	return s;
}
int main()
{
	int i, j, x, y, xx, yy, q, p, z;
	n = re();
	m = re();
	for (i = 1; i <= n; i++)
		for (j = 1; j <= m; j++)
		{
			a[i][j] = re();
			add(a[i][j], i, j, 1);
		}
	q = re();
	while (q--)
	{
		p = re();
		if (!(p ^ 1))
		{
			x = re();
			y = re();
			z = re();
			add(a[x][y], x, y, -1);
			add(a[x][y] = z, x, y, 1);
		}
		else
		{
			x = re();
			xx = re();
			y = re();
			yy = re();
			z = re();
			printf("%d\n", ask(z, xx, yy) - ask(z, xx, y - 1) - ask(z, x - 1, yy) + ask(z, x - 1, y - 1));
		}
	}
	return 0;
}

posted on 2018-09-24 14:47  Iowa_Battleship  阅读(115)  评论(0编辑  收藏  举报

导航