Turtles in the Pond
Time limit2sMemory limit512 MB
Given a growing connected set of grid cells, after each insertion decide whether every pair can be joined by a path that uses only two fixed directions of movement.
- Level
Hard8 of 10
- Topics
- Graph, Union-find, Implementation, Math
- Solved
- No attempts yet
Problem
The pond of Tortilla the turtle has changed a lot since Buratino's last visit. It is now a rectangular grid of columns and rows, with water lilies in some of its cells. On these lilies, on sunny days, numerous children, grandchildren, and great-grandchildren of Tortilla bask.
The pond contains a set of connected lilies. A set of lilies is connected if from any lily one can reach any other by moving between lilies that share a side.
Turtles love visiting each other. When one turtle decides to pay a visit to another, it builds some route that passes through lilies sharing a side and connects its lily to the lily of its acquaintance. However, the sedentary lifestyle of turtles imposes serious constraints on the possible routes: before starting to move, a turtle chooses some two of the four possible directions of movement (up, down, left, right) and then can step from lily to lily only in one of these two directions. For example, in the figure below, the turtle on lily A, wishing to visit the turtle on lily B, needs to choose the directions up and left in order to be able to lay out a route. On the other hand, the turtle on lily C cannot reach lily A, since any path connecting their lilies requires moving in at least three directions.

Turtles consider that the lilies in the pond are convenient for turtles if a turtle can get from any lily to any other using only two directions of movement. For example, the lilies in the pond in the figure above are inconvenient, since from the lily marked C one cannot reach the lily marked A. If, however, one adds to the existing lilies a lily in the cell marked with a star, then the lilies in the pond become convenient for turtles.
As a result of a family council, Tortilla decided to successively add new lilies to the existing ones.
Your task is to determine, before the additions and after each addition, whether the set of lilies is convenient for turtles. Tortilla guarantees that after each successive addition the set of lilies remains connected.
Input
The first line contains two integers , , the number of rows and columns in the grid that is the pond ().
The next line contains an integer , the number of lilies in the pond initially ().
The following lines contain two integers each, : for each lily, the row and column numbers at whose intersection it is located (, ).
The next line contains an integer , the number of lilies Tortilla is going to add ().
The following lines contain, in the same format, pairs of integers , denoting the row number and column number of the cell with the next added lily (, ).
It is guaranteed that no two lilies in the input coincide. It is guaranteed that initially and after each addition the set of lilies is connected.
Output
Output lines, each of which is either the word YES or the word NO, depending on whether at the corresponding moment the system of lilies is convenient for turtles or not. The first line must contain the answer for the initial arrangement of lilies, and each subsequent line must contain the answer after the next added lily.