For each table, we know which direction it is going to be flipped over, so we can create two rectangle objects for each table. The rectangle of the starting position will be denoted as \(S\), and the rectangle for the ending position will be denoted as \(E\). We can coordinate compress all the values across these rectangles to make the problem slightly easier. First, we can check if any of the tables overlap in their ending states. A standard line sweep can be done over all of the \(E\) rectangles in one dimension, with a segment tree of ongoing rectangles maintained over the other dimension. If any overlap is found, the answer for the test case is `"NO"`. The tables must start in a valid position, and now we know that they also end in a valid position. We must now check if there is an ordering where they stay valid at each step. Let’s create a graph \(G\) with \(N\) nodes, one for each table. Let there be a directed edge from table \(A\) to table \(B\) if flipping over table \(A\) would cause it to intersect with a non-flipped over table \(B\) (\(E\) rectangle from \(A\) intersects with \(S\) rectangle from \(B\)). This means that rectangle \(B\) must be flipped over before rectangle \(A\). If this graph is acyclic then an ordering exists such that the tables never overlap at any step. The ordering would be the reverse of the topological sort based on how edges are defined above. This leads to a simple \(O(N^2)\) solution. Construct the graph above naively by checking every pair of rectangles and adding the edge if it exists, then run a cycle check or topological sorting on the graph. There are \(N\) vertices and \(N^2\) edges so any cycle check or topo sort algorithm will take \(O(V+E) = O(N^2)\). Due to the properties of the graph, it is actually possible to perform a cycle check on this graph on the order of number of vertices, and not edges. Consider the following interactive problem: There is a graph \(G\) with \(V\) vertices and \(E\) edges. There are no self-loops or multi-edges. You are not given the graph, only the number of vertices \(V\) (labeled \(1\) to \(V\)). You may perform \(2\) queries on this graph: Query 1) Remove vertex with label \(X\) from the graph. This will remove all incoming and outgoing edges from this node. Query 2) Ask for any outgoing edge from vertex \(X\). If vertex \(X\) contains any outgoing edges, this will return the vertex of any of them at random. If vertex \(X\) is already removed or contains no outgoing edges, this will return \(-1\). Your task is to determine whether the original graph (before removing any vertices) contains a cycle in no more than \(4*V\) queries. This can be solved via the following algorithm: ``` void dfs(int n) { seen[n] = true; while (!hasCycle) { int to = query2(n); if (to == -1) break; else if (seen[to]) hasCycle = true; else dfs(to); } query1(n); } boolean hasCycle = false; boolean[] seen = new boolean[N]; for (int i = 0; i < N; i++) if (!seen[i]) dfs(i); ``` Query 1 is performed on each node at most once amounting to \(N\) queries. For query 2, if a returned node has previously been visited then a cycle is found and the process ends. Otherwise we visit that node, so query 2 is performed at most once for each time a node is entered, and then potentially once more on each node when no more edges are found. The total amount of queries is at most \(N + N + N + 1\). Since this DFS only follows edges of the original graph, it is easy to see that if no cycle exists in the original graph, no false positive will be found. If a cycle exists, this solution will find it. Consider the initial path of nodes the DFS visits right before calling query 1 on any node: \(A \to B \to C \to D \to E\) We know each of these nodes are distinct because if there were any duplicates, a cycle would be found. We also know that node \(E\) has no outgoing edges in the original graph since no node has been destroyed yet and query 1 is about to be called (if \(E\) had an outgoing edge to \(A-D\), then this cycle would get found and the process would end). Since node \(E\) has no outgoing edges, we can guarantee that it is not part of any cycle in the graph. Node \(E\) then gets safely removed and now the stack looks like \(A \to B \to C \to D\) Now we will take any outgoing edge from \(D\) (if any exist) until we reach another point where a node has no more outgoing edges: \(A \to B \to C \to D \to F \to G\) If we do not reach a point where a node has no more outgoing edges, that means a cycle will get reached and be found. If none of the nodes reachable from \(A\) (including \(A\) itself) are part of a cycle, then this function will eventually remove all of these nodes from the graph and finish without finding a cycle. Then search then starts from another node (if available). Let’s consider the case where one of the reachable nodes is part of a cycle. Let’s consider the stack right after the first node that is part of a cycle is reached: \(A \to B \to C \to D\) Here \(D\) is the first node on the stack that is part of a cycle. It has been shown that nodes are only removed when they are not part of any cycle, and if we call DFS on node \(X\) then eventually all nodes reachable from \(X\) including \(X\) itself will be removed if they are not part of a cycle. This means that we will certainly find and reach any outgoing edges from \(D\) to nodes that also belong to a cycle containing \(D\). This logic continues until we eventually reach a node with an outgoing edge to \(D\) itself. Once we process this edge, we find the cycle and the process ends. Now that we’ve shown we can do cycle section on a graph with these two queries, the original problem needs to be translated to this new problem. We can create a data structure to store/remove rectangles and query for some rectangle if there is another rectangle that intersects with it, and return one of them. Create a segment tree across the \(X\) values of the rectangles. Each node stores an implicit segment tree across \(Y\) values. When adding a rectangle, for its left \(X\) and right \(X\) boundaries, for all \(\log(N)\) nodes including that \(X\), set the index of the lower \(Y\) to be the value of the upper \(Y\). To support insertion and deletion of multiple rectangles with the same lower \(Y\) without disrupting each other, one approach is to give each rectangle a unique lower \(Y\) "key" rather than compressing them all to exactly the same one. Add all the \(S\) rectangles to this data structure. Now to check if a node has any outgoing edges, query from left X to right X for the corresponding \(E\) rectangle of that node. For each \(\log(N)\) nodes in this range, query the max from \([0, \) upper \(Y]\) and check if that value is \(\ge\) lower \(Y\). If this is true, then there is some intersecting rectangle and we can return the index of it by storing extra data. There is one case this will not find: If the intersecting \(S\) rectangle starts at a smaller \(X\) and ends at a larger \(X\) than the \(E\) rectangle we are checking, the \(\log(N)\) nodes in the segment tree will not find this rectangle. If we rotate the problem and also check by having a segment tree across \(Y\) values and implicit segment tree across \(X\), then it will be instead found in the other structure. Since \(E\) rectangles are formed by reflecting across its corresponding \(S\) rectangle, then if there were any case such that an \(E\) rectangle is completely contained inside an \(S\) rectangle (without touching its borders) then there would be an intersection between the two \(S\) rectangles and this would be found during the sweep line. Each insertion into and deletion from this data structure takes \(O(\log^2(N))\) time, and querying for an intersecting rectangle also takes \(O(\log^2(N))\) time. Since these queries are called \(O(N)\) times in the algorithm described above, the overall runtime of this solution is \(O(N \log^2(N))\).