随笔 - 141,  文章 - 0,  评论 - 0,  阅读 - 1407

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;
}

 

posted on   TLECODE  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示