Space Gophers
Time limit20sMemory limit512 MB
Given tunnels that each remove a full axis-parallel line from a cube, answer whether two empty cells are connected through empty cells.
- Level
Hard8 of 10
- Topics
- Union-find, Segment tree, Sorting
- Solved
- No attempts yet
Problem
Space Gophers live on a strange, cube-shaped asteroid whose every side has length exactly . The geographers have divided the asteroid into microcubes and introduced a standard Cartesian coordinate system, so every microcube has three coordinates .
The Gophers' main occupation, and also their favorite pastime, is tunnel digging. Many tunnels have been dug in the asteroid. Each tunnel starts at one of the asteroid's outer walls and is a straight line of removed microcubes, perpendicular to that wall, running all the way through to the opposite side. Today is a grand festival, though, and all digging is forbidden for now. Any Gopher who wants to visit friends must move through empty space only.
Navigation is hard for the Gophers, so they need your help. You are given a list of pairs of starting and ending positions. For each pair, determine whether the end can be reached from the start by moving only through empty microcubes. The Gophers move only through space they have dug, and they never leave the initial cube. This is probably some kind of agoraphobia.
Input
The first line contains the number of test cases (). Each test case is described as follows.
The first line of a test case contains a positive integer (), the number of tunnels. The next lines describe the tunnels. Each line contains a triple of integers of one of the forms , , or , where .
The triple means that a tunnel removes all microcubes for every integer . Similarly, removes all , and removes all .
The next line contains the number of queries (). Each of the following lines contains six integers , each between 1 and . Each query asks for a route from the cube to the cube that visits only empty cubes and moves between adjacent cubes. Two cubes are adjacent if they share a face. All starting and ending positions are guaranteed to be empty.
Output
For each test case, output the answers to all of its queries, one per line. Output "YES" if a route exists and "NO" if it does not.