BZOJ 1452「JSOI2009」Count

Description

Description

Input

Input

Output

Output

Sample Input

3 3
1 2 3
3 2 1
2 1 3
3
2 1 2 1 2 1
1 2 3 2
2 2 3 2 3 2

Sample Output

1
2

HINT

HINT

Solution

二维树状数组模板题

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
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
#include<stack>
#include<map>
using namespace std;
#define R register
#define ll long long
#define fo(i,a,b) for(R int (i)=(a);(i)<=(b);++(i))
#define dbg(x) cout<<#x<<"="<<x<<'\n'
#define mmt(x,y) memset(x,y,sizeof(x))
int n,m,q,ma[310][310];
int a[110][310][310];
inline int lowbit(int &x){return x&(-x);}
void add(int ty,int x,int y,int z)
{
for(int i=x;i<=n;i+=lowbit(i))
for(int j=y;j<=m;j+=lowbit(j))
a[ty][i][j]+=z;
}
int query(int ty,int x,int y)
{
R int ans=0;
for(int i=x;i;i-=lowbit(i))
for(int j=y;j;j-=lowbit(j))
ans+=a[ty][i][j];
return ans;
}
int main()
{
scanf("%d%d",&n,&m);
fo(i,1,n)
fo(j,1,m)
{
R int x;
scanf("%d",&x);ma[i][j]=x;
add(x,i,j,1);
}
scanf("%d",&q);
while(q--)
{
R int odd;
scanf("%d",&odd);
if(odd==1)
{
R int x,y,c;
scanf("%d%d%d",&x,&y,&c);
add(ma[x][y],x,y,-1);
add(c,x,y,1);
ma[x][y]=c;
}
else
{
R int x,y,xx,yy,c;
scanf("%d%d%d%d%d",&x,&xx,&y,&yy,&c);
printf("%d\n",(query(c,xx,yy)-query(c,x-1,yy)-query(c,xx,y-1)+query(c,x-1,y-1)));
}
}
return 0;
}

显示 Gitment 评论