1452: [JSOI2009]Count
Time Limit: 10 Sec Memory Limit: 64 MB
Submit: 1687 Solved: 1000
[Submit][Status][Discuss]
Description
Input
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
Source
JSOI2009Day1
建c个二维树状数组,读入的矩阵当做c,先加1,单点更新的时候先把加的1减去,用更新的c更新矩阵,查询个数的时候直接二维树状数组求和即可。。
只不过多一个for循环。。变量名重复错误弄了我好久QAQ
感谢hzwer学长。。
#include<cstdio>
using namespace std;
int n,m,k,a[301][301],f[101][301][301];
int read()
{
int x=0,w=1;
char ch=getchar();
while (ch<'0' || ch>'9')
{
if (ch=='-')
w=-1;
ch=getchar();
}
while (ch>='0' && ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*w;
}
int lowbit(int x)
{
return x&(-x);
}
void add(int i,int j,int c,int value)
{
int x,y;
for (x=i;x<=n;x+=lowbit(x))
for (y=j;y<=m;y+=lowbit(y))
f[c][x][y]+=value;
}
int query(int i,int j,int c)
{
int ans=0,x,y;
for (x=i;x>=1;x-=lowbit(x))
for (y=j;y>=1;y-=lowbit(y))
ans+=f[c][x][y];
return ans;
}
int main()
{
int i,j,p,x1,y1,x2,y2,c;
n=read();
m=read();
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
{
a[i][j]=read();
add(i,j,a[i][j],1);
}
k=read();
for (i=1;i<=k;i++)
{
p=read();
if (p==1)
{
x1=read();
y1=read();
c=read();
add(x1,y1,a[x1][y1],-1);
a[x1][y1]=c;
add(x1,y1,a[x1][y1],1);
}
if (p==2)
{
x1=read();
x2=read();
y1=read();
y2=read();
c=read();
printf("%d\n",query(x2,y2,c)+query(x1-1,y1-1,c)-query(x1-1,y2,c)-query(x2,y1-1,c));
}
}
return 0;
}