【bzoj 1452】 Count 【JSOI2009】

Description

一个N*M的方格,初始时每个格子有一个整数权值,接下来每次有2个操作:

改变一个格子的权值

求一个子矩阵中某个特定权值出现的个数

Input

每一行有两个数字N,M

接下来N行,每行M个数字。第i+1行第j个数字表示格子(i,j)的初值

接下来输入一个Q,后面Q行每行描述一个操作

操作1:

1 x y c,表示将格子(x,y)的值变为c

操作2:

2 x1 x2 y1 y2 c,表示询问所有满足格子中数字为c的格子数字

(n,m<=300,Q<=5000)

(1<=x<=N,1<=y<=M,1<=c<=100)

(x1<=x<=x2,y1<=y<=y2)

Output

对于每个操作2,按输入中出现的顺序,依次输出一行一个整数表示所求得的个数

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

对于这道题,对每一种颜色建一个二维树状数组即可,下面是程序:

#include<stdio.h>
#include<iostream>
#define lowbit(n) n&(-n)
using namespace std;
const int N=305;
int n,m,q,map[N][N];
struct D1_tree{
	int c[N];
	void add(int x,int w){
		while(x<=n){
			c[x]+=w;
			x+=lowbit(x);
		}
	}
	int ask(int x){
		int s=0;
		while(x){
			s+=c[x];
			x-=lowbit(x);
		}
		return s;
	}
};
struct D2_tree{
	D1_tree c[N];
	void add(int x,int y,int w){
		while(y<=m){
			c[y].add(x,w);
			y+=lowbit(y);
		}
	}
	int ask(int x,int y){
		int s=0;
		while(y){
			s+=c[y].ask(x);
			y-=lowbit(y);
		}
		return s;
	}
}t[105];
int main(){
	int i,j,a,b,c,d;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++){
		for(j=1;j<=m;j++){
			scanf("%d",&map[i][j]);
			t[map[i][j]].add(i,j,1);
		}
	}
	scanf("%d",&q);
	while(q--){
		scanf("%d",&i);
		if(i==1){
			scanf("%d%d%d",&a,&b,&c);
			t[map[a][b]].add(a,b,-1);
			t[map[a][b]=c].add(a,b,1);
		}
		else{
			scanf("%d%d%d%d%d",&a,&c,&b,&d,&j);
			--a,--b;
			printf("%d\n",t[j].ask(c,d)-t[j].ask(a,d)-t[j].ask(c,b)+t[j].ask(a,b));
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值