Editorial for COCI '10 Contest 3 #6 Mono
Submitting an official solution before solving the problem yourself is a bannable offence.
Let us denote by
Note that the area of a single rectangle can be computed using the inclusion-exclusion principle. The area equals the difference of areas above the lower and upper edge.
Furthermore, the sum of their areas can be obtained by traversing the polygon anticlockwise. Whenever we traverse a horizontal edge, we add the area above it to the sum, with the sign depending on the direction of traversal. Since the uppermost edge will be traversed from right to left, we will add areas in that direction with the negative sign.
Let us select a cell from the table as a potential upper left corner of the rectangle
The number of incidences of a letter within a polygon can be calculated in an analogous way to calculating its area. The only difference is that instead of adding areas, we add the number of incidences of the required letter. A matrix can be precomputed containing data enabling us to answer such queries in constant time.
Finally, the total number of monoliteral polygons is obtained by running the described algorithm for each potential upper left cell in the table. The time complexity is
Comments