比赛 2026.9.12 评测结果 AAAATTTTTT
题目名称 彩色卡牌 最终得分 40
用户昵称 赵飞羽 运行时间 27.173 s
代码语言 C++ 内存使用 5.58 MiB
提交时间 2026-09-12 10:07:04
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;

constexpr int N = 510, M = 250010;
int n, m, q, a[N][N], b[N][N];
int xy[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
bool vis[N][N], t[M];
struct node{
	int x, y;
};
void bfs(int x, int y, int z) {
	if (a[x][y] > z) {
		cout << 0 << "\n";
		return;
	}
	int res = 1;
	memset(t, 0, sizeof(t));
	memset(vis, 0, sizeof(vis));
	queue <node> q;
	q.push((node){x, y});
	while (!q.empty()) {
		node u = q.front();
		vis[u.x][u.y] = 1;
		t[b[u.x][u.y]] = 1;
		q.pop();
		for (int i = 0; i < 4; i++) {
			int dx = u.x + xy[i][0];
			int dy = u.y + xy[i][1];
			if (dx < 1 || dx > n || dy < 1 || dy > m) continue;
			if (vis[dx][dy]) continue;
			vis[dx][dy] = 1;
			if (a[dx][dy] <= z) {
				if (!t[b[dx][dy]]) {
					t[b[dx][dy]] = 1;
					res++;
				}
				q.push((node{dx, dy}));
			}
		}
	}
	cout << res << "\n";
}

signed main() {
	freopen("card.in", "r", stdin);
	freopen("card.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n >> m >> q;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> a[i][j];
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> b[i][j];
		}
	}
	while (q--) {
		int op, x, y, z;
		cin >> op >> x >> y >> z;
		if (op == 1) b[x][y] = z;
		if (op == 2) bfs(x, y, z);
	}
	return 0;
}