Pawn
Time limit1sMemory limit192 MB
Given a large board colored by row intervals, answer whether two squares lie in the same connected same-color region under 8-directional moves.
- Level
Medium7 of 10
- Topics
- Union-find, Intervals, Array, Graph
- Solved
- No attempts yet
Problem
You are given an chessboard. Every square is either black or white. A pawn stands on one square and moves as follows: from its current square it may step to any of the up to eight squares that touch it horizontally, vertically, or diagonally, but only if that neighbouring square has the same colour as the square the pawn currently occupies. In other words, the pawn never changes colour and can only walk within a connected region of same-coloured squares.

Examples of valid moves.
For several pairs of squares, decide whether a pawn placed on the first square of the pair can reach the second square using only such moves.
Input
The first line contains three integers , , and (, , ): the side length of the board, the number of black fragments that describe the colouring, and the number of queries. Rows and columns are numbered from to .
Each of the next lines contains three integers , , (, ), meaning that in row every square whose column lies between and (inclusive) is black. Fragments may overlap. Every square not covered by any fragment is white.
Each of the next lines contains four integers , , , (): a query asking whether a pawn can travel from the square in row , column to the square in row , column .
Output
Print lines, one per query, in the same order as the input. For each query print TAK (meaning yes) if a pawn can move from the first square to the second one without ever stepping onto a square of a different colour, or NIE (meaning no) otherwise.
Hint

The chessboard and the queries used in the sample test.