Bzoj 1452(二维树状数组)

BZOJ传送门
可以每一种颜色建一个二维树状数组,数据$O(n^2c)$的空间复杂度可以接受

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#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;
}

------ 本文结束 ------