AtCoder abc461_d Count Subgrid Sum = K

题目描述

There is an $ H \times W $ grid, and each cell contains an integer $ 0 $ or $ 1 $ .
The information of the integer written in each cell is given as $ H $ strings $ S_1, S_2, \dots, S_H $ of length $ W $ . If the $ j $ -th character of $ S_i $ is 0, then $ 0 $ is written in the cell at the $ i $ -th row from the top and the $ j $ -th column from the left of the grid; if the $ j $ -th character of $ S_i $ is 1, then $ 1 $ is written in that cell.

Find the number of rectangular regions where the sum of the written integers equals $ K $ .
More formally, find the number of quadruples of integers $ (r_1, c_1, r_2, c_2) $ satisfying all of the following conditions:

  • $ 1 \le r_1 \le r_2 \le H $
  • $ 1 \le c_1 \le c_2 \le W $
  • The sum of the integers written in the cell at the $ i $ -th row from the top and the $ j $ -th column from the left, over all pairs of integers $ (i, j) $ satisfying $ r_1 \le i \le r_2, c_1 \le j \le c_2 $ , equals $ K $ .

输入格式

The input is given from Standard Input in the following format:

$ H $ $ W $ $ K $ $ S_1 $ $ S_2 $ $ \vdots $ $ S_H $

输出格式

Output the answer.

输入输出样例 #1

输入 #1

3 4 3
1001
1101
0110

输出 #1

8

输入输出样例 #2

输入 #2

5 4 20
0101
1010
0101
1010
0101

输出 #2

0

输入输出样例 #3

输入 #3

15 20 17
10111101101100000100
01100000000010000011
01110010111000111000
11001100000111011000
10100001100011100010
01101000101010000101
10110001111110000100
10110011101100101101
01010001110011001001
01010110010001100110
01110100011110011110
01100000100111010010
11001101100111101100
10111100010101111011
00101101011100010000

输出 #3

448

说明/提示

Sample Explanation 1

There are eight rectangular regions where the sum of the written integers equals $ K $ :

  • The rectangular region extracted from row $ 1 $ to row $ 2 $ from the top and column $ 1 $ to column $ 2 $ from the left
  • The rectangular region extracted from row $ 1 $ to row $ 2 $ from the top and column $ 1 $ to column $ 3 $ from the left
  • The rectangular region extracted from row $ 1 $ to row $ 2 $ from the top and column $ 2 $ to column $ 4 $ from the left
  • The rectangular region extracted from row $ 1 $ to row $ 3 $ from the top and column $ 2 $ to column $ 3 $ from the left
  • The rectangular region extracted from row $ 1 $ to row $ 3 $ from the top and column $ 3 $ to column $ 4 $ from the left
  • The rectangular region extracted from row $ 2 $ to row $ 2 $ from the top and column $ 1 $ to column $ 4 $ from the left
  • The rectangular region extracted from row $ 2 $ to row $ 3 $ from the top and column $ 1 $ to column $ 2 $ from the left
  • The rectangular region extracted from row $ 2 $ to row $ 3 $ from the top and column $ 2 $ to column $ 3 $ from the left

Constraints

  • $ H $ and $ W $ are integers between $ 1 $ and $ 500 $ , inclusive.
  • $ K $ is an integer between $ 0 $ and $ HW $ , inclusive.
  • $ S_i $ is a string of length $ W $ consisting of 0 and 1.

思路概述

问题是求某个矩形内数字之和为定值的数量。所以用二维前缀和来解决。

其实这题很板,只需要略微优化一下输入和统计就行了。

热知识:输入字符串比输入字符慢很多。

代码示例

#include <bits/stdc++.h>
using namespace std;
const int H=510,W=510;
int h,w,k;
long long ans;
string s;
char c;
int a[H][W],sum[H][W];
int GetSum(int lx,int ly,int rx,int ry) {
	return sum[rx][ry]-sum[rx][ly-1]-sum[lx-1][ry]+sum[lx-1][ly-1];
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin>>h>>w>>k;
	for(int i=1;i<=h;i++) {
		for(int j=1;j<=w;j++) {
			cin>>c;
			a[i][j]=c-'0';
		}
	}
	for(int i=1;i<=h;i++)
		for(int j=1;j<=w;j++)
			sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+a[i][j];
	for(int x=1;x<=h;x++)
		for(int i=x;i<=h;i++)
			for(int y=1;y<=w;y++)
				for(int j=y;j<=w;j++)
					ans+=(GetSum(x,y,i,j)==k);
	cout<<ans;
	return 0;
}
posted @ 2026-06-10 20:46  naijil  阅读(10)  评论(0)    收藏  举报