洛谷P4054 [jsoi2009]计数问题

题目链接:https://www.luogu.com.cn/problem/P4054

题意:要求维护一个矩阵,支持单点修改,和求一个区域内某个值c出现的次数

这题只要注意到这个c<=100,然后就是个二维BIT的裸题了:开100个二维BIT,单点修改就是+1,求出现次数就是求区域和

#include<bits/stdc++.h>
using namespace std;

int c[105][305][305],a[305][305],n,m,i,j,q;

int lowbit(int x){return x&(-x);}
void add(int k,int x,int y,int d){
	for (int i=x;i<=n;i+=lowbit(i))
	  for (int j=y;j<=m;j+=lowbit(j))
	    c[k][i][j]+=d;
}
int ask(int k,int x,int y){
	int res=0;
	for (int i=x;i>0;i-=lowbit(i))
	  for (int j=y;j>0;j-=lowbit(j))
	    res+=c[k][i][j];
	return res;
}

int main(){
	scanf("%d%d",&n,&m);
	for (i=1;i<=n;i++)
	  for (j=1;j<=m;j++){
	  	scanf("%d",&a[i][j]);
	  	add(a[i][j],i,j,1);
	  }
	scanf("%d",&q);
	while (q--){
	  int op; scanf("%d",&op);
	  if (op==1){
	  	int x,y,ch;
	  	scanf("%d%d%d",&x,&y,&ch);
	  	if (a[x][y]!=ch){
	  	  add(a[x][y],x,y,-1);
	  	  a[x][y]=ch;
		  add(ch,x,y,1);	
		}
	  }
	  else{
	  	int x1,y1,x2,y2,ch;
	  	scanf("%d%d%d%d%d",&x1,&x2,&y1,&y2,&ch);
	  	int ans=ask(ch,x2,y2)-ask(ch,x1-1,y2)-ask(ch,x2,y1-1)+ask(ch,x1-1,y1-1); //*
	  	printf("%d\n",ans);
	  }
	}
	return 0;
}

  

 

posted @ 2020-09-06 00:21  coastal_taipan  阅读(189)  评论(0编辑  收藏  举报