跳到正文

题解

题解:P17182 [ICPC 2017 Hong Kong R] Black and White

CTLM_qwq_约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年8月3日
收录
2026年8月4日
更新
2026年11月11日
篇幅
约 2 分钟
获赞
1
题目
P17182

版本

  1. v1当前

题目传送门

思路

简单的二维差分,如果不会的花可以去 这里 学习一下。

直接对于每次翻转进行二维差分,然后对差分数组前缀和:

  • 如果算出的值是偶数,那么表示是白色的,无需变化。
  • 反之,那么表示是黑色的,答案加一。

记得卡常。

AC CODE

C++
#include <bits/stdc++.h>
using namespace std;
int T, n, k, a, b, c, d, res[10002][10002];
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cin >> T;
	while (T--) {
		cin >> n >> k;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				res[i][j] = 0;
			}
		}
		for (int i = 1; i <= k; i++) {
			cin >> a >> c >> b >> d;
			res[a][b]++;
			res[c + 1][b]--;
			res[a][d + 1]--;
			res[c + 1][d + 1]++;
		}
		int ans = 0;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				res[i][j] = res[i][j] + res[i - 1][j] + res[i][j - 1] - res[i - 1][j - 1];
				if (res[i][j] % 2) ans++;
			}
		}
		cout << ans << '\n';
	}
	return 0;
}
原文