PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#PS0000215. Đếm hình chữ nhật có tổng bằng K (Number of Submatrices That Sum to Target)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Đếm hình chữ nhật có tổng bằng K (Number of Submatrices That Sum to Target)
Number of Submatrices That Sum to Target
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given an integer matrix with rows and columns, count nonempty rectangular submatrices with cell sum exactly . Each rectangle contains all cells from row to and column to , inclusive. Rectangles with different boundary coordinates count separately, even if their sums coincide.
Input
The first line contains three integers . Each of the next lines contains exactly integers .
Output
Print the number of valid rectangles.
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; ; .
- Subtask 3 (50 points): ; ; .
Example 1
Input
3 3 0
0 1 0
1 1 1
0 1 0
Output
4
Explanation
Only the four zero-valued corner cells are valid rectangles.
Example 2
Input
2 2 0
1 -1
-1 1
Output
5
Explanation
Both rows, both columns, and the whole matrix sum to 0.
Example 3
Input
1 1 0
904
Output
0
Explanation
The sole cell is 904, not 0.