This page is still under construction.

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

Pawn

Time limit1sMemory limit192 MB

Summary
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 n×nn \times n 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 nn, mm, and pp (1≤n≤1000001 \le n \le 100000, 1≤m≤10000001 \le m \le 1000000, 1≤p≤10001 \le p \le 1000): 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 11 to nn.

Each of the next mm lines contains three integers wiw_i, ki,1k_{i,1}, ki,2k_{i,2} (1≤wi≤n1 \le w_i \le n, 1≤ki,1≤ki,2≤n1 \le k_{i,1} \le k_{i,2} \le n), meaning that in row wiw_i every square whose column lies between ki,1k_{i,1} and ki,2k_{i,2} (inclusive) is black. Fragments may overlap. Every square not covered by any fragment is white.

Each of the next pp lines contains four integers ai,1a_{i,1}, bi,1b_{i,1}, ai,2a_{i,2}, bi,2b_{i,2} (1≤ai,1,bi,1,ai,2,bi,2≤n1 \le a_{i,1}, b_{i,1}, a_{i,2}, b_{i,2} \le n): a query asking whether a pawn can travel from the square in row ai,1a_{i,1}, column bi,1b_{i,1} to the square in row ai,2a_{i,2}, column bi,2b_{i,2}.

Output

Print pp 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.

Examples2

  1. Example 1

    Input
    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
    
    Expected output
    NIE
    TAK
    
  2. Example 2

    Input
    1 1 1
    1 1 1
    1 1 1 1
    
    Expected output
    TAK