Ten
Time limit0.5sMemory limit1024 MB
Count the rectangular submatrices of a positive-integer matrix whose entries sum to exactly 10, with dimensions up to 300 by 300.
- Level
Medium6 of 10
- Topics
- Prefix sum, Two pointers, Binary search, Matrix
- Solved
- No attempts yet
Problem
The real estate company IC manages a rectangular section of land. The section is divided into segments in an matrix, where the number of rows is and the number of columns is . Each segment has a price, which is a positive integer. IC wants to sell a rectangular subsection of the land, and the price of that subsection must be ten. The price of a subsection is the sum of the prices of the segments in it. Several such subsections may exist, so IC wants to know how many candidate subsections it can sell. Write a program that helps IC count the candidate subsections of the land.
For example, the prices of the segments of a land with segments are given as follows.

We can find four candidate subsections to sell, marked by rectangles: the first consists of four segments in the first and second rows spanning from the second to the third columns, the second consists of six segments in the second and third rows spanning from the third to the fifth columns, the third consists of two segments in the first row spanning from the fifth to the sixth columns, and the fourth consists of three segments in the seventh column spanning from the third to the fifth rows. Therefore, for the input above, your program must print 4.
Input
Your program reads from standard input. The first line of the input contains two positive integers and (), the dimensions of the land, separated by a space. Each of the following lines contains positive integers , the prices of the segments in the -th row (, , ), also separated by a space.
Output
Your program writes to standard output. Print exactly one line containing an integer, the number of rectangular subsections whose price is ten.