BZOJ 1452
可以每一种颜色建一个二维树状数组,数据$O(n^2c)$的空间复杂度可以接受
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ms(i,j) memset(i, j, sizeof i);
using namespace std;
int n,m,Q;
int a[300 + 5][300 + 5];
int lowbit(int x)
{
return x&(-x);
}
struct bits
{
int a[300 + 5][300 + 5];
void init()
{
ms(a, 0);
}
void add(int x, int y, int c)
{
for (int i=x;i<=n;i+=lowbit(i))
for (int j=y;j<=n;j+=lowbit(j))
{
a[i][j] += c;
}
}
int sum(int x, int y)
{
int ret = 0;
for (int i=x;i>0;i-=lowbit(i))
for (int j=y;j>0;j-=lowbit(j))
{
ret += a[i][j];
}
return ret;
}
int query(int x1, int y1, int x2, int y2)
{
return sum(x2, y2)+sum(x1-1, y1-1)-sum(x1-1, y2)-sum(x2, y1-1);
}
}b[100 + 5];
int main()
{
// freopen("bzoj1452.in", "r", stdin); freopen("bzoj1452.out", "w", stdout);
for (int i=1;i<=102;i++) b[i].init();
scanf("%d%d", &n, &m);
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
scanf("%d", &a[i][j]);
b[a[i][j]].add(i,j,1);
}
scanf("%d", &Q);
for (int i=1;i<=Q;i++)
{
int opt;
scanf("%d", &opt);
if (opt==1)
{
int x, y, c;
scanf("%d%d%d", &x, &y, &c);
b[a[x][y]].add(x,y,-1);
b[a[x][y]=c].add(x,y,1);
} else
{
int x1, x2, y1, y2, c;
scanf("%d%d%d%d%d", &x1, &x2, &y1, &y2, &c);
printf("%d\n", b[c].query(x1,y1,x2,y2));
}
}
return 0;
}