AE3B - Pawn

no tags 

The rapidly growing popularity of Bytean chess is the reason why many different variants of this game have been invented. Because the traditional version is played on an infinite chessboard, what can be quite troublesome, sometimes simpler variants are chosen, in which the dimensions of the chessboards are bounded by 100000 * 100000. Some squares of the chessboard are black and the remaining ones are white, however the painting pattern depends on the particular chessboard. A pawn moves on such a chessboard in a bit different way than in traditional chess - it can move horizontally, vertically or diagonally to any of the adjacent eight squares provided that this square has the same colour as the square currently occupied by the pawn.

Examples of valid moves.

For given pairs of squares on the chessboard it should be determined whether a pawn can travel between these squares.

Input

The first line of the standard input contains three integers n, m and P (1 ≤ n ≤ 100000, 1 ≤ m ≤ 1000000, 1 ≤ p ≤ 1000) separated by single spaces and representing the size of the chessboard, the number of black fragments of the chessboard described in the input, and the number of queries, respectively. The chessboard has dimensions n*n and consists of squares with both coordinates between 1 and n. The following m lines contain descriptions of black fragments of the chessboard (they do not necessarily need to be disjoint). Each one of them consists of three integers wi, ki,1 and ki,2 (1 ≤ wi ≤ n, 1 ≤ ki,1 ≤ ki,2 ≤ n) separated by single spaces and meaning that in row wi squares in columns between ki,1 and ki,2 are black. The squares that are not contained in any dark fragment specified in the input are white.

The following P lines contain the queries. Each query consists of two pairs of integers ai,1, bi,1, ai,2, bi,2 (1 ≤ ai,1, bi,1, ai,2, bi,2 ≤ n) separated by single spaces. The query is: can a pawn get from the square in row ai,1 and column bi,1 to the square in row ai,2 and column bi,2?

Output

Your program should output P lines to the standard output: the answers to the respective queries, in the same order as they appear in the input. The answer to each query is a line with a word "TAK" (meaning YES) or "NIE" (meaning NO) without the quotes, depending on whether a pawn can get from the first of the specified squares to the second one without passing through a square with different colour.

Example

For the input data:

4 5 2
1 1 1
2 3 4
3 2 2
4 2 2
4 2 2
1 1 3 2
1 2 4 4

the correct result is:

NIE
TAK

The chessboard and the queries from the example.

Task author: Krzysztof Diks.


hide comments
[Trichromatic] XilinX: 2009-05-24 06:33:16

This problem will stay in tutorial section until the test cases updated.

Ivan Katanic: 2009-05-08 17:49:36

please put more test cases..there are only first two official tests, with small constraints.


Added by:Race with time
Date:2009-05-03
Time limit:7s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:Algorithmic Engagements 2009