Map
Time limit1sMemory limit128 MB
Given an n by m grid and q queries, each comparing two h by w subrectangles, decide if at most k corresponding cells differ.
- Level
Medium6 of 10
- Topics
- Prefix sum, Matrix, Implementation
- Solved
- No attempts yet
Problem
In Byteland, a new institution has been founded to study how similar the country's various regions are to one another. The map of the country is an rectangle made up of unit squares. Each square is a province, and every province is assigned exactly one natural number describing a characteristic feature (for example, 1 for coal deposits, 2 for lakes, and so on).
Two regions are called -similar if, among all pairs of corresponding provinces, at most pairs have differing features (all the others are identical). Given the map, you must answer queries asking whether two given regions are -similar.
For example, consider the following two regions:
The two regions above are 2-similar and 3-similar, but they are neither 1-similar nor 0-similar.
Input
The first line contains three integers , , (, ): the number of rows and columns of the map and the number of queries. Each of the next lines describes the map; line contains integers (), where is the feature of the province in row , column (rows and columns are numbered from 1).
Each of the next lines contains one query as seven integers (, , ). The two regions are rectangles whose top-left province lies at (column , row ) and (column , row ) respectively, each spanning columns across and rows down; their bottom-right provinces are therefore at (column , row ) and (column , row ). Both regions lie entirely inside the map. Decide whether the two regions are -similar.
Output
Print lines, one per query: TAK if the two regions are -similar, or NIE otherwise.