This page is still under construction.

Parts of this page are still being built. What you see may change.

Map

Time limit1sMemory limit128 MB

Summary
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 n×mn \times m rectangle made up of n⋅mn \cdot m 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 kk-similar if, among all pairs of corresponding provinces, at most kk pairs have differing features (all the others are identical). Given the map, you must answer queries asking whether two given regions are kk-similar.

For example, consider the following two 3×33 \times 3 regions:

113
523
628
913
513
628

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 nn, mm, qq (1≤n,m≤2001 \le n, m \le 200, 1≤q≤200001 \le q \le 20000): the number of rows and columns of the map and the number of queries. Each of the next nn lines describes the map; line i+1i+1 contains mm integers ai,1,…,ai,ma_{i,1}, \dots, a_{i,m} (1≤ai,j≤1001 \le a_{i,j} \le 100), where ai,ja_{i,j} is the feature of the province in row ii, column jj (rows and columns are numbered from 1).

Each of the next qq lines contains one query as seven integers x1 y1 x2 y2 w h kx_1\ y_1\ x_2\ y_2\ w\ h\ k (0≤k≤10000 \le k \le 1000, 1≤w≤m1 \le w \le m, 1≤h≤n1 \le h \le n). The two regions are rectangles whose top-left province lies at (column x1x_1, row y1y_1) and (column x2x_2, row y2y_2) respectively, each spanning ww columns across and hh rows down; their bottom-right provinces are therefore at (column x1+w−1x_1+w-1, row y1+h−1y_1+h-1) and (column x2+w−1x_2+w-1, row y2+h−1y_2+h-1). Both regions lie entirely inside the map. Decide whether the two regions are kk-similar.

Output

Print qq lines, one per query: TAK if the two regions are kk-similar, or NIE otherwise.

Examples2

  1. Example 1

    Input
    3 4 2
    1 1 1 2
    1 2 1 1
    1 1 1 2
    1 1 3 1 2 3 2
    1 1 3 1 2 3 3
    
    Expected output
    NIE
    TAK
    
  2. Example 2

    Input
    3 6 4
    1 1 3 9 1 3
    5 2 3 5 1 3
    6 2 8 6 2 8
    1 1 4 1 3 3 0
    1 1 4 1 3 3 1
    1 1 4 1 3 3 2
    1 1 4 1 3 3 3
    
    Expected output
    NIE
    NIE
    TAK
    TAK