前缀和80分求助
查看原帖
前缀和80分求助
878106
vivid_wei楼主2025/8/3 12:17
#include <iostream>
using namespace std;
int n, m, k;
long long a[501][501], b[501][501], c[501][501];
int ans;

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	cin >> n >> m >> k;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> a[i][j];
			b[i][j] = b[i][j - 1] + a[i][j];
			c[i][j] = c[i - 1][j] + b[i][j];
		}
	}
	for (int x1 = 1; x1 <= n; x1++) {
		for (int y1 = 1; y1 <= m; y1++) {
			for (int x2 = x1; x2 <= n; x2++) {
				for (int y2 = y1; y2 <= m; y2++) {
					int res = c[x2][y2] - c[x1 - 1][y2] - c[x2][y1 - 1] + c[x1 - 1][y1 - 1];
					if (res <= k) {
						ans++;
					}
				}
			}
		}
	}
	cout << ans << endl;
	return 0;
}
2025/8/3 12:17
加载中...