abc D题求调!!崩
  • 板块学术版
  • 楼主Kingna
  • 当前回复9
  • 已保存回复10
  • 发布时间2023/12/2 22:17
  • 上次更新2023/12/3 09:39:23
查看原帖
abc D题求调!!崩
411727
Kingna楼主2023/12/2 22:17

就是把他分成中间一堆整块,周围一堆散块来做。给个 hack 数据也行!!!

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

#define int long long
const int N = 1005;

int n, m, sum[N][N];
char a[N][N];

int get(int a, int b,int c, int d) {
	if (a > c || b > d) return 0;
	return sum[c][d] - sum[a - 1][d] - sum[c][b - 1] + sum[a - 1][b - 1];
}

signed main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) cin >> a[i][j], sum[i][j] = (a[i][j] == 'B');
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (j) sum[i][j] += sum[i][j - 1];
			if (i) sum[i][j] += sum[i - 1][j];
			if (i && j)sum[i][j] -= sum[i - 1][j - 1];
		}
	}
	while (m--) {
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		int aa = a % n, bb = b % n, cc = c % n, dd = d % n;
		
		
		int pos1 = a;
		while (1) {
			if (pos1 % n == n - 1) break;
			pos1++;
		}
		int pos2 = c;
		while (1) {
			if (pos2 % n == 0) break;
			pos2--;
		}
		int pos3 = b;
		while (1) {
			if (pos3 % n == n - 1) break;
			pos3++;
		}
		int pos4 = d;
		while (1) {
			if (pos4 % n == 0) break;
			pos4--;
		}
//		cout << pos1 << ' ' << pos2 << ' ' << pos3 << ' ' << pos4 << endl;
		int J = get(aa, bb, n - 1, n - 1) + get(0, bb, cc, n - 1) + get(aa, 0, n - 1, dd) + get(0, 0, cc, dd);
		int tt = get(aa, 0, n - 1, n - 1) * (pos4 - 1 - (pos3 + 1) + 1) / n
			+ get(0, 0, cc, n - 1) * (pos4 - 1 - (pos3 + 1) + 1) / n
			+ get(0, bb, n - 1, n - 1) * (pos2 - 1 - (pos1 + 1) + 1) / n
			+ get(0, 0, n - 1, dd) * (pos2 - 1 - (pos1 + 1) + 1) / n;
		int ll = get(0, 0, n - 1, n - 1) * (pos4 - 1 - (pos3 + 1) + 1) / n * (pos2 - 1 - (pos1 + 1) + 1) / n;
		
//		cout << J << ' ' << tt << ' ' << ll << endl; 
		cout << J + tt + ll << endl;	 
	}
}
2023/12/2 22:17
加载中...