#P11053. Mosaic Coloring Queries
Mosaic Coloring Queries
Mosaic Coloring Queries
Salma wants to color a mosaic made up of square tiles. There are a total of tiles, each with dimensions , and initially none of them are colored. The rows are numbered from to from top to bottom and the columns from to from left to right. The tile at row and column is denoted by . Each tile is painted either white (denoted by ) or black (denoted by ).
Initially, Salma selects two binary arrays and , each of length , with the condition that . She paints the top row (row ) according to the array , setting the color of tile to for . Similarly, she paints the leftmost column (column ) according to the array , so that the color of tile is for .
Then, she repeatedly colors an uncolored tile for which both the tile directly above and the tile directly to the left are already colored. The rule is as follows:
[
\text{color}(i,j)=\begin{cases} 1, & \text{if } \text{color}(i-1,j)=0 \text{ and } \text{color}(i,j-1)=0,\ 0, & \text{otherwise.} \end{cases}
]
It can be proven that the final coloring of the mosaic is independent of the order in which Salma colors the tiles.
Yasmin is curious about the final colors and asks queries. In the -th query (), a subrectangle of the mosaic is specified by its top row , bottom row , left column , and right column , where and . The answer to the query is the number of black tiles (tiles with color ) contained in the specified subrectangle.
Your task is to implement a function that efficiently answers these queries.
inputFormat
Input is given as follows:
- The first line contains two integers and , where is the size of the mosaic and is the number of queries.
- The second line contains integers (each or ) representing the array , which gives the colors for the top row.
- The third line contains integers (each or ) representing the array , which gives the colors for the left column ().
- Then lines follow; each line contains four integers , , , and , defining a query.
outputFormat
For each query, output the number of black tiles in the specified subrectangle. Each answer should be printed on a separate line.
sample
3 3
0 1 0
0 0 1
0 1 0 1
1 2 1 2
0 2 0 2
1
1
3
</p>