Land
Time limit2sMemory limit1024 MB
Lines with coefficients in {-1,0,1} are added online, and for each query the task is to tell whether two points lie in the same region of the plane cut by all lines so far.
- Level
Hard8 of 10
- Topics
- Divide and conquer, Union-find, Geometry
- Solved
- No attempts yet
Problem
Farmer Lasagna bought his dream land, where his cows will live and where he will grow his corn. Cows love to eat corn and will destroy any crop they can reach. So Lasagna plans to build long walls that are high and strong enough that his cows cannot cross them. Lasagna is busy with a performance in Sweden, so he asks you to help him organize his land.
The land is a coordinate plane, and each wall is a line on that plane. Lasagna will send Q queries, and each query is one of two types:
-
Build a wall given by the equation , which is the line of all points that satisfy it. Lasagna guarantees that and are integers from -1 to 1.
-
Given the walls built so far, a cow stands at point and a corn crop is planted at point . Can the cow reach and destroy the crop?
Can you help him answer all of his queries?
Input
The first line contains an integer (), the number of queries.
Each of the next lines is in one of two formats.
-
"1 a b c", where , , and are integers with , or , and . The test data guarantees that when the wall is built, no existing wall coincides with the new wall at every point.
-
"2 x1 y1 x2 y2", where , , , and are integers with . is the cow's position and is the corn's position. The test data guarantees that at that moment neither point lies on any wall, and the two points are different.
Output
For each query of the second type, print "DA" on its own line if the cow can reach the crop, or "NE" otherwise.
Hint
In the first sample, the first line is the x-axis, and it separates points with positive y-coordinates from points with negative y-coordinates. The second line is the y-axis, and together they split the points by quadrant.
The sketch in the third sample shows the lines for all walls and all points that were queried.
